-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSingletonBehaviour.cs
79 lines (61 loc) · 2.4 KB
/
SingletonBehaviour.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using JetBrains.Annotations;
namespace UnityEngine
{
public abstract class SingletonBehaviour<T> : SingletonBehaviour where T : MonoBehaviour
{
[CanBeNull]
private static T _instance;
[NotNull]
private static readonly object Lock = new object();
[SerializeField]
private bool _persistent = true;
[NotNull]
public static T Instance
{
get
{
if (Quitting)
{
Debug.LogWarning($"[{nameof(SingletonBehaviour)}<{typeof(T)}>] Instance will not be returned because the application is quitting.");
return null;
}
lock (Lock)
{
if (_instance != null)
return _instance;
var instances = FindObjectsOfType<T>();
var count = instances.Length;
if (count > 0)
{
if (count == 1)
return _instance = instances[0];
Debug.LogWarning($"[{nameof(SingletonBehaviour)}<{typeof(T)}>] There should never be more than one {nameof(SingletonBehaviour)} of type {typeof(T)} in the scene, but {count} were found. The first instance found will be used, and all others will be destroyed.");
for (var i = 1; i < instances.Length; i++)
{
Destroy(instances[i]);
}
return _instance = instances[0];
}
Debug.Log($"[{nameof(SingletonBehaviour)}<{typeof(T)}>] An instance is needed in the scene and no existing instances were found, so a new instance will be created.");
return _instance = new GameObject($"({nameof(SingletonBehaviour)}){typeof(T)}")
.AddComponent<T>();
}
}
}
private void Awake()
{
if (this._persistent)
DontDestroyOnLoad(this.gameObject);
OnAwake();
}
protected virtual void OnAwake() { }
}
public abstract class SingletonBehaviour : MonoBehaviour
{
public static bool Quitting { get; private set; }
private void OnApplicationQuit()
{
Quitting = true;
}
}
}