blob: bcd8319984250a090b201d9565814d06a4e1bbba (
plain)
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
|
using System;
using UnityEngine;
public class DestroyableSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
public static bool InstanceExists
{
get
{
return DestroyableSingleton<T>._instance;
}
}
public static T Instance
{
get
{
if (!DestroyableSingleton<T>._instance)
{
DestroyableSingleton<T>._instance = UnityEngine.Object.FindObjectOfType<T>();
if (!DestroyableSingleton<T>._instance)
{
DestroyableSingleton<T>._instance = new GameObject().AddComponent<T>();
}
}
return DestroyableSingleton<T>._instance;
}
}
private static T _instance;
public bool DontDestroy;
public virtual void Awake()
{
if (!DestroyableSingleton<T>._instance)
{
DestroyableSingleton<T>._instance = (this as T);
if (this.DontDestroy)
{
UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
return;
}
}
else if (DestroyableSingleton<T>._instance != this)
{
UnityEngine.Object.Destroy(base.gameObject);
}
}
public virtual void OnDestroy()
{
if (!this.DontDestroy)
{
DestroyableSingleton<T>._instance = default(T);
}
}
}
|