前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity3D-单例类Instance

Unity3D-单例类Instance

作者头像
孙寅
发布2020-06-02 11:42:49
1.4K0
发布2020-06-02 11:42:49
举报
文章被收录于专栏:宜达数字

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

代码语言:javascript
复制
// ========================================================
// 描述:基于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("执行单例方法");
    }
}
  • 万能单例类
代码语言:javascript
复制
// ========================================================
// 描述:万能单例类
// 作者:雷潮 
// 创建时间: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;
        }
    }
}

雨落随风提供单例类

代码语言:javascript
复制
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;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档