Skip to content

CHANGE: Add action map disable / enable to rebinding sample #2137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Assets/Samples/RebindingUI/RebindActionUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,23 @@ void CleanUp()
{
m_RebindOperation?.Dispose();
m_RebindOperation = null;
action.Enable();

action.actionMap.Enable();
m_UIInputActionMap?.Enable();
}

//Fixes the "InvalidOperationException: Cannot rebind action x while it is enabled" error
action.Disable();
// An "InvalidOperationException: Cannot rebind action x while it is enabled" will
// be thrown if rebinding is attempted on an action that is enabled.
//
// On top of disabling the target action while rebinding, it is recommended to
// disable any actions (or action maps) that could interact with the rebinding UI
// or gameplay - it would be undesirable for rebinding to cause the player
// character to jump.
//
// In this example, we explicitly disable both the UI input action map and
// the action map containing the target action.
action.actionMap.Disable();
m_UIInputActionMap?.Disable();

// Configure the rebind.
m_RebindOperation = action.PerformInteractiveRebinding(bindingIndex)
Expand Down Expand Up @@ -329,6 +341,8 @@ protected void OnEnable()
s_RebindActionUIs.Add(this);
if (s_RebindActionUIs.Count == 1)
InputSystem.onActionChange += OnActionChange;
if (m_DefaultInputActions != null && m_UIInputActionMap == null)
m_UIInputActionMap = m_DefaultInputActions.FindActionMap("UI");
}

protected void OnDisable()
Expand Down Expand Up @@ -398,6 +412,12 @@ private static void OnActionChange(object obj, InputActionChange change)
[SerializeField]
private Text m_RebindText;

[Tooltip("Optional reference to default input actions containing the UI action map. The UI action map is "
+ "disabled when rebinding is in progress.")]
[SerializeField]
private InputActionAsset m_DefaultInputActions;
private InputActionMap m_UIInputActionMap;

[Tooltip("Event that is triggered when the way the binding is display should be updated. This allows displaying "
+ "bindings in custom ways, e.g. using images instead of text.")]
[SerializeField]
Expand Down
3 changes: 3 additions & 0 deletions Assets/Samples/RebindingUI/RebindActionUIEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected void OnEnable()
m_BindingTextProperty = serializedObject.FindProperty("m_BindingText");
m_RebindOverlayProperty = serializedObject.FindProperty("m_RebindOverlay");
m_RebindTextProperty = serializedObject.FindProperty("m_RebindText");
m_DefaultInputActionsProperty = serializedObject.FindProperty("m_DefaultInputActions");
m_UpdateBindingUIEventProperty = serializedObject.FindProperty("m_UpdateBindingUIEvent");
m_RebindStartEventProperty = serializedObject.FindProperty("m_RebindStartEvent");
m_RebindStopEventProperty = serializedObject.FindProperty("m_RebindStopEvent");
Expand Down Expand Up @@ -62,6 +63,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(m_BindingTextProperty);
EditorGUILayout.PropertyField(m_RebindOverlayProperty);
EditorGUILayout.PropertyField(m_RebindTextProperty);
EditorGUILayout.PropertyField(m_DefaultInputActionsProperty);
}

// Events section.
Expand Down Expand Up @@ -153,6 +155,7 @@ protected void RefreshBindingOptions()
private SerializedProperty m_BindingIdProperty;
private SerializedProperty m_ActionLabelProperty;
private SerializedProperty m_BindingTextProperty;
private SerializedProperty m_DefaultInputActionsProperty;
private SerializedProperty m_RebindOverlayProperty;
private SerializedProperty m_RebindTextProperty;
private SerializedProperty m_RebindStartEventProperty;
Expand Down
Loading