Fix: cleanup and minor perf improvements
This commit is contained in:
@@ -8,6 +8,21 @@ namespace Unheimlich.Core
|
||||
/// </summary>
|
||||
public static class PlayerEvents
|
||||
{
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void ResetAll()
|
||||
{
|
||||
OnJump = null;
|
||||
OnTeleport = null;
|
||||
OnCameraShake = null;
|
||||
OnDiapasonEquip = null;
|
||||
OnDiapasonUnequip = null;
|
||||
OnDiapasonTap = null;
|
||||
OnEnterDialogue = null;
|
||||
OnExitDialogue = null;
|
||||
OnEnterListening = null;
|
||||
OnExitListening = null;
|
||||
}
|
||||
|
||||
// Movement events
|
||||
public static event System.Action OnJump;
|
||||
public static event System.Action<Vector3, Quaternion> OnTeleport;
|
||||
|
||||
@@ -14,10 +14,11 @@ namespace Unheimlich.Core.Input
|
||||
{
|
||||
public enum InputContext
|
||||
{
|
||||
Normal, // All inputs enabled
|
||||
Dialogue, // Only look and interact
|
||||
Listening, // Only look and diapason
|
||||
Inspection // Only look and interact (for item inspection)
|
||||
Normal, // All inputs enabled
|
||||
Dialogue, // Only look and interact
|
||||
Listening, // Only look and diapason
|
||||
Inspection, // Only look and interact (for item inspection)
|
||||
DetectiveBoard // Mouse cursor free, movement disabled
|
||||
}
|
||||
|
||||
[Header("Current State")]
|
||||
@@ -30,6 +31,14 @@ namespace Unheimlich.Core.Input
|
||||
public static Vector2 MoveInput { get; private set; }
|
||||
public static Vector2 LookInput { get; private set; }
|
||||
|
||||
// Cached InputAction references — populated once during initialization
|
||||
private static UnityEngine.InputSystem.InputAction _cachedMoveAction;
|
||||
private static UnityEngine.InputSystem.InputAction _cachedLookAction;
|
||||
private static UnityEngine.InputSystem.InputAction _cachedInteractAction;
|
||||
private static UnityEngine.InputSystem.InputAction _cachedDiapasonAction;
|
||||
private static UnityEngine.InputSystem.InputAction _cachedFireAction;
|
||||
private static UnityEngine.InputSystem.InputAction _cachedExitAction;
|
||||
|
||||
// Legacy compatibility
|
||||
public static bool IsInputEnabled => CurrentContext == InputContext.Normal;
|
||||
public static bool IsLookingEnabled => CurrentContext != InputContext.Dialogue;
|
||||
@@ -38,6 +47,7 @@ namespace Unheimlich.Core.Input
|
||||
{
|
||||
// Try to subscribe, will retry in Start if needed
|
||||
SubscribeAllInputs();
|
||||
CacheAllActions();
|
||||
SetContext(InputContext.Normal);
|
||||
}
|
||||
|
||||
@@ -56,8 +66,9 @@ namespace Unheimlich.Core.Input
|
||||
}
|
||||
|
||||
|
||||
// Ensure we're subscribed
|
||||
// Ensure we're subscribed and actions are cached
|
||||
SubscribeAllInputs();
|
||||
CacheAllActions();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
@@ -79,6 +90,38 @@ namespace Unheimlich.Core.Input
|
||||
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Exit, OnExit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Caches all InputAction references from OctoCore once at startup to avoid
|
||||
/// per-frame iteration over the Actions list.
|
||||
/// </summary>
|
||||
private void CacheAllActions()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return;
|
||||
|
||||
_cachedMoveAction = null;
|
||||
_cachedLookAction = null;
|
||||
_cachedInteractAction = null;
|
||||
_cachedDiapasonAction = null;
|
||||
_cachedFireAction = null;
|
||||
_cachedExitAction = null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Reference == null) continue;
|
||||
|
||||
switch (actionInfo.Name)
|
||||
{
|
||||
case "Move": _cachedMoveAction = actionInfo.Reference.action; break;
|
||||
case "Look": _cachedLookAction = actionInfo.Reference.action; break;
|
||||
case "Interact": _cachedInteractAction = actionInfo.Reference.action; break;
|
||||
case "Diapason": _cachedDiapasonAction = actionInfo.Reference.action; break;
|
||||
case "Fire": _cachedFireAction = actionInfo.Reference.action; break;
|
||||
case "Exit": _cachedExitAction = actionInfo.Reference.action; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UnsubscribeAllInputs()
|
||||
{
|
||||
if (OctoCore.OctoCore.Instance == null) return;
|
||||
@@ -126,6 +169,12 @@ namespace Unheimlich.Core.Input
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
break;
|
||||
|
||||
case InputContext.DetectiveBoard:
|
||||
// Mouse cursor free for board interaction, movement disabled
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,83 +229,12 @@ namespace Unheimlich.Core.Input
|
||||
public static Vector2 GetMoveInput() => MoveInput;
|
||||
public static Vector2 GetLookInput() => LookInput;
|
||||
|
||||
// Action accessors for direct button checks
|
||||
public static UnityEngine.InputSystem.InputAction GetMoveAction()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Name == "Move" && actionInfo.Reference != null)
|
||||
return actionInfo.Reference.action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UnityEngine.InputSystem.InputAction GetDiapasonAction()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Name == "Diapason" && actionInfo.Reference != null)
|
||||
return actionInfo.Reference.action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UnityEngine.InputSystem.InputAction GetInteractAction()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Name == "Interact" && actionInfo.Reference != null)
|
||||
return actionInfo.Reference.action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UnityEngine.InputSystem.InputAction GetFireAction()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Name == "Fire" && actionInfo.Reference != null)
|
||||
return actionInfo.Reference.action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UnityEngine.InputSystem.InputAction GetExitAction()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Name == "Exit" && actionInfo.Reference != null)
|
||||
return actionInfo.Reference.action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UnityEngine.InputSystem.InputAction GetLookAction()
|
||||
{
|
||||
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
||||
if (asset == null) return null;
|
||||
|
||||
foreach (var actionInfo in asset)
|
||||
{
|
||||
if (actionInfo.Name == "Look" && actionInfo.Reference != null)
|
||||
return actionInfo.Reference.action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// Action accessors — return pre-cached references, no per-frame allocation
|
||||
public static UnityEngine.InputSystem.InputAction GetMoveAction() => _cachedMoveAction;
|
||||
public static UnityEngine.InputSystem.InputAction GetLookAction() => _cachedLookAction;
|
||||
public static UnityEngine.InputSystem.InputAction GetInteractAction() => _cachedInteractAction;
|
||||
public static UnityEngine.InputSystem.InputAction GetDiapasonAction() => _cachedDiapasonAction;
|
||||
public static UnityEngine.InputSystem.InputAction GetFireAction() => _cachedFireAction;
|
||||
public static UnityEngine.InputSystem.InputAction GetExitAction() => _cachedExitAction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,9 @@ namespace Unheimlich.Core.Interaction
|
||||
|
||||
private void ResetSessionState()
|
||||
{
|
||||
// With Domain Reload disabled, static fields and per-instance flags persist between
|
||||
// Play Mode sessions. This method is called from OnEnable() to reset all transient
|
||||
// animation state so merged objects start fresh each session without needing a full reload.
|
||||
if (_isActive)
|
||||
{
|
||||
foreach (var word in _spawnedWords)
|
||||
@@ -107,17 +110,6 @@ namespace Unheimlich.Core.Interaction
|
||||
_hasCompletedMergeOnce = false; // reset so MergeOnce objects start fresh each session
|
||||
_currentConvergenceRadius = 0f;
|
||||
}
|
||||
|
||||
// Resets _hasCompletedMergeOnce at the very start of each Play Mode session so
|
||||
// merged objects start fresh without requiring a full Domain Reload.
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void OnPlayModeReset()
|
||||
{
|
||||
// Finding all instances isn't possible from a static context with SubsystemRegistration.
|
||||
// The reset is handled per-instance in OnEnable via _hasCompletedMergeOnce being
|
||||
// re-initialized when the scene reloads. For Enter Play Mode without scene reload,
|
||||
// ResetSessionState() is called from OnEnable and now correctly clears the flag.
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
@@ -21,6 +21,6 @@ namespace Unheimlich.Core.Utils
|
||||
public const float GAMEPAD_DEADZONE = 0.01f;
|
||||
|
||||
public const int RENDER_TEXTURE_DEPTH = 24;
|
||||
public const RenderTextureFormat RENDER_TEXTURE_FORMAT = RenderTextureFormat.ARGB32;
|
||||
public const RenderTextureFormat RENDER_TEXTURE_FORMAT = RenderTextureFormat.DefaultHDR;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user