Files
2026-02-21 16:45:37 +08:00

48 lines
917 B
C#

using UnityEngine;
namespace UltimateWater.Internal
{
public class ApplicationSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _Instance;
private static bool _Quiting;
public static T Instance
{
get
{
if (_Instance != null)
{
return _Instance;
}
_Instance = (T)Object.FindObjectOfType(typeof(T));
if (_Instance != null)
{
return _Instance;
}
if (_Quiting)
{
return (T)null;
}
GameObject gameObject = new GameObject("[" + typeof(T).Name + "] - instance");
gameObject.hideFlags = HideFlags.HideInHierarchy;
GameObject gameObject2 = gameObject;
Object.DontDestroyOnLoad(gameObject2);
_Instance = gameObject2.AddComponent<T>();
return _Instance;
}
}
protected virtual void OnApplicationQuit()
{
_Quiting = true;
}
protected virtual void OnDestroy()
{
_Quiting = true;
}
}
}