Skip to content

Commit 1bda82d

Browse files
authored
CHANGE: Add action map disable / enable to rebinding sample (Unity-Technologies#2137)
1 parent 581d5da commit 1bda82d

File tree

4 files changed

+450
-264
lines changed

4 files changed

+450
-264
lines changed

Assets/Samples/RebindingUI/RebindActionUI.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,23 @@ 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+
// An "InvalidOperationException: Cannot rebind action x while it is enabled" will
269+
// be thrown if rebinding is attempted on an action that is enabled.
270+
//
271+
// On top of disabling the target action while rebinding, it is recommended to
272+
// disable any actions (or action maps) that could interact with the rebinding UI
273+
// or gameplay - it would be undesirable for rebinding to cause the player
274+
// character to jump.
275+
//
276+
// In this example, we explicitly disable both the UI input action map and
277+
// the action map containing the target action.
278+
action.actionMap.Disable();
279+
m_UIInputActionMap?.Disable();
268280

269281
// Configure the rebind.
270282
m_RebindOperation = action.PerformInteractiveRebinding(bindingIndex)
@@ -329,6 +341,8 @@ protected void OnEnable()
329341
s_RebindActionUIs.Add(this);
330342
if (s_RebindActionUIs.Count == 1)
331343
InputSystem.onActionChange += OnActionChange;
344+
if (m_DefaultInputActions != null && m_UIInputActionMap == null)
345+
m_UIInputActionMap = m_DefaultInputActions.FindActionMap("UI");
332346
}
333347

334348
protected void OnDisable()
@@ -398,6 +412,12 @@ private static void OnActionChange(object obj, InputActionChange change)
398412
[SerializeField]
399413
private Text m_RebindText;
400414

415+
[Tooltip("Optional reference to default input actions containing the UI action map. The UI action map is "
416+
+ "disabled when rebinding is in progress.")]
417+
[SerializeField]
418+
private InputActionAsset m_DefaultInputActions;
419+
private InputActionMap m_UIInputActionMap;
420+
401421
[Tooltip("Event that is triggered when the way the binding is display should be updated. This allows displaying "
402422
+ "bindings in custom ways, e.g. using images instead of text.")]
403423
[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)