Files
ichni_Official/Assets/Scripts/SLSUtilities/General/Singleton.cs

36 lines
883 B
C#
Raw Normal View History

2026-03-14 03:13:10 -04:00
using Sirenix.OdinInspector;
using UnityEngine;
namespace SLSUtilities.General
{
public class Singleton<T> : SerializedMonoBehaviour where T : MonoBehaviour
{
protected static T instance;
public static T Instance => instance == null ? FindFirstObjectByType<T>() : instance;
protected virtual void Awake()
{
Initialize(false);
}
protected virtual void Initialize(bool dontDestroy)
{
if (dontDestroy)
{
if (instance == null)
{
instance = this as T;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
else
{
instance = this as T;
}
}
}
}