Skip to content
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

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions Assets/Samples/RebindingUI/RebindActionUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,21 @@ 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();
// When rebinding an action, it is good practice to disable the action itself.
//
// Here, we are disabling the entire action map in which the action resides.
// It is recommended to organise your action maps such that a "Gameplay" action map
// which contains the rebindable actions. This map can then be disabled while the
// rebinding UI is on display.
//
// Also prevents "InvalidOperationException: Cannot rebind action x while it is enabled" errors.
action.actionMap.Disable();
m_UIInputActionMap?.Disable();

// Configure the rebind.
m_RebindOperation = action.PerformInteractiveRebinding(bindingIndex)
Expand Down Expand Up @@ -329,6 +339,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 +410,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