Files
Ipseity-Demo/Assets/Unheimlich_POC/Scripts/Core/Singleton.cs
T
2026-01-30 17:39:26 +01:00

34 lines
913 B
C#

using UnityEngine;
namespace Unheimlich.Core
{
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
public static T Instance { get; private set; }
protected virtual void Awake()
{
if (Instance != null && Instance != this)
{
Debug.LogWarning($"[{typeof(T).Name}] Multiple instances detected! Destroying duplicate on GameObject '{gameObject.name}'.");
Destroy(this);
return;
}
Instance = this as T;
OnAwake();
}
protected virtual void OnDestroy()
{
if (Instance == this)
{
Instance = null;
OnDestroyInstance();
}
}
protected virtual void OnAwake() { }
protected virtual void OnDestroyInstance() { }
}
}