Unity 游戏框架搭建 2017 (十四) 优雅的 QSingleton (四) 属性器实现 Mono 单例

大家都出去过周六了,而我却在家写代码T.T... 接下来介绍通过属性器实现MonoSingleton。 代码如下:

  • MonoSingletonProperty.cs

    namespace QFramework.Example
    {
      using System.Collections;
      using UnityEngine;
    
      class Class2MonoSingletonProperty : MonoBehaviour,ISingleton
      {
          public static Class2MonoSingletonProperty Instance
          {
              get { return QMonoSingletonProperty<Class2MonoSingletonProperty>.Instance; }
          }
    
          public void Dispose()
          {
              QMonoSingletonProperty<Class2MonoSingletonProperty>.Dispose();
          }
    
          public void OnSingletonInit()
          {
              Debug.Log(name + ":" + "OnSingletonInit");
          }
    
          private void Awake()
          {
              Debug.Log(name + ":" + "Awake");
          }
    
          private void Start()
          {
              Debug.Log(name + ":" + "Start");
          }
    
          protected void OnDestroy()
          {
              Debug.Log(name + ":" + "OnDestroy");
          }
      }
    
      public class MonoSingletonProperty : MonoBehaviour
      {
          private IEnumerator Start()
          {
              var instance = Class2MonoSingletonProperty.Instance;
    
              yield return new WaitForSeconds(3.0f);
    
              instance.Dispose();
          }
      }
    }

结果:


三秒之后,同样触发了OnDestroy事件

转载请注明地址:凉鞋的笔记:liangxiegame.com

更多内容

原文地址:https://www.cnblogs.com/liangxiegame/p/you-ya-deQSignleton-si-tong-guo-shu-xing-qi-shi-xi.html