chore: Project Restructuring & Theatre
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unheimlich.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Clears all Singleton static instances at the start of every Play Mode session.
|
||||
/// Necessary when Domain Reload is disabled in Enter Play Mode settings.
|
||||
/// </summary>
|
||||
internal static class SingletonRegistry
|
||||
{
|
||||
private static readonly List<Action> _resetCallbacks = new List<Action>();
|
||||
|
||||
public static void Register(Action resetCallback)
|
||||
{
|
||||
if (!_resetCallbacks.Contains(resetCallback))
|
||||
_resetCallbacks.Add(resetCallback);
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void ResetAll()
|
||||
{
|
||||
foreach (Action reset in _resetCallbacks)
|
||||
reset?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
public static T Instance { get; private set; }
|
||||
|
||||
static Singleton()
|
||||
{
|
||||
SingletonRegistry.Register(() => Instance = null);
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Debug.LogWarning($"[{typeof(T).Name}] Multiple instances detected! Destroying duplicate on '{gameObject.name}'.");
|
||||
Destroy(this);
|
||||
return;
|
||||
}
|
||||
Instance = this as T;
|
||||
OnAwake();
|
||||
}
|
||||
|
||||
// Re-registers the instance when entering Play Mode with Scene Reload disabled.
|
||||
// SubsystemRegistration clears Instance before OnEnable runs, so we restore it here.
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (Instance == null)
|
||||
Instance = this as T;
|
||||
|
||||
OnEnabled();
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
if (Instance == this)
|
||||
{
|
||||
Instance = null;
|
||||
OnDestroyInstance();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnAwake() { }
|
||||
protected virtual void OnEnabled() { }
|
||||
protected virtual void OnDestroyInstance() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 654e642c73c2b954fa86e0caac3f08c1
|
||||
Reference in New Issue
Block a user