Files
Continentis/Assets/Scripts/ScriptExtensions/General/Singleton.cs

35 lines
812 B
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using UnityEngine;
2025-10-23 00:49:44 -04:00
namespace SLSFramework.General
2025-10-03 00:02:43 -04:00
{
2025-10-23 00:49:44 -04:00
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
2025-10-03 00:02:43 -04:00
{
protected static T instance;
public static T Instance => 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;
}
}
}
}