Game.SceneFlow.EnabledActionScoped
Assembly:
Assembly-CSharp
Namespace:
Game.SceneFlow
Type:
public class
Base:
System.IDisposable
Summary:
Helper/RAII-style class that temporarily enables or disables a proxy input action based on the currently active overlay screen. When constructed it locates a ProxyAction from the input system, registers a handler with the game's overlayBindings.onScreenActivated event, and uses a DisplayNameOverride to control the action's displayed name/priority. When the scope is disposed it unsubscribes from the event, disables the proxy action, and disposes the name override. Typical use is to enable an input action only when specific overlay screens are active.
Fields
-
private readonly OverlayBindings m_Bindings
References the overlay bindings instance from the GameManager; used to subscribe to onScreenActivated so the enabled state can update when the active overlay screen changes. -
private readonly ProxyAction m_Proxy
The ProxyAction wrapper for the target input action found via Game.Input.InputManager.instance.FindAction(...). The class toggles m_Proxy.enabled to turn the action on/off. -
private readonly DisplayNameOverride m_NameOverride
Manages a display name/priority override for the action while it is enabled. This is activated/deactivated in sync with the proxy action. -
private readonly Func<OverlayScreen, bool> m_ShouldBeEnabled
Optional predicate that receives the currently active OverlayScreen and returns whether the action should be enabled for that screen. If null, the action is enabled for all screens.
Properties
- (none)
Constructors
public EnabledActionScoped(GameManager manager, string actionMapName, string actionName, Func<OverlayScreen, bool> shouldBeEnabled = null, string displayProperty = null, int displayPriority = 20)
Finds the specified ProxyAction (actionMap + action name) via the game's InputManager, creates a DisplayNameOverride (using displayProperty and displayPriority), asserts the proxy is found, stores the optional predicate, and subscribes HandleScreenChange to manager.userInterface.overlayBindings.onScreenActivated so the action and name override will be toggled when overlay screens change.
Methods
-
private void HandleScreenChange(OverlayScreen screen)
Event handler called when the active overlay screen changes. Evaluates the predicate m_ShouldBeEnabled (if provided) to determine whether the proxy action should be enabled. Sets m_Proxy.enabled and m_NameOverride.active to the resulting boolean. -
public void Dispose()
Unsubscribes HandleScreenChange from overlayBindings.onScreenActivated, disables the proxy action (m_Proxy.enabled = false), and disposes the DisplayNameOverride. After disposal the scoped enabling behavior stops and the action remains disabled. -
public static implicit operator InputAction(EnabledActionScoped scoped)
Implicit conversion operator allowing the scoped instance to be used where an InputAction is expected by returning scoped.m_Proxy.sourceAction. Useful for passing the underlying InputAction directly to APIs.
Usage Example
// Create and keep the scope for as long as you want the action to auto-enable/disable
private EnabledActionScoped _scopedCancel;
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Enable the "Cancel" action only when the Build overlay is active,
// and apply a display name override (optional).
_scopedCancel = new EnabledActionScoped(
manager: GameManager.instance,
actionMapName: "UI",
actionName: "Cancel",
shouldBeEnabled: screen => screen == OverlayScreen.Build,
displayProperty: "ui_action_cancel",
displayPriority: 20
);
// You can also get the underlying InputAction directly:
UnityEngine.InputSystem.InputAction cancelAction = _scopedCancel;
}
// When the object is destroyed or no longer needs the behavior:
protected override void OnDestroy()
{
_scopedCancel?.Dispose();
_scopedCancel = null;
base.OnDestroy();
}