-
Notifications
You must be signed in to change notification settings - Fork 10
/
BaseSingleton.cs
141 lines (114 loc) · 5.03 KB
/
BaseSingleton.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using yaSingleton.Helpers;
using yaSingleton.Utility;
namespace yaSingleton {
/// <summary>
/// Base class for singletons. Contains method stubs and the Create method. Use this to create custom Singleton flavors.
/// If you're looking to create a singleton, inherit Singleton or LazySingleton.
/// </summary>
public abstract class BaseSingleton : PreloadedScriptableObject {
protected static SingletonUpdater Updater {
get { return SingletonUpdater.Updater; }
}
internal abstract void CreateInstance();
// Reduce the visibility of OnEnable; Inheritors should override Initialize instead.
private new void OnEnable() {
base.OnEnable();
#if UNITY_EDITOR
if(!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) {
return;
}
#endif
AllSingletons.Add(this);
}
// Reduce the visibility of OnDisable; Inheritors should override Deinitialize instead.
private new void OnDisable() {
base.OnDisable();
}
protected virtual void Initialize() {
Updater.DestroyEvent += Deinitialize;
Updater.FixedUpdateEvent += OnFixedUpdate;
Updater.UpdateEvent += OnUpdate;
Updater.LateUpdateEvent += OnLateUpdate;
Updater.ApplicationFocusEvent += OnApplicationFocus;
Updater.ApplicationPauseEvent += OnApplicationPause;
Updater.ApplicationQuitEvent += OnApplicationQuit;
Updater.DrawGizmosEvent += OnDrawGizmos;
Updater.PostRenderEvent += OnPostRender;
Updater.PreCullEvent += OnPreCull;
Updater.PreRenderEvent += OnPreRender;
}
protected virtual void Deinitialize() { }
#region UnityEvents
public virtual void OnFixedUpdate() { }
public virtual void OnUpdate() { }
public virtual void OnLateUpdate() { }
public virtual void OnApplicationFocus() { }
public virtual void OnApplicationPause() { }
public virtual void OnApplicationQuit() { }
public virtual void OnDrawGizmos() { }
public virtual void OnPostRender() { }
public virtual void OnPreCull() { }
public virtual void OnPreRender() { }
#endregion
#region Coroutines
/// <summary>
/// <para>Starts a coroutine.</para>
/// </summary>
/// <param name="routine">The coroutine</param>
protected Coroutine StartCoroutine(IEnumerator routine) {
return Updater.StartCoroutine(routine);
}
/// <summary>
/// <para>Starts a coroutine named methodName.</para>
/// </summary>
/// <param name="methodName">Name of coroutine.</param>
protected Coroutine StartCoroutine(string methodName) {
return Updater.StartCoroutine(methodName);
}
/// <summary>
/// <para>Stops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.</para>
/// </summary>
/// <param name="routine">Name of the function in code, including coroutines.</param>
protected void StopCoroutine(Coroutine routine) {
Updater.StopCoroutine(routine);
}
/// <summary>
/// <para>Stops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.</para>
/// </summary>
/// <param name="routine">Name of the function in code, including coroutines.</param>
protected void StopCoroutine(IEnumerator routine) {
Updater.StopCoroutine(routine);
}
/// <summary>
/// <para>Stops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.</para>
/// </summary>
/// <param name="methodName">Name of coroutine.</param>
protected void StopCoroutine(string methodName) {
Updater.StopCoroutine(methodName);
}
/// <summary>
/// <para>Stops all coroutines running on this behaviour.</para>
/// </summary>
protected void StopAllCoroutines() {
Updater.StopAllCoroutines();
}
#endregion
public static readonly List<BaseSingleton> AllSingletons = new List<BaseSingleton>();
protected static T GetOrCreate<T>() where T : BaseSingleton {
var instance = AllSingletons.FirstOrDefault(s => s.GetType() == typeof(T)) as T;
instance = instance ? instance : CreateInstance<T>();
instance.Initialize();
return instance;
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void InitializeSingletons() {
foreach(var singleton in AllSingletons) {
singleton.CreateInstance();
}
}
}
}