Game.InputExtensions
Assembly: Assembly-CSharp
Namespace: Game
Type: public static class InputExtensions
Base: N/A (static class)
Summary:
Extension helper methods for Unity's InputAction / InputActionSetupExtensions.BindingSyntax that simplify finding and iterating composite bindings and their parts for a given InputAction. These helpers are useful when you need to inspect or modify composite bindings (for example, "1D Axis", "2D Vector") and their named parts (like "up", "down", "left", "right") that are associated with a specific InputAction.
Fields
- This static class defines no instance or static fields.
Additional info: All functionality is provided via extension methods operating on InputAction and BindingSyntax values.
Properties
- This type exposes no properties.
Additional info: Use the extension methods directly on an InputAction or with a BindingSyntax iterator.
Constructors
- This static class has no public constructors.
Additional info: As a static class, it cannot be instantiated and has no instance constructors.
Methods
-
public static bool TryGetCompositeOfActionWithName(this InputAction action, string compositeName, out InputActionSetupExtensions.BindingSyntax iterator)
Additional info: Locates a composite binding (by exact composite name) that triggers the provided InputAction. Starts from the first composite binding in the action's actionMap and advances using NextCompositeBinding(). The out parameteriterator
will point at the matching composite binding when true, or at the last checked (invalid) iterator when false. Returns true when a valid composite binding matching compositeName and triggering the action is found; otherwise returns false. Typical use: find a specific composite (e.g., "2DVector") attached to the action. -
public static bool TryGetFirstCompositeOfAction(this InputAction action, out InputActionSetupExtensions.BindingSyntax iterator)
Additional info: Finds the first composite binding attached to the given InputAction (first composite that TriggersAction(action)). Setsiterator
to that composite binding if found and returns true. Returns false if no composite that triggers the action is found. Use this to get a starting point for iterating parts or subsequent composite bindings. -
public static bool ForEachCompositeOfAction(this InputAction inputAction, InputActionSetupExtensions.BindingSyntax startIterator, Func<InputActionSetupExtensions.BindingSyntax, bool> action, out InputActionSetupExtensions.BindingSyntax endIterator)
Additional info: Iterates composite bindings for the given inputAction starting fromstartIterator
. IfstartIterator
is not a composite, it advances to the next composite binding. For each composite binding that triggers the inputAction, the provided Func is invoked. If the Func returns false at any time, iteration stops and the method returns false.endIterator
is set to the last binding passed to the Func. Returns true if iteration completed without the Func returning false. Useful when you already have an iterator and want fine-grained control over iteration. -
public static bool ForEachCompositeOfAction(this InputAction inputAction, Func<InputActionSetupExtensions.BindingSyntax, bool> action)
Additional info: Convenience overload. Finds the first composite for the action and then calls the previous ForEachCompositeOfAction. Returns false if action is null or no composite found. Returns the same boolean semantics as the iterator-based overload. -
public static bool ForEachPartOfCompositeWithName(this InputAction inputAction, InputActionSetupExtensions.BindingSyntax startIterator, string partName, Func<InputActionSetupExtensions.BindingSyntax, bool> action, out InputActionSetupExtensions.BindingSyntax endIterator)
Additional info: Iterates parts of a composite that match the specified partName (for example, "up", "down", "positive", "negative"). IfstartIterator.binding.isComposite
is true, it advances to the first part binding matching partName via NextPartBinding(partName). Then it repeatedly invokes the provided Func for each matching part binding that is part of the composite and triggers the given inputAction. If the Func returns false, iteration stops and the method returns false. The out parameterendIterator
is set to the last processed part binding. Returns true when iteration finishes without early termination. Use this to inspect or modify specific named parts of a composite. -
public static bool ForEachPartOfCompositeWithName(this InputAction inputAction, string partName, Func<InputActionSetupExtensions.BindingSyntax, bool> action)
Additional info: Convenience overload that finds the first composite for the action and calls the iterator-based ForEachPartOfCompositeWithName. Returns false if action is null, the partName is null/empty, or no composite is found.
Usage Example
// Example: iterate all composite bindings on an InputAction and print their names,
// then iterate the "up" parts of each composite and print their effective paths.
using UnityEngine.InputSystem;
using UnityEngine;
public class InputDebug
{
public static void DumpActionComposites(InputAction action)
{
// Iterate all composites for the action
action.ForEachCompositeOfAction(binding =>
{
Debug.Log($"Composite name: {binding.binding.name}, path: {binding.binding.effectivePath}");
// For each composite, iterate "up" part bindings
action.ForEachPartOfCompositeWithName(binding, "up", part =>
{
Debug.Log($" Part 'up' path: {part.binding.effectivePath}");
return true; // continue iterating parts
}, out var _);
return true; // continue iterating composites
});
}
// Convenience overload usage
public static void DumpFirstCompositeParts(InputAction action)
{
// Iterate parts named "left" across the first composite found
action.ForEachPartOfCompositeWithName("left", part =>
{
Debug.Log($"Left part: {part.binding.effectivePath}");
return true;
});
}
}
Notes and tips: - The BindingSyntax returned by these helper methods is a struct-like iterator produced by InputActionSetupExtensions.BindingSyntax methods (NextCompositeBinding, NextPartBinding). Treat it as an iterator/value rather than a persistent reference to a binding. - The helper methods rely on BindingSyntax.binding.TriggersAction(action) to ensure the binding is associated with the target InputAction; this protects against iterating composites that belong to other actions in the same action map. - Be careful when mutating bindings from within these iteration callbacks; if you modify the action map or binding collection, iterators may become invalid. Prefer collecting edits and applying them after iteration if necessary.