-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingletonBehaviour.cs
56 lines (41 loc) · 987 Bytes
/
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
//Author: João Azuaga
using UnityEngine;
namespace razveck.UnityUtility {
[DefaultExecutionOrder(-1000)]
public abstract class SingletonBehaviour<T> : MonoBehaviour where T : SingletonBehaviour<T> {
public static T Instance;
private void Awake() {
if(Instance != null) {
gameObject.SafeDestroy();
return;
}
Instance = (T)this;
transform.SetParent(null);
DontDestroyOnLoad(this);
Debug.Log($"{Instance.GetType()} initialized.");
AwakeInternal();
}
protected virtual void AwakeInternal() { }
protected void OnEnable() {
if(Instance != this) {
return;
}
OnEnableInternal();
}
protected virtual void OnEnableInternal() { }
private void Start() {
if(Instance != this) {
return;
}
StartInternal();
}
protected virtual void StartInternal() { }
private void OnDestroy() {
if(Instance != this) {
return;
}
OnDestroyInternal();
}
protected virtual void OnDestroyInternal() { }
}
}