Skip to content

Commit b8069a1

Browse files
committed
CHANGE: Modified rebinding UI sample to disable action maps (the target action map and the UI action map) when rebinding is in progress.
1 parent b9153bc commit b8069a1

File tree

3 files changed

+447
-264
lines changed

3 files changed

+447
-264
lines changed

Assets/Samples/RebindingUI/RebindActionUI.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,21 @@ void CleanUp()
260260
{
261261
m_RebindOperation?.Dispose();
262262
m_RebindOperation = null;
263-
action.Enable();
263+
264+
action.actionMap.Enable();
265+
m_UIInputActionMap?.Enable();
264266
}
265267

266-
//Fixes the "InvalidOperationException: Cannot rebind action x while it is enabled" error
267-
action.Disable();
268+
// When rebinding an action, it is good practice to disable the action itself.
269+
//
270+
// Here, we are disabling the entire action map in which the action resides.
271+
// It is recommended to organise your action maps such that a "Gameplay" action map
272+
// which contains the rebindable actions. This map can then be disabled while the
273+
// rebinding UI is on display.
274+
//
275+
// Also prevents "InvalidOperationException: Cannot rebind action x while it is enabled" errors.
276+
action.actionMap.Disable();
277+
m_UIInputActionMap?.Disable();
268278

269279
// Configure the rebind.
270280
m_RebindOperation = action.PerformInteractiveRebinding(bindingIndex)
@@ -329,6 +339,8 @@ protected void OnEnable()
329339
s_RebindActionUIs.Add(this);
330340
if (s_RebindActionUIs.Count == 1)
331341
InputSystem.onActionChange += OnActionChange;
342+
if (m_DefaultInputActions != null && m_UIInputActionMap == null)
343+
m_UIInputActionMap = m_DefaultInputActions.FindActionMap("UI");
332344
}
333345

334346
protected void OnDisable()
@@ -398,6 +410,12 @@ private static void OnActionChange(object obj, InputActionChange change)
398410
[SerializeField]
399411
private Text m_RebindText;
400412

413+
[Tooltip("Optional reference to default input actions containing the UI action map. The UI action map is "
414+
+ "disabled when rebinding is in progress.")]
415+
[SerializeField]
416+
private InputActionAsset m_DefaultInputActions;
417+
private InputActionMap m_UIInputActionMap;
418+
401419
[Tooltip("Event that is triggered when the way the binding is display should be updated. This allows displaying "
402420
+ "bindings in custom ways, e.g. using images instead of text.")]
403421
[SerializeField]

Assets/Samples/RebindingUI/RebindActionUIEditor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected void OnEnable()
2121
m_BindingTextProperty = serializedObject.FindProperty("m_BindingText");
2222
m_RebindOverlayProperty = serializedObject.FindProperty("m_RebindOverlay");
2323
m_RebindTextProperty = serializedObject.FindProperty("m_RebindText");
24+
m_DefaultInputActionsProperty = serializedObject.FindProperty("m_DefaultInputActions");
2425
m_UpdateBindingUIEventProperty = serializedObject.FindProperty("m_UpdateBindingUIEvent");
2526
m_RebindStartEventProperty = serializedObject.FindProperty("m_RebindStartEvent");
2627
m_RebindStopEventProperty = serializedObject.FindProperty("m_RebindStopEvent");
@@ -62,6 +63,7 @@ public override void OnInspectorGUI()
6263
EditorGUILayout.PropertyField(m_BindingTextProperty);
6364
EditorGUILayout.PropertyField(m_RebindOverlayProperty);
6465
EditorGUILayout.PropertyField(m_RebindTextProperty);
66+
EditorGUILayout.PropertyField(m_DefaultInputActionsProperty);
6567
}
6668

6769
// Events section.
@@ -153,6 +155,7 @@ protected void RefreshBindingOptions()
153155
private SerializedProperty m_BindingIdProperty;
154156
private SerializedProperty m_ActionLabelProperty;
155157
private SerializedProperty m_BindingTextProperty;
158+
private SerializedProperty m_DefaultInputActionsProperty;
156159
private SerializedProperty m_RebindOverlayProperty;
157160
private SerializedProperty m_RebindTextProperty;
158161
private SerializedProperty m_RebindStartEventProperty;

0 commit comments

Comments
 (0)