34 lines
913 B
C#
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() { }
|
|
}
|
|
}
|