Skip to content

Commit 9ba0d43

Browse files
author
Rene Damm
authored
NEW: Save and load rebinds as JSON (#1186).
1 parent 37cda0e commit 9ba0d43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2621
-325
lines changed

Assets/Samples/CustomComposite/CustomComposite.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static CustomComposite()
5858

5959
// In the player, [RuntimeInitializeOnLoadMethod] will make sure our
6060
// initialization code gets called during startup.
61-
[RuntimeInitializeOnLoadMethod]
61+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
6262
private static void Initialize()
6363
{
6464
// This registers the composite with the input system. After calling this

Assets/Samples/CustomDevice/CustomDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static CustomDevice()
154154

155155
// In the player, [RuntimeInitializeOnLoadMethod] will make sure our
156156
// initialization code gets called during startup.
157-
[RuntimeInitializeOnLoadMethod]
157+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
158158
private static void Initialize()
159159
{
160160
// Register our device with the input system. We also register

Assets/Samples/CustomDeviceUsages/CustomDeviceUsages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static InitCustomDeviceUsages()
4444
Initialize();
4545
}
4646

47-
[RuntimeInitializeOnLoadMethod]
47+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
4848
private static void Initialize()
4949
{
5050
// Here we register the layout override with the system.

Assets/Samples/InGameHints/InGameHintsActions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace UnityEngine.InputSystem.Samples.InGameHints
1919
{
20-
public partial class @InGameHintsActions: IInputActionCollection, IDisposable
20+
public partial class @InGameHintsActions: IInputActionCollection2, IDisposable
2121
{
2222
public InputActionAsset asset { get; }
2323
public @InGameHintsActions()
@@ -311,6 +311,18 @@ public void Disable()
311311
asset.Disable();
312312
}
313313

314+
public IEnumerable<InputBinding> bindings => asset.bindings;
315+
316+
public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false)
317+
{
318+
return asset.FindAction(actionNameOrId, throwIfNotFound);
319+
}
320+
321+
public int FindBinding(InputBinding bindingMask, out InputAction action)
322+
{
323+
return asset.FindBinding(bindingMask, out action);
324+
}
325+
314326
// Gameplay
315327
private readonly InputActionMap m_Gameplay;
316328
private IGameplayActions m_GameplayActionsCallbackInterface;

Assets/Samples/RebindingUI/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ This sample demonstrates how to use the Input System APIs to set up a rebinding
22

33
To demonstrate how to use images instead of textual display strings, take a look at [GamepadIconsExample](./GamepadIconsExample.cs).
44

5+
Finally, the [RebindSaveLoad](./RebindSaveLoad.cs) script demonstrates how to persist user rebinds in `PlayerPrefs` and how to restore them from there.
6+
57
The icons used in the sample are taken from [Free Prompts Pack](https://opengameart.org/content/free-keyboard-and-controllers-prompts-pack) made by Nicolae Berbece.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using UnityEngine;
2+
using UnityEngine.InputSystem;
3+
4+
public class RebindSaveLoad : MonoBehaviour
5+
{
6+
public InputActionAsset actions;
7+
8+
public void OnEnable()
9+
{
10+
var rebinds = PlayerPrefs.GetString("rebinds");
11+
if (!string.IsNullOrEmpty(rebinds))
12+
actions.LoadBindingOverridesFromJson(rebinds);
13+
}
14+
15+
public void OnDisable()
16+
{
17+
var rebinds = actions.SaveBindingOverridesAsJson();
18+
PlayerPrefs.SetString("rebinds", rebinds);
19+
}
20+
}

Assets/Samples/RebindingUI/RebindSaveLoad.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)