Hallucination system

This commit is contained in:
Ferlat Thibaud
2026-03-16 17:06:47 +01:00
parent 10faf74405
commit 0d968f75ab
44 changed files with 2860 additions and 909 deletions
@@ -1,23 +1,62 @@
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 '{gameObject.name}'.");
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)
@@ -26,8 +65,9 @@ namespace Unheimlich.Core
OnDestroyInstance();
}
}
protected virtual void OnAwake() { }
protected virtual void OnEnabled() { }
protected virtual void OnDestroyInstance() { }
}
}