Unity单例模式的使用

发表于2017-08-23
评论0 1.28w浏览

一.单例模式的基本实现:

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

最普通的单例:(样式一)

  1. public class Singleton    
  2. {    
  3.     static Singleton instance;    
  4.       
  5.     public static Singleton Instance {    
  6.         get {    
  7.             if (instance == null) {    
  8.                 instance = new Singleton ();    
  9.             }    
  10.             return instance;    
  11.         }    
  12.     }    
  13. }    

Unity单例模式二:

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

好,我们来实现以下挂到gameobject的单例(模式二):

  1. public class UnitySingleton : MonoBehaviour    
  2. {    
  3.   static UnitySingleton instance;    
  4.       
  5.   public static UnitySingleton Instance {    
  6.   get {    
  7.    if (instance == null) {    
  8.    instance = FindObjectOfType (typeof(UnitySingleton)) as UnitySingleton;    
  9.    if (instance == null) {    
  10.    GameObject obj = new GameObject ();      
  11.    instance = obj.AddComponent (typeof(UnitySingleton));    
  12.    }    
  13.  }    
  14.  return instance;    
  15.  }    
  16.     }    
  17. }    

Unity单例模式三:

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

  1. public class UnitySingleton : MonoBehaviour    
  2.     where T : Component    
  3. {    
  4.     private static T _instance;    
  5.     public static T Instance {    
  6.         get {    
  7.             if (_instance == null) {    
  8.                 instance = FindObjectOfType (typeof(T)) as T;    
  9.                 if (_instance == null) {    
  10.                     GameObject obj = new GameObject ();    
  11.                     obj.hideFlags = HideFlags.HideAndDontSave;//隐藏实例化的new game object,下同    
  12.                     _instance = obj.AddComponent (typeof(T));    
  13.                 }    
  14.             }    
  15.             return _instance;    
  16.         }    
  17.     }    
  18. }    

这样一来,场景中需要单例化的脚本只要简单的继承这个类就可以了
  1. public class SingletonTest : MonoBehaviour {    
  2.         // Use this for initialization    
  3.         void Start () {    
  4.                 Debug.Log(Manager.Instance.testText);    
  5.         }    
  6. }    

Unity单例模式四:

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

  1. using UnityEngine;    
  2. public class UnitySingleton : MonoBehaviour     
  3.         where T:Component {    
  4.         private static T _instance;    
  5.         public static T Instance        {    
  6.                 get{    
  7.                         if(_instance == null){    
  8.                                 _instance = FindObjectOfType(typeof(T)) as T;    
  9.                                 if(_instance == null){    
  10.                                         GameObject obj = new GameObject ();    
  11.                                         //obj.hideFlags = HideFlags.DontSave;    
  12.                                         obj.hideFlags = HideFlags.HideAndDontSave;    
  13.                                         _instance =(T) obj.AddComponent(typeof(T));    
  14.                                 }    
  15.                         }    
  16.                         return _instance;    
  17.                 }    
  18.         }    
  19.         public virtual void Awake()    
  20.         {    
  21.                 DontDestroyOnLoad(this.gameObject);    
  22.                 if(_instance == null){    
  23.                         _instance = this as T;    
  24.                 }    
  25.                 else{    
  26.                         Destroy(gameObject);    
  27.                 }    
  28.         }    
  29. }    
至此,所有单例模式已经全部探讨完了,单例模式是最简单的设计模式,概念上没什么难懂的,只不过在unity中实现起来需要注意一下,



二.单例模式的高级使用

此种方式更加具有健壮性和灵活性,更加适合面向对象的基本思想“多组合,少继承”,更容易实现代码的维护和扩展。例如可以继续扩展对多个不同单例对象组件的管理

一位国外的程序猿在论坛上问:“在unity3D中,有木有什么萌萌哒方法可以创建一个类似全局类的静态实例一样的单例管理类啊!”

然后又说:“我该怎么做呢?需要把它挂在物体上吗?我能让它萌萌哒躺在文件夹里而不需要放在场景中吗”
下面就有位技术大大看到这个萌
萌哒问题,心里顿时就软了,

然后很细心的告诉他了,这要看情况了,一般我用两种单例类:
1、作为组件挂在物体上
2、作为一个单独的类并且不继承字MonoBehaviour类
下面就是这个类的示例代码:

这个类主要由两部分组成:一个是用于单例管理的静态方法CreateInstance(),一个用于挂载组件的GameObject类型Main。

  1. public class MainComponentManger {    
  2.     private static MainComponentManger instance;    
  3.     public static void CreateInstance () {    
  4.         if (instance == null) {    
  5.             instance = new MainComponentManger ();    
  6.             GameObject go = GameObject.Find ("Main");    
  7.             if (go == null) {    
  8.                 go = new GameObject ("Main");    
  9.                 instance.main = go;    
  10.                 // important: make game object persistent:    
  11.                 Object.DontDestroyOnLoad (go);    
  12.             }    
  13.             // trigger instantiation of other singletons    
  14.             Component c = MenuManager.SharedInstance;    
  15.             // ...    
  16.         }    
  17.     }    
  18.      
  19.     GameObject main;    
  20.      
  21.     public static MainComponentManger SharedInstance {    
  22.         get {    
  23.             if (instance == null) {    
  24.                 CreateInstance ();    
  25.             }    
  26.             return instance;    
  27.         }    
  28.     }    
  29.      
  30.     public static T AddMainComponent  () where T : UnityEngine.Component {    
  31.         T t = SharedInstance.main.GetComponent ();    
  32.         if (t != null) {    
  33.             return t;    
  34.         }    
  35.         return SharedInstance.main.AddComponent  ();    
  36.     }    
然后呢,这个技术大大又说这个类是这样用的,

示例代码如下:

  1. public class AudioManager : MonoBehaviour {    
  2.     private static AudioManager instance = null;    
  3.     public static AudioManager SharedInstance {    
  4.         get {    
  5.             if (instance == null) {    
  6.                 instance = MainComponentManger.AddMainComponent ();    
  7.             }    
  8.             return instance;    
  9.         }    
  10.     }    

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论