游戏框架征服Unity3dUnity技术分享

优雅的QSignleton (二) MonoSingleton单

2017-11-07  本文已影响21人  凉鞋的笔记
namespace QFramework.Example
{
    using System.Collections;
    using UnityEngine;
    
    class Class2MonoSingleton : QMonoSingleton<Class2MonoSingleton>
    {
        public override void OnSingletonInit()
        {
            Debug.Log(this.name + ":" + "OnSingletonInit");
        }

        private void Awake()
        {
            Debug.Log(this.name + ":" + "Awake");
        }

        private void Start()
        {
            Debug.Log(this.name + ":" + "Start");
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            
            Debug.Log(this.name + ":" + "OnDestroy");
        }
    }

    public class MonoSingleton : MonoBehaviour
    {
        private IEnumerator Start()
        {
            var instance = Class2MonoSingleton.Instance;

            yield return new WaitForSeconds(3.0f);
            
            instance.Dispose();
        }
    }
}

结果:

image

三秒之后,单例GameObject消失,并且触发了OnDestroy事件。

image

相关链接:

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

微信公众号:liangxiegame

image

output/writing/Unity游戏框架搭建

上一篇 下一篇

猜你喜欢

热点阅读