-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.cs
More file actions
49 lines (46 loc) · 1.12 KB
/
Copy pathSingleton.cs
File metadata and controls
49 lines (46 loc) · 1.12 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
using UnityEngine;
namespace VTLTools
{
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
[SerializeField]
private bool IsDontDestroyOnLoad;
protected static bool INeedDontDestroy;
protected static T instance;
private static object _lock = new object();
public static T Instance
{
get
{
lock (_lock)
{
return instance;
}
}
}
protected virtual void Awake()
{
INeedDontDestroy = IsDontDestroyOnLoad;
if (instance == null)
{
instance = this as T;
if (INeedDontDestroy)
{
DontDestroyOnLoad(this);
}
}
else
{
if (instance != null)
{
Destroy(gameObject);
}
}
}
protected virtual void OnDestroy()
{
if (instance == this)
instance = null;
}
}
}