Unity C# 脚本的单例
今天学习了一个比较不错的单例模式
1 public class UnitySigleton <T>: MonoBehaviour where T:class 2 { 3 public static T instance; 4 5 protected virtual void Awake(){ 6 if (instance != null) { 7 DestroyImmediate (this); 8 return; 9 } else 10 instance = this as T; 11 } 12 13 protected virtual void OnDistroy(){ 14 T t = this as T; 15 if (this == t) { 16 instance = null; 17 } 18 } 19 }
但是这种单例,脚本需要手动挂在场景中!
你有什么资格不努力!——进击的皮卡丘