Unity单例模式,但是是取自Ultrakill反编译代码
以下是用dnSpy对Ultrakill源码反编译后拿到的MonoSingleton写法,删去了一部分应该是需要关联其他东西的代码。
using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.SceneManagement;
public abstract class MonoSingleton<T> : MonoSingleton where T : MonoSingleton<T>
{
private static T Initialize()
{
if (!MonoSingleton<T>.flags.HasFlag(SingletonFlags.NoAutoInstance))
{
GameObject gameObject = new GameObject(typeof(T).FullName);
T result = gameObject.AddComponent<T>();
if (MonoSingleton<T>.flags.HasFlag(SingletonFlags.HideAutoInstance))
{
gameObject.hideFlags = HideFlags.HideAndDontSave;
}
if (MonoSingleton<T>.flags.HasFlag(SingletonFlags.PersistAutoInstance))
{
Object.DontDestroyOnLoad(gameObject);
}
return result;
}
if (!SceneManager.GetActiveScene().isLoaded)
{
return Object.FindObjectOfType<T>();
}
return default(T);
}
public static T Instance
{
get
{
if (!MonoSingleton<T>.instance)
{
return MonoSingleton<T>.instance = MonoSingleton<T>.Initialize();
}
return MonoSingleton<T>.instance;
}
protected set
{
MonoSingleton<T>.instance = value;
}
}
protected virtual void Awake()
{
if (MonoSingleton<T>.instance && MonoSingleton<T>.flags.HasFlag(SingletonFlags.DestroyDuplicates) && MonoSingleton<T>.instance != this)
{
Object.Destroy(this);
return;
}
if (MonoSingleton<T>.flags.HasFlag(SingletonFlags.NoAwakeInstance))
{
return;
}
if (MonoSingleton<T>.instance && MonoSingleton<T>.instance.isActiveAndEnabled && !base.isActiveAndEnabled)
{
return;
}
MonoSingleton<T>.Instance = (T)((object)this);
}
protected virtual void OnEnable()
{
MonoSingleton<T>.Instance = (T)((object)this);
}
protected virtual void OnDestroy()
{
}
private static T instance;
private static readonly SingletonFlags flags;
}
(感觉是Pitr或者其他程序员写的,反正不是Hakita写的就是了)