Unity基础(25)-单例类Instance

今天介绍Unity中所有使用的单例类

// ========================================================
// 描述:基于Unity的单例类
// 作者:雷潮 
// 创建时间:2019-01-23 17:46:06
// 版 本:1.0
//========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyTest : MonoBehaviour {
   private static MyTest _Instance;
    public static MyTest Instance
    {
        get {
            if (_Instance == null)
            {
                GameObject obj = new GameObject("Test");
                _Instance = obj.AddComponent<MyTest>();
                DontDestroyOnLoad(obj);
            }
            return _Instance;       
        }
    }

    public void TestLog()
    {
        Debug.Log("执行单例方法");
    }
}
  • 万能单例类
// ========================================================
// 描述:万能单例类
// 作者:雷潮 
// 创建时间:2019-01-23 18:12:43
// 版 本:1.0
//========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Singleton<T>  where T : class,new()
{
    private static T _instance;
    // 加锁
    private static object _lock = new object();

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

雨落随风提供单例类

using System;
using UnityEngine;

[AutoSingleton(true)]
public class MonoSingleton<T> : MonoBehaviour where T : Component
{
    private static T _instance;

    private static bool _destroyed;

    public static T instance
    {
        get
        {
            return MonoSingleton<T>.GetInstance();
        }
    }

    public static T GetInstance()
    {
        if (MonoSingleton<T>._instance == null && !MonoSingleton<T>._destroyed)
        {
            Type typeFromHandle = typeof(T);
            MonoSingleton<T>._instance = (T)((object)Object.FindObjectOfType(typeFromHandle));
            if (MonoSingleton<T>._instance == null)
            {
                object[] customAttributes = typeFromHandle.GetCustomAttributes(typeof(AutoSingletonAttribute), true);
                if (customAttributes.Length > 0 && !((AutoSingletonAttribute)customAttributes[0]).bAutoCreate)
                {
                    return (T)((object)null);
                }
                GameObject gameObject = new GameObject(typeof(T).get_Name());
                MonoSingleton<T>._instance = gameObject.AddComponent<T>();
                GameObject gameObject2 = GameObject.Find("BootObj");
                if (gameObject2 != null)
                {
                    gameObject.transform.SetParent(gameObject2.transform);
                }
            }
        }
        return MonoSingleton<T>._instance;
    }

    public static void DestroyInstance()
    {
        if (MonoSingleton<T>._instance != null)
        {
            Object.Destroy(MonoSingleton<T>._instance.gameObject);
        }
        MonoSingleton<T>._destroyed = true;
        MonoSingleton<T>._instance = (T)((object)null);
    }

    public static void ClearDestroy()
    {
        MonoSingleton<T>.DestroyInstance();
        MonoSingleton<T>._destroyed = false;
    }

    protected virtual void Awake()
    {
        if (MonoSingleton<T>._instance != null && MonoSingleton<T>._instance.gameObject != base.gameObject)
        {
            if (Application.isPlaying)
            {
                Object.Destroy(base.gameObject);
            }
            else
            {
                Object.DestroyImmediate(base.gameObject);
            }
        }
        else if (MonoSingleton<T>._instance == null)
        {
            MonoSingleton<T>._instance = base.GetComponent<T>();
        }
        Object.DontDestroyOnLoad(base.gameObject);
        this.Init();
    }

    protected virtual void OnDestroy()
    {
        if (MonoSingleton<T>._instance != null && MonoSingleton<T>._instance.gameObject == base.gameObject)
        {
            MonoSingleton<T>._instance = (T)((object)null);
        }
    }

    public static bool HasInstance()
    {
        return MonoSingleton<T>._instance != null;
    }

    protected virtual void Init()
    {
    }
}
public class AutoSingletonAttribute : Attribute
{
    public bool bAutoCreate;

    public AutoSingletonAttribute(bool bCreate)
    {
        this.bAutoCreate = bCreate;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 很多开发者或者有经验的老手都会建议尽量不要用单例模式,这是有原因的。 单例模式是设计模式中最简单的也是大家通...
    凉鞋的笔记阅读 6,070评论 3 14
  • “啊~好无聊啊,最近也没个案子什么的。”我边申懒腰边说。“是啊,好无聊啊”在一边看报纸的崔华生也说。 作者:“唉唉...
    清源_745a阅读 3,255评论 2 3
  • 这是我简书的第一篇文章,来写写参加BM高效自我管理训练营的感受和见闻吧~ 从2015年底到现在,BM给我的感觉一直...
    时尚的小妖精阅读 3,490评论 9 3
  • 上周随意翻了翻这本下载许久的《天年》,便一发不可收,到处抽空、耗时5个小时读完了这本科幻小说。 整篇小说科幻色彩十...
    趙小陽阅读 3,550评论 0 0
  • 漫步山外青山处,落日黄昏映山红。 形单影只念从前,怨恨当时太轻狂。 不知世道太险恶,犯下糊涂难挽回。 青葱岁月已过...
    楼满风_c524阅读 1,690评论 0 2