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

36 lines
883 B
C#
Raw Normal View History

2026-01-03 18:19:39 -05:00
using Sirenix.OdinInspector;
2025-11-25 08:19:33 -05:00
using UnityEngine;
2026-02-13 09:22:11 -05:00
namespace SLSUtilities.General
2025-11-25 08:19:33 -05:00
{
2026-01-03 18:19:39 -05:00
public class Singleton<T> : SerializedMonoBehaviour where T : MonoBehaviour
2025-11-25 08:19:33 -05:00
{
protected static T instance;
2026-02-13 09:22:11 -05:00
public static T Instance => instance == null ? FindFirstObjectByType<T>() : instance;
2025-11-25 08:19:33 -05:00
protected virtual void Awake()
{
Initialize(false);
}
2026-02-13 09:22:11 -05:00
2025-11-25 08:19:33 -05:00
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;
}
}
}
}