C# unity 中实现类的单例比较好的做法

2016-1-19 雨辰 C#

继承自Singleton 即可实现一个单例的类
using UnityEngine;
using System.Collections;

namespace DMGame
{
    public class Singleton<T> where T : class, new()
    {
        protected static T _instance = default(T);

        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new T();
                }
                return _instance;
            }
        }

        public static void Reset()
        {
            _instance = null;
        }
    }

    public class SystemBasis<T> where T : class, new()
    {
        public static T Instance { get { return Singleton<T>.Instance; } }
        public virtual void Init() { }
        public virtual void Update() { }
        public virtual void LateUpdate() { }
        public virtual void Reset() { }
        public virtual void Release() { }

        public virtual void OnAppQuit() { }
        public virtual void OnAppPause(bool pause) { }
        public virtual void OnAppFocus(bool focus) { }
    }
}

标签: Unity3D C#

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号