U3D点滴-单例模式

例如:对于一个音乐管理类

using UnityEngine;
 
public class MusicManager : MonoBehaviour
{
     public void Play()
     {
         //Play some audio!
     }
}

现在要调用来播放音乐,如下代码

Object.FindObjectOfType < MusicManager > ().Play();

以上的代码不仅难懂而且不安全,比较的好方式如下代码:

var musicManager = Object.FindObjectOfType < MusicManager >();

if ( musicManager != null )

{

musicManager.Play ();

}

以上的代码运行没有问题,但是每次调用的话会比较麻烦。


现在就用单例模式来创建代码:


public class MusicManager : MonoBehaviour
{
     //We make a static variable to our MusicManager instance
     public static MusicManager instance { get ; private set ; }
 
     //When the object awakens, we assign the static variable
     void Awake()
     {
         instance = this ;
     }
 
     public void Play()
     {
         //Play some audio!
     }
}
 
//...
//Now in another class, we can call Play() by using the static variable!
public class LevelController : MonoBehaviour
{
     void PlayMusic()
     {
         MusicManager.instance.Play();
}
}
这样的方式叫做lazy Singleton(偷懒的单例模式吗?!)
这样使用要注意一个地方就是调用的时间,就是如果要使用不能在awake()方法里面,以保证这个实例不为null
比如,Start()方法就可以。

以下是标准的单例模式
public class MusicManager : MonoBehaviour
{
     //Here is a private reference only this class can access
     private static MusicManager _instance;
 
     //This is the public reference that other classes will use
     public static MusicManager instance
     {
         get
         {
             //If _instance hasn't been set yet, we grab it from the scene!
             //This will only happen the first time this reference is used.
             if (_instance == null )
                 _instance = GameObject.FindObjectOfType<MusicManager>();
             return _instance;
         }
     }
 
     public void Play()
     {
         //Play some audio!
     }
}


持续的单例
有时候你可能要在不同的场景里面使用单例
public class MusicManager : MonoBehaviour
{
     private static MusicManager _instance;
 
     public static MusicManager instance
     {
         get
         {
             if (_instance == null )
             {
                 _instance = GameObject.FindObjectOfType<MusicManager>();
 
                 //Tell unity not to destroy this object when loading a new scene!
                 DontDestroyOnLoad(_instance.gameObject);
             }
 
             return _instance;
         }
     }
 
     void Awake()
     {
         if (_instance == null )
         {
             //If I am the first instance, make me the Singleton
             _instance = this ;
             DontDestroyOnLoad( this );
         }
         else
         {
             //If a Singleton already exists and you find
             //another reference in scene, destroy it!
             if ( this != _instance)
                 Destroy( this .gameObject);
         }
     }
 
     public void Play()
     {
         //Play some audio!
     }
}


DontDestroyOnLoad() will maintain all scripts that are placed on the same GameObject as your singleton.

 For this reason it’s usually a good idea to put a singleton on it’s own GameObject alone.

Other Notes:

  • DontDestroyOnLoad() only needs to be used on objects inheriting MonoBehaviour. A static reference in a normal class will maintain its data across scenes.
  • In Unity, if you have a reference to a GameObject or MonoBehaviour that has been destroyed, it will equal null. The singleton examples above use this to their advantage.
以上是e文,自己体会吧


原文引用:http://unitypatterns.com/singletons/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值