美文网首页
单例脚本基类

单例脚本基类

作者: 貪狼大人 | 来源:发表于2017-11-01 21:18 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 单例脚本基类
    /// </summary>
    public class SingletonMono<T> : MonoBehaviour
        where T : SingletonMono<T>
    {
    
        private static T instance;
    
        public static T Instance
        {
            get
            {
                if (null == instance)
                {
                    GameObject obj = new GameObject(typeof(T).Name);
                    instance = obj.AddComponent<T>();
                }
                return instance;
            }
        }
    
        protected virtual void Awake()
        {
            instance = this as T;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:单例脚本基类

          本文链接:https://www.haomeiwen.com/subject/pxqnpxtx.html