-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonoSingleton.cs
More file actions
74 lines (59 loc) · 1.11 KB
/
MonoSingleton.cs
File metadata and controls
74 lines (59 loc) · 1.11 KB
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
using UnityEngine;
// namespace FGUFW.Core
// {
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
private static T mInstance = null;
public static T I
{
get
{
if (mInstance == null)
{
mInstance = GameObject.FindObjectOfType(typeof(T)) as T;
if (mInstance == null)
{
GameObject go = new GameObject(typeof(T).Name);
mInstance = go.AddComponent<T>();
}
}
return mInstance;
}
}
private void Awake()
{
if (mInstance == null)
{
mInstance = this as T;
if(IsDontDestroyOnLoad())
{
DontDestroyOnLoad(gameObject);
}
Init();
}
else
{
Debug.LogError("mono单例重复");
}
}
/// <summary>
/// This function is called when the MonoBehaviour will be destroyed.
/// </summary>
void OnDestroy()
{
Dispose();
MonoSingleton<T>.mInstance = null;
}
protected virtual void Init()
{
}
public void DestroySelf()
{
UnityEngine.Object.Destroy(gameObject);
}
public virtual void Dispose()
{
}
protected abstract bool IsDontDestroyOnLoad();
}
// }