今天介绍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("执行单例方法");
}
}
- 万能单例类
// ========================================================
// 描述:万能单例类
// 作者:雷潮
// 创建时间: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;
}
}