风动破

导航

unity 单例模式深入探讨[转]

转--http://www.unitymanual.com/archiver/tid-16916.html?page=1

单例模式(singleton pattern)大家都不陌生,我今天主要是和大家探讨一下单例模式在unity中的实现,比起一般的单例,unity中有些他的特点。
最普通的单例:(样式一)

public class Singleton
{
    static Singleton instance;

    public static Singleton Instance {
      get {
            if (instance == null) {
                instance = new Singleton ();
            }
            return instance;
      }
    }
}

unity单例模式二:
但是unity的所有脚本都必须挂在一个gameobject上,否则无法执行,你这个单例中若只是一些数据,那倒没关系,但我相信绝大多数单例模式都不会只包含数据,若只要实现包含数据的功能,用全局静态变量就行了,说到这里加一句,有些盆友喜欢用单例脚本当做全局脚本来用,那其实是违背单例模式的初衷的...
好,我们来实现以下挂到gameobject的单例(模式二):

public class UnitySingleton : MonoBehaviour
{
static UnitySingleton instance;

public static UnitySingleton Instance {
get {
   if (instance == null) {
   instance = FindObjectOfType (typeof(UnitySingleton)) as UnitySingleton;
   if (instance == null) {
   GameObject obj = new GameObject ();
   instance = obj.AddComponent (typeof(UnitySingleton));
   }
}
return instance;
}
    }
}

unity单例模式三:
那如果我的游戏里有很多单例脚本,每个脚本都这么写岂不是很麻烦?岂不是很违背oo思想?^_^,那我们来设计一个泛型类:

public class UnitySingleton<T> : MonoBehaviour
    where T : Component
{
    private static T _instance;
    public static T Instance {
      get {
            if (_instance == null) {
                instance = FindObjectOfType (typeof(T)) as T;
                if (_instance == null) {
                  GameObject obj = new GameObject ();
                  obj.hideFlags = HideFlags.HideAndDontSave;//隐藏实例化的new game object,下同
                  _instance = obj.AddComponent (typeof(T));
                }
            }
            return _instance;
      }
    }
}


这样一来,场景中需要单例化的脚本只要简单的继承这个类就可以了
public class Manager : UnitySingleton<Manager>
{
public string testText = "hello Sinngleton!";
}

manager单例脚本将在第一次实例化时自动创建,当然,你是看不到他的呵呵,但是底部的debuglog出卖了他...:

public class SingletonTest : MonoBehaviour {
      // Use this for initialization
      void Start () {
                Debug.Log(Manager.Instance.testText);
      }
}


unity单例模式四:
最后一个问题就是,我希望我的所有单例脚本在场景变化后依然存在,那自然是用DontDestroyOnLoad,直接上脚本:

using UnityEngine;
public class UnitySingleton<T> : MonoBehaviour 
      where T:Component {
      private static T _instance;
      public static T Instance      {
                get{
                        if(_instance == null){
                              _instance = FindObjectOfType(typeof(T)) as T;
                              if(_instance == null){
                                        GameObject obj = new GameObject ();
                                        //obj.hideFlags = HideFlags.DontSave;
                                        obj.hideFlags = HideFlags.HideAndDontSave;
                                        _instance =(T) obj.AddComponent(typeof(T));
                              }
                        }
                        return _instance;
                }
      }
      public virtual void Awake()
      {
                DontDestroyOnLoad(this.gameObject);
                if(_instance == null){
                        _instance = this as T;
                }
                else{
                        Destroy(gameObject);
                }
      }
}

posted on 2015-08-25 14:43  风动破  阅读(289)  评论(0)    收藏  举报