-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
860 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
Packages/com.unity.inputsystem/Documentation~/CreatingBindingsAPI.md
This file was deleted.
Oops, something went wrong.
130 changes: 130 additions & 0 deletions
130
Packages/com.unity.inputsystem/Documentation~/InteractiveRebinding.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
|
||
|
||
## Interactive rebinding | ||
|
||
>__Note:__ To download a sample project which demonstrates how to set up a rebinding user interface with Input System APIs, open the Package Manager, select the Input System Package, and choose the sample project "Rebinding UI" to download. | ||
Runtime rebinding allows users of your application to set their own Bindings. | ||
|
||
To allow users to choose their own Bindings interactively, use the [`InputActionRebindingExtensions.RebindingOperation`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html) class. Call the [`PerformInteractiveRebinding()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_PerformInteractiveRebinding_UnityEngine_InputSystem_InputAction_System_Int32_) method on an Action to create a rebinding operation. This operation waits for the Input System to register any input from any Device which matches the Action's expected Control type, then uses [`InputBinding.overridePath`](../api/UnityEngine.InputSystem.InputBinding.html#UnityEngine_InputSystem_InputBinding_overridePath) to assign the Control path for that Control to the Action's Bindings. If the user actuates multiple Controls, the rebinding operation chooses the Control with the highest [magnitude](Controls.md#control-actuation). | ||
|
||
>IMPORTANT: You must dispose of [`InputActionRebindingExtensions.RebindingOperation`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html) instances via `Dispose()`, so that they don't leak memory on the unmanaged memory heap. | ||
```C# | ||
void RemapButtonClicked(InputAction actionToRebind) | ||
{ | ||
var rebindOperation = actionToRebind | ||
.PerformInteractiveRebinding().Start(); | ||
} | ||
``` | ||
|
||
The [`InputActionRebindingExtensions.RebindingOperation`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html) API is highly configurable to match your needs. For example, you can: | ||
|
||
* Choose expected Control types ([`WithExpectedControlType()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RebindingOperation_WithExpectedControlType_System_Type_)). | ||
|
||
* Exclude certain Controls ([`WithControlsExcluding()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RebindingOperation_WithControlsExcluding_System_String_)). | ||
|
||
* Set a Control to cancel the operation ([`WithCancelingThrough()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RebindingOperation_WithCancelingThrough_UnityEngine_InputSystem_InputControl_)). | ||
|
||
* Choose which Bindings to apply the operation on if the Action has multiple Bindings ([`WithTargetBinding()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RebindingOperation_WithTargetBinding_System_Int32_), [`WithBindingGroup()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RebindingOperation_WithBindingGroup_System_String_), [`WithBindingMask()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RebindingOperation_WithBindingMask_System_Nullable_UnityEngine_InputSystem_InputBinding__)). | ||
|
||
Refer to the scripting API reference for [`InputActionRebindingExtensions.RebindingOperation`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.RebindingOperation.html) for a full overview. | ||
|
||
Note that [`PerformInteractiveRebinding()`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_PerformInteractiveRebinding_UnityEngine_InputSystem_InputAction_System_Int32_) automatically applies a set of default configurations based on the given action and targeted binding. | ||
|
||
## Saving and loading rebinds | ||
|
||
You can serialize override properties of [Bindings](../api/UnityEngine.InputSystem.InputBinding.html) by serializing them as JSON strings and restoring them from these. Use [`SaveBindingOverridesAsJson`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_SaveBindingOverridesAsJson_UnityEngine_InputSystem_IInputActionCollection2_) to create these strings and [`LoadBindingOverridesFromJson`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_LoadBindingOverridesFromJson_UnityEngine_InputSystem_IInputActionCollection2_System_String_System_Boolean_) to restore overrides from them. | ||
|
||
```CSharp | ||
// Store player rebinds in PlayerPrefs. | ||
var rebinds = playerInput.actions.SaveBindingOverridesAsJson(); | ||
PlayerPrefs.SetString("rebinds", rebinds); | ||
|
||
// Restore player rebinds from PlayerPrefs (removes all existing | ||
// overrides on the actions; pass `false` for second argument | ||
// in case you want to prevent that). | ||
var rebinds = PlayerPrefs.GetString("rebinds"); | ||
playerInput.actions.LoadBindingOverridesFromJson(rebinds); | ||
``` | ||
|
||
### Restoring original Bindings | ||
|
||
You can remove Binding overrides and thus restore defaults by using [`RemoveBindingOverride`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RemoveBindingOverride_UnityEngine_InputSystem_InputAction_System_Int32_) or [`RemoveAllBindingOverrides`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_RemoveAllBindingOverrides_UnityEngine_InputSystem_IInputActionCollection2_). | ||
|
||
```CSharp | ||
// Remove binding overrides from the first binding of the "fire" action. | ||
playerInput.actions["fire"].RemoveBindingOverride(0); | ||
|
||
// Remove all binding overrides from the "fire" action. | ||
playerInput.actions["fire"].RemoveAllBindingOverrides(); | ||
|
||
// Remove all binding overrides from a player's actions. | ||
playerInput.actions.RemoveAllBindingOverrides(); | ||
``` | ||
|
||
### Displaying Bindings | ||
|
||
It can be useful for the user to know what an Action is currently bound to (taking any potentially active rebindings into account) while rebinding UIs, and for on-screen hints while the app is running. You can use [`InputBinding.effectivePath`](../api/UnityEngine.InputSystem.InputBinding.html#UnityEngine_InputSystem_InputBinding_effectivePath) to get the currently active path for a Binding (which returns [`overridePath`](../api/UnityEngine.InputSystem.InputBinding.html#UnityEngine_InputSystem_InputBinding_overridePath) if set, or otherwise returns [`path`](../api/UnityEngine.InputSystem.InputBinding.html#UnityEngine_InputSystem_InputBinding_path)). | ||
|
||
The easiest way to retrieve a display string for an action is to call [`InputActionRebindingExtensions.GetBindingDisplayString`](../api/UnityEngine.InputSystem.InputActionRebindingExtensions.html#UnityEngine_InputSystem_InputActionRebindingExtensions_GetBindingDisplayString_) which is an extension method for [`InputAction`](../api/UnityEngine.InputSystem.InputAction.html). | ||
|
||
```CSharp | ||
// Get a binding string for the action as a whole. This takes into account which | ||
// bindings are currently active and the actual controls bound to the action. | ||
m_RebindButton.GetComponentInChildren<Text>().text = action.GetBindingDisplayString(); | ||
|
||
// Get a binding string for a specific binding on an action by index. | ||
m_RebindButton.GetComponentInChildren<Text>().text = action.GetBindingDisplayString(1); | ||
|
||
// Look up binding indices with GetBindingIndex. | ||
var bindingIndex = action.GetBindingIndex(InputBinding.MaskByGroup("Gamepad")); | ||
m_RebindButton.GetComponentInChildren<Text>().text = | ||
action.GetBindingDisplayString(bindingIndex); | ||
``` | ||
|
||
You can also use this method to replace the text string with images. | ||
|
||
```CSharp | ||
// Call GetBindingDisplayString() such that it also returns information about the | ||
// name of the device layout and path of the control on the device. This information | ||
// is useful for reliably associating imagery with individual controls. | ||
// NOTE: The first argument is the index of the binding within InputAction.bindings. | ||
var bindingString = action.GetBindingDisplayString(0, out deviceLayout, out controlPath); | ||
|
||
// If it's a gamepad, look up an icon for the control. | ||
Sprite icon = null; | ||
if (!string.IsNullOrEmpty(deviceLayout) | ||
&& !string.IsNullOrEmpty(controlPath) | ||
&& InputSystem.IsFirstLayoutBasedOnSecond(deviceLayout, "Gamepad")) | ||
{ | ||
switch (controlPath) | ||
{ | ||
case "buttonSouth": icon = aButtonIcon; break; | ||
case "dpad/up": icon = dpadUpIcon; break; | ||
//... | ||
} | ||
} | ||
|
||
// If you have an icon, display it instead of the text. | ||
var text = m_RebindButton.GetComponentInChildren<Text>(); | ||
var image = m_RebindButton.GetComponentInChildren<Image>(); | ||
if (icon != null) | ||
{ | ||
// Display icon. | ||
text.gameObject.SetActive(false); | ||
image.gameObject.SetActive(true); | ||
image.sprite = icon; | ||
} | ||
else | ||
{ | ||
// Display text. | ||
text.gameObject.SetActive(true); | ||
image.gameObject.SetActive(false); | ||
text.text = bindingString; | ||
} | ||
``` | ||
|
||
Additionally, each Binding has a [`ToDisplayString`](../api/UnityEngine.InputSystem.InputBinding.html#UnityEngine_InputSystem_InputBinding_ToDisplayString_UnityEngine_InputSystem_InputBinding_DisplayStringOptions_UnityEngine_InputSystem_InputControl_) method, which you can use to turn individual Bindings into display strings. There is also a generic formatting method for Control paths, [`InputControlPath.ToHumanReadableString`](../api/UnityEngine.InputSystem.InputControlPath.html#UnityEngine_InputSystem_InputControlPath_ToHumanReadableString_System_String_UnityEngine_InputSystem_InputControlPath_HumanReadableStringOptions_UnityEngine_InputSystem_InputControl_), which you can use with arbitrary Control path strings. | ||
|
||
Note that the Controls a Binding resolves to can change at any time, and the display strings for controls might change dynamically. For example, if the user switches the currently active keyboard layout, the display string for each individual key on the [`Keyboard`](../api/UnityEngine.InputSystem.Keyboard.html) might change. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.