Skip to content

Commit b402fa8

Browse files
author
unknown
committed
Merge branch 'release/0.2.1' into stable
2 parents 08ba46c + 7849357 commit b402fa8

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

Runtime/PreloadSettings.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,39 @@ public static SettingsProvider LoadSettingsProvider()
5656

5757
private static PreloadSettings GetOrCreateSettings()
5858
{
59-
if (_settings != null) return _settings;
59+
try
60+
{
61+
if (_settings != null) return _settings;
6062

61-
_settings = PreloadSettings.Instance;
62-
63-
if (_settings != null) return _settings;
64-
65-
_settings = AssetDatabase.LoadAssetAtPath<PreloadSettings>(SETTINGS_PATH);
63+
_settings = PreloadSettings.Instance;
64+
65+
if (_settings != null) return _settings;
66+
67+
_settings = AssetDatabase.LoadAssetAtPath<PreloadSettings>(SETTINGS_PATH);
68+
if (_settings != null)
69+
{
70+
Debug.Log("FOUND SETTINGS C, RETURNING");
71+
72+
return _settings;
73+
}
6674

67-
if (_settings != null) return _settings;
75+
if (!Directory.Exists(SETTINGS_PATH))
76+
{
77+
Directory.CreateDirectory(SETTINGS_PATH);
78+
}
79+
80+
_settings = ScriptableObject.CreateInstance<PreloadSettings>();
6881

69-
if (!Directory.Exists(SETTINGS_PATH))
82+
AssetDatabase.CreateAsset(_settings, SETTINGS_PATH);
83+
AssetDatabase.SaveAssets();
84+
85+
return _settings;
86+
}
87+
catch (Exception _exception)
7088
{
71-
Directory.CreateDirectory(SETTINGS_PATH);
89+
Debug.LogError(_exception);
90+
throw;
7291
}
73-
74-
_settings = ScriptableObject.CreateInstance<PreloadSettings>();
75-
76-
AssetDatabase.CreateAsset(_settings, SETTINGS_PATH);
77-
AssetDatabase.SaveAssets();
78-
79-
return _settings;
8092
}
8193

8294
private static SettingsProvider CreateSettingsProvider()

Runtime/ScriptableObjectSingleton.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using System.Collections.Generic;
21
using System.Linq;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
34

45
using UnityEngine;
56

@@ -28,17 +29,28 @@ public static T Instance
2829
{
2930
if (InstanceExists) return _internalInstance;
3031

32+
Object[] _preloadedAssets = PreloadedAssets;
33+
foreach (Object _preloadedAsset in _preloadedAssets)
34+
{
35+
if (_preloadedAsset is T _singleton) //(_preloadedAsset.GetType() == typeof(T))
36+
{
37+
return _internalInstance = _singleton;
38+
}
39+
}
40+
3141
T[] _found = Resources.FindObjectsOfTypeAll<T>();
3242

3343
//TODO: Ensured?
34-
if (_found != null)
44+
if (_found != null && _found.Length != 0)
3545
{
3646
if (_found[0] != null)
3747
{
3848
return _internalInstance = _found[0];
3949
}
4050
}
4151

52+
Debug.LogWarning("Couldn't find Singleton Instance!");
53+
4254
return null;
4355
//return _internalInstance = CreateInstance<T>();
4456
}
@@ -49,13 +61,13 @@ protected set
4961
#if UNITY_EDITOR
5062
if(_internalInstance == null) return;
5163

52-
List<Object> _preloadedAssets = PlayerSettings.GetPreloadedAssets().ToList();
64+
List<Object> _preloadedAssets = PreloadedAssets.ToList();
5365

5466
if (_preloadedAssets.Contains(_internalInstance)) return;
5567

5668
_preloadedAssets.Add(_internalInstance);
5769

58-
PlayerSettings.SetPreloadedAssets(_preloadedAssets.ToArray());
70+
PreloadedAssets = _preloadedAssets.ToArray();
5971
#endif
6072
}
6173
}
@@ -64,6 +76,14 @@ protected set
6476
[PublicAPI]
6577
public static bool InstanceExists => (_internalInstance != null);
6678

79+
private static Object[] PreloadedAssets
80+
{
81+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
82+
get => PlayerSettings.GetPreloadedAssets();
83+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
84+
set => PlayerSettings.SetPreloadedAssets(value);
85+
}
86+
6787
#endregion
6888

6989
#region Methods

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.common-games.utils.preload",
33
"displayName": "CGTK.Utils.Preload",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"unity": "2020.3",
66
"description": "Allows one to easily preload assets before entering the first scene.",
77
"keywords": [

0 commit comments

Comments
 (0)