Unity 单例

1. 继承于MonoBehaviour(不随着场景切换而销毁)

  基类代码:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 /// <summary>
 6 /// 单例模式基类,继承于 MonoBehaviour,不随场景切换而销毁
 7 /// 在场景中新建空物体 DDOL,挂载 T 类继承 DDOLSingleton<T> 即可
 8 /// </summary>
 9 /// <typeparam name="T"></typeparam>
10 public abstract class DDOLSingleton<T> : MonoBehaviour where T : DDOLSingleton<T>
11 {
12     protected static T _instance = null;
13 
14     public static T Instance
15     {
16         get
17         {
18             if (null == _instance)
19             {
20                 GameObject go = GameObject.Find("DDOL");
21                 if (null == go)
22                 {
23                     go = new GameObject("DDOL");
24                     DontDestroyOnLoad(go);
25                 }
26                 _instance = go.GetComponent<T>();
27                 if (null == _instance)
28                 {
29                     _instance = go.AddComponent<T>();
30                 }
31             }
32             return _instance;
33         }
34     }
35 }

  测试:

  新建空实体 DDOL,挂载脚本 GameManage。

  新建一个按钮,实现加载下一场景功能,测试是否随场景销毁而销毁。

  Hierarchy :

  GameManage 代码:

 1 using System.Collections.Generic;
 2 using UnityEngine;
 3 
 4 /// <summary>
 5 /// 单例
 6 /// </summary>
 7 public class GameManage : DDOLSingleton<GameManage> {
 8     public void TestMethod()
 9     {
10         Debug.Log("GameManage");
11     }
12 }

 

  测试代码: 

 1 /// <summary>
 2 /// 测试单例
 3 /// </summary>
 4 public class Test : MonoBehaviour {
 5 
 6     // Use this for initialization
 7     void Start () {
 8         GameManage.Instance.TestMethod();
 9     }
10     
11     // Update is called once per frame
12     void Update () {
13         
14     }
15 
16     public void OnBtnClick()
17     {
18         SceneManager.LoadScene(1);
19     }
20 }

  效果图:

 

 

2. 不继承于MonoBehaviour(随着场景切换而销毁)

  基类代码:

 1 /// <summary>
 2 /// 单例模式基类,不继承于 MonoBehaviour,随场景切换而销毁
 3 /// 挂载 T 类继承 DDOLSingleton<T>,重载 Init 函数即可
 4 /// </summary>
 5 /// <typeparam name="T"></typeparam>
 6 public abstract class Singleton<T> where T : class, new()
 7 {
 8     protected static T _instance = null;
 9 
10     public static T Instance
11     {
12         get
13         {
14             if (null == _instance)
15             {
16                 _instance = new T();
17             }
18             return _instance;
19         }
20     }
21 
22     protected Singleton()
23     {
24         if (null != _instance)
25         {
26             Debug.LogError("This " + typeof(T).ToString() + " Singleton Instance is not null !!!");
27         }
28         Init();
29     }
30 
31     public virtual void Init()
32     {
33 
34     }
35 }

 

 

  此时 GameManage 代码修改为:

 1 /// <summary>
 2 /// 测试 Singleton
 3 /// </summary>
 4 public class GameManage : Singleton<GameManage> {
 5     public override void Init()
 6     {
 7         base.Init();
 8     }
 9 
10     public void TestMethod()
11     {
12         Debug.Log("GameManage");
13     }
14 }

 

posted @ 2018-04-02 15:36  Just_for_Myself  阅读(563)  评论(1编辑  收藏  举报