- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
- 纳金币
- 53488
- 精华
- 316
|
单例模版:
单例我就不说了,直接贴代码就行:
//非MonoBehaviour 的模版 就是不需要在Hierarchey中创建物体的单例- public class SigletonNotMono<T> where T :class,new()
- {
- private static T _instance;
- public static T Instance
- {
- get{
- if(_instance==null)
- _instance = new T();
- return _instance;
- }
- }
- }
- //MonoBehaviour 单例模版,在游戏创建物体
- public class SigletonMono<T> : MonoBehaviour where T:MonoBehaviour
- {
- private static T _instance;
- public static T Instance
- {
- get{
- if(_instance==null)
- {
- GameObject singleton = new GameObject();
- _instance = singleton.AddComponent<T>();
- singleton.name = "(singleton)"+typeof(T).ToString();
- DontDestroyOnLoad(singleton);
- }
- return _instance;
- }
- }
- }
复制代码 |
|