游戏框架征服Unity3dUnity技术分享

优雅的QSignleton (三) 通过属性器实现Singlet

2017-11-07  本文已影响21人  凉鞋的笔记

接下来介绍,不通过继承的方式实现单例模式。大家都出去嗨了,而我却在家码代码...

代码如下:

namespace QFramework.Example
{
    using UnityEngine;

    class Class2SignetonProperty : ISingleton
    {
        public static Class2SignetonProperty Instance
        {
            get { return QSingletonProperty<Class2SignetonProperty>.Instance; }
        }

        private Class2SignetonProperty() {}
        
        private static int mIndex = 0;

        public void OnSingletonInit()
        {
            mIndex++;
        }

        public void Dispose()
        {
            QSingletonProperty<Class2SignetonProperty>.Dispose();
        }
        
        public void Log(string content)
        {
            Debug.Log("Class2SingletonProperty" + mIndex + ":" + content);
        }
    }
        
    public class SingletonProperty : MonoBehaviour
    {
        // Use this for initialization
        void Start () 
        {
            Class2SignetonProperty.Instance.Log("Hello World!");    
            
            // delete current instance
            Class2SignetonProperty.Instance.Dispose();
            
            // new instance
            Class2SignetonProperty.Instance.Log("Hello World!");
        }
    }
}

结果:

image

相关链接

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

微信公众号:liangxiegame

image

output/writing/Unity游戏框架搭建

上一篇 下一篇

猜你喜欢

热点阅读