com.wallstop-studios.unity-helpers 2.0.0-rc80 → 2.0.0-rc80.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
namespace WallstopStudios.UnityHelpers.Core.Attributes
|
|
2
|
+
{
|
|
3
|
+
using System;
|
|
4
|
+
|
|
5
|
+
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
|
|
6
|
+
public sealed class ScriptableSingletonPathAttribute : Attribute
|
|
7
|
+
{
|
|
8
|
+
public readonly string resourcesPath;
|
|
9
|
+
|
|
10
|
+
public ScriptableSingletonPathAttribute(string resourcesPath)
|
|
11
|
+
{
|
|
12
|
+
this.resourcesPath = resourcesPath ?? string.Empty;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -5,6 +5,7 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
5
5
|
using System.Linq;
|
|
6
6
|
using Core.Extension;
|
|
7
7
|
using UnityEngine;
|
|
8
|
+
using UnityEngine.Serialization;
|
|
8
9
|
|
|
9
10
|
[Flags]
|
|
10
11
|
public enum ChildSpawnMethod
|
|
@@ -20,6 +21,10 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
20
21
|
{
|
|
21
22
|
private static readonly HashSet<GameObject> SpawnedPrefabs = new();
|
|
22
23
|
|
|
24
|
+
[FormerlySerializedAs("dontDestroyOnLoad")]
|
|
25
|
+
[SerializeField]
|
|
26
|
+
private bool _dontDestroyOnLoad = true;
|
|
27
|
+
|
|
23
28
|
[SerializeField]
|
|
24
29
|
private ChildSpawnMethod _spawnMethod = ChildSpawnMethod.Start;
|
|
25
30
|
|
|
@@ -58,6 +63,7 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
58
63
|
|
|
59
64
|
private void Spawn()
|
|
60
65
|
{
|
|
66
|
+
TrySetDontDestroyOnLoad();
|
|
61
67
|
if (
|
|
62
68
|
_prefabs
|
|
63
69
|
.Concat(_editorOnlyPrefabs)
|
|
@@ -133,5 +139,13 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
133
139
|
}
|
|
134
140
|
return child;
|
|
135
141
|
}
|
|
142
|
+
|
|
143
|
+
private void TrySetDontDestroyOnLoad()
|
|
144
|
+
{
|
|
145
|
+
if (_dontDestroyOnLoad && Application.isPlaying && !gameObject.IsDontDestroyOnLoad())
|
|
146
|
+
{
|
|
147
|
+
DontDestroyOnLoad(gameObject);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
136
150
|
}
|
|
137
151
|
}
|
|
@@ -2,6 +2,8 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
2
2
|
{
|
|
3
3
|
using System;
|
|
4
4
|
using UnityEngine;
|
|
5
|
+
using WallstopStudios.UnityHelpers.Core.Attributes;
|
|
6
|
+
using WallstopStudios.UnityHelpers.Core.Helper;
|
|
5
7
|
#if ODIN_INSPECTOR
|
|
6
8
|
using Sirenix.OdinInspector;
|
|
7
9
|
#endif
|
|
@@ -14,9 +16,46 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
14
16
|
#endif
|
|
15
17
|
where T : ScriptableObjectSingleton<T>
|
|
16
18
|
{
|
|
19
|
+
private static string GetResourcesPath()
|
|
20
|
+
{
|
|
21
|
+
Type type = typeof(T);
|
|
22
|
+
ScriptableSingletonPathAttribute attribute =
|
|
23
|
+
ReflectionHelpers.GetAttributeSafe<ScriptableSingletonPathAttribute>(type);
|
|
24
|
+
if (attribute != null && !string.IsNullOrWhiteSpace(attribute.resourcesPath))
|
|
25
|
+
{
|
|
26
|
+
return attribute.resourcesPath;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return type.Name;
|
|
30
|
+
}
|
|
31
|
+
|
|
17
32
|
protected static readonly Lazy<T> LazyInstance = new(() =>
|
|
18
33
|
{
|
|
19
|
-
|
|
34
|
+
string path = GetResourcesPath();
|
|
35
|
+
T[] instances = Resources.LoadAll<T>(path);
|
|
36
|
+
|
|
37
|
+
if (instances == null || instances.Length == 0)
|
|
38
|
+
{
|
|
39
|
+
T named = Resources.Load<T>(typeof(T).Name);
|
|
40
|
+
if (named != null)
|
|
41
|
+
{
|
|
42
|
+
instances = new[] { named };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (instances == null || instances.Length == 0)
|
|
47
|
+
{
|
|
48
|
+
instances = Resources.LoadAll<T>(string.Empty);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (instances == null)
|
|
52
|
+
{
|
|
53
|
+
Debug.LogError(
|
|
54
|
+
$"Failed to find ScriptableSingleton of {typeof(T).Name} - null instances."
|
|
55
|
+
);
|
|
56
|
+
return default;
|
|
57
|
+
}
|
|
58
|
+
|
|
20
59
|
switch (instances.Length)
|
|
21
60
|
{
|
|
22
61
|
case 1:
|
|
@@ -25,7 +64,9 @@ namespace WallstopStudios.UnityHelpers.Utils
|
|
|
25
64
|
}
|
|
26
65
|
case 0:
|
|
27
66
|
{
|
|
28
|
-
Debug.LogError(
|
|
67
|
+
Debug.LogError(
|
|
68
|
+
$"Failed to find ScriptableSingleton of type {typeof(T).Name} - empty instances."
|
|
69
|
+
);
|
|
29
70
|
return null;
|
|
30
71
|
}
|
|
31
72
|
}
|
package/package.json
CHANGED