<转载>
unity类似全局类的静态实例一样的单例管理类
1 public class Singleton<T>:MonoBehaviour
2 where T:component
3 {
4 private static T Instance;
5 public static T Instance
6 {
7 get
8 {
9 if(Instance == null)
10 {
11 Instance = FindObjectType(typeof(T)) as T;
12 if(Instance == null)
13 {
14 GameObject obj = new GameObject();
15 DontDestroyOnload(obj);
16 obj.hideFlags = HideFlags.HideInHierarchy;
17 Instance = obj.getcomponent<>();
18 }
19 }
20 return Instance;
21 }
22
23 }
24 }
调用方法:
Singleton<MonoBehaviour>.Instance.StartCoroutine(routine);