Unity單例模式的實踐

為什么使用單例模式

??單例模式的效果是系統(tǒng)中對應(yīng)類只有一個實例對象,根據(jù)不同的場景原因各有不同,有的是為了避免多次創(chuàng)建帶來的資源開銷,有的是為了方便數(shù)據(jù)的統(tǒng)一處理和保存等等。

經(jīng)驗中可使用單例的情景

  • 游戲數(shù)據(jù)管理器:管理游戲數(shù)據(jù)如最新的關(guān)卡,上次進行關(guān)卡的數(shù)據(jù)等。
  • 聲音管理器:播放背景音樂或者點擊音效。
  • 內(nèi)購管理:管理內(nèi)購。
  • 廣告管理器:管理廣告的顯示和隱藏,統(tǒng)一管理廣告的各種回調(diào)如廣告的關(guān)閉。
  • 更多

實踐

??當(dāng)單例無需使用MonoBehaviour的特性如開啟協(xié)程的時候,可以直接使用C#的單例。

C#單例(餓漢)

using System;
public class Singleton{
  private object _lock = new object();
  private Singleton _instance = null;
  
  public static Instance{
    get{
      if(_instance==null){
            lock(_lock){
                 if(_instance==null){
                       
                        _instance = new Singleton();
                    
                  }
  
            }
        }

      return _instance;
    }

  }

}

MonoBehaviour單例

?? 我在網(wǎng)上找到一個MonoBehaviour單例的寫法,如下所示。

using UnityEngine;
public abstract class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T _instance;

        private static object _lock = new object();

        public static T Instance
        {
            get
            {
                if (applicationIsQuitting)
                {
                    Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
                                     "' already destroyed on application quit." +
                                     " Won't create again - returning null.");
                    return null;
                }

                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = (T) FindObjectOfType(typeof(T));

                        if (FindObjectsOfType(typeof(T)).Length > 1)
                        {
                            Debug.LogError("[Singleton] Something went really wrong " +
                                           " - there should never be more than 1 singleton!" +
                                           " Reopening the scene might fix it.");
                            return _instance;
                        }

                        if (_instance == null)
                        {
                            GameObject singletonPrefab = null;
                            GameObject singleton = null;

                            // Check if exists a singleton prefab on Resources Folder.
                            // -- Prefab must have the same name as the Singleton SubClass
                            singletonPrefab =
                                (GameObject) Resources.Load("Prefabs/" + typeof(T).ToString(), typeof(GameObject));

                            // Create singleton as new or from prefab
                            if (singletonPrefab != null)
                            {
                                singleton = Instantiate(singletonPrefab);
                                _instance = singleton.GetComponent<T>();
                            }
                            else
                            {
                                singleton = new GameObject();
                                _instance = singleton.AddComponent<T>();
                            }

                            singleton.name = "(singleton) " + typeof(T).ToString();

                            DontDestroyOnLoad(singleton);

                            Debug.Log("[Singleton] An instance of " + typeof(T) +
                                      " is needed in the scene, so '" + singleton +
                                      "' was created with DontDestroyOnLoad.");
                        }
                        else
                        {
                            Debug.Log("[Singleton] Using instance already created: " +
                                      _instance.gameObject.name);
                        }
                    }

                    return _instance;
                }
            }
        }


        private static bool applicationIsQuitting = false;

        /// <summary>
        /// When Unity quits, it destroys objects in a random order.
        /// In principle, a Singleton is only destroyed when application quits.
        /// If any script calls Instance after it have been destroyed, 
        ///   it will create a buggy ghost object that will stay on the Editor scene
        ///   even after stopping playing the Application. Really bad!
        /// So, this was made to be sure we're not creating that buggy ghost object.
        /// </summary>
        public void OnDestroy()
        {
            applicationIsQuitting = true;
        }


        void Awake()
        {
            if (_instance != null && _instance != this)
            {
                Destroy(this.gameObject);
            }
            else
            {
                Init();
            }
            DontDestroyOnLoad(this.gameObject);
        }

        protected virtual void Init()
        {
        }
    }

其中下列語句是重要,可要到Resource目錄下加載與類名相對應(yīng)的Prefab預(yù)制體

singletonPrefab =(GameObject)Resources.Load(typeof(T).ToString(),typeof(GameObject)); 

Init()方法在生命周期函數(shù)Awake()中調(diào)用,可重寫該函數(shù)擴展操作。

 protected virtual void Init()
  {
  }

我的實踐

SoundManager C#腳本

??對于聲音管理器,我定義了下面顯示的SoundManager用于聲音的管理。SoundManager繼承了上述的SingletonMono類。


using System.Collections;
using System.Collections.Generic;
using Model;
using UnityEngine;
using zbUtil;

public class SoundManager : SingletonMono<SoundManager>
{
    public AudioClip[] AudioClips;
    private AudioSource _audioSource;

    public enum SoundType
    {
        Click = 0,
        CorrectLetter,
        Coin,
        Win,
        Move,
    }

    protected override void Init()
    {
        base.Init();
        _audioSource = GetComponent<AudioSource>();
    }

    private bool _soundOn
    {
        get { return GameDataManager.getInstance().IsSoundOn(); }
    }

    public void Play(SoundType type)
    {
        if (!_soundOn)
        {
            return;
        }
        _audioSource.PlayOneShot(AudioClips[(int) type]);
    }
}

SoundManager的預(yù)制體

  • 存儲目錄
  • Inspector

使用

SoundManager.Instance.Play(SoundManager.SoundType.Click);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容