优雅的QSignleton (四) 通过属性器实现MonoSingleton

发表于2017-11-07
评论0 1k浏览

  大家都出去过周六了,而我却在家写代码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

output/writing/Unity游戏框架搭建

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

标签: