Skip to content

Commit ba19e19

Browse files
committed
Improved RebindSaveLoad to allow reuse, added embryo of RebindBindingParameter to enable mouse sensitivity setting.
1 parent a66c819 commit ba19e19

File tree

5 files changed

+189
-9
lines changed

5 files changed

+189
-9
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.InputSystem;
4+
using UnityEngine.InputSystem.Processors;
5+
using UnityEngine.UI;
6+
7+
public class RebindBindingParameter : MonoBehaviour
8+
{
9+
/// <summary>
10+
/// Reference to the action that is to be rebound.
11+
/// </summary>
12+
public InputActionReference actionReference
13+
{
14+
get => m_Action;
15+
set
16+
{
17+
m_Action = value;
18+
}
19+
}
20+
21+
[Tooltip("Reference to action that holds the parameter to be .")]
22+
[SerializeField]
23+
private InputActionReference m_Action;
24+
25+
private Slider m_Slider;
26+
27+
void Awake()
28+
{
29+
m_Slider = GetComponent<Slider>();
30+
// TODO How to we register an event listener?
31+
}
32+
33+
public void ResetToDefault()
34+
{
35+
}
36+
37+
// Start is called once before the first execution of Update after the MonoBehaviour is created
38+
void Start()
39+
{
40+
//m_Action.action.processors
41+
}
42+
43+
// Update is called once per frame
44+
void Update()
45+
{
46+
}
47+
48+
private void OnEnable()
49+
{
50+
if (TryGetParameterValue(m_ParameterName, out var value))
51+
m_Slider.value = value;
52+
}
53+
54+
private string m_ParameterName = "x";
55+
56+
private bool TryGetParameterValue(string name, out float value)
57+
{
58+
if (m_Action != null)
59+
{
60+
//var k = m_Action.action.GetParameterValue((ScaleVector2Processor p) => p.x);
61+
62+
var v = m_Action.action.GetParameterValue("x");
63+
if (v.HasValue)
64+
{
65+
var val = v.Value;
66+
if (val.type == TypeCode.Single)
67+
{
68+
value = val.ToSingle();
69+
return true;
70+
}
71+
}
72+
}
73+
74+
value = default;
75+
return false;
76+
}
77+
78+
private void SetParameterValue(float value)
79+
{
80+
if (!Mathf.Approximately(m_Slider.value, value))
81+
m_Slider.value = value;
82+
if (m_Action != null)
83+
m_Action.action.ApplyParameterOverride(m_ParameterName, value);
84+
}
85+
86+
private void UpdateDisplayValue()
87+
{
88+
if (m_Action == null) return;
89+
if (m_Action.action == null) return;
90+
var parameterValue = m_Action.action.GetParameterValue("x");
91+
if (parameterValue != null)
92+
m_Slider.value = parameterValue.Value.ToSingle();
93+
}
94+
}

Assets/Samples/RebindingUI/RebindBindingParameter.cs.meta

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

Assets/Samples/RebindingUI/RebindSaveLoad.cs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,77 @@
33

44
namespace UnityEngine.InputSystem.Samples.RebindUI
55
{
6+
/// <summary>
7+
/// Handles persisting binding overrides which implies that customizations of controls will be persisted
8+
/// between runs.
9+
/// </summary>
610
public class RebindSaveLoad : MonoBehaviour
711
{
12+
/// <summary>
13+
/// The associated input action asset (Required).
14+
/// </summary>
15+
[Tooltip("The associated input action asset to be serialized to player preferences (Required).")]
816
public InputActionAsset actions;
917

10-
public void OnEnable()
18+
/// <summary>
19+
/// The associated player preference key.
20+
/// </summary>
21+
[Tooltip("The player preference key to be used when serializing binding overrides to player preferences (Required).")]
22+
public string playerPreferenceKey;
23+
24+
/// <summary>
25+
/// Loads binding overrides from player preferences and applies them to the associated input action asset.
26+
/// </summary>
27+
public void Load()
1128
{
29+
if (!IsValidConfiguration())
30+
return;
31+
1232
var rebinds = PlayerPrefs.GetString("rebinds");
13-
if (!string.IsNullOrEmpty(rebinds))
14-
actions.LoadBindingOverridesFromJson(rebinds);
33+
if (string.IsNullOrEmpty(rebinds))
34+
return; // OK, we may not have saved any binding overrides yet.
35+
36+
actions.LoadBindingOverridesFromJson(rebinds);
1537
}
1638

17-
public void OnDisable()
39+
/// <summary>
40+
/// Saves binding overrides from the associated input action asset and persists them to player preferences.
41+
/// </summary>
42+
public void Save()
1843
{
44+
if (!IsValidConfiguration())
45+
return;
46+
1947
var rebinds = actions.SaveBindingOverridesAsJson();
20-
PlayerPrefs.SetString("rebinds", rebinds);
48+
PlayerPrefs.SetString(playerPreferenceKey, rebinds);
49+
}
50+
51+
private void OnEnable()
52+
{
53+
Load();
54+
}
55+
56+
private void OnDisable()
57+
{
58+
Save();
59+
}
60+
61+
private bool IsValidConfiguration()
62+
{
63+
if (actions == null)
64+
{
65+
Debug.LogWarning("Unable to apply binding overrides from player preferences without an associated " +
66+
"action asset.");
67+
return false;
68+
}
69+
70+
if (string.IsNullOrEmpty(playerPreferenceKey))
71+
{
72+
Debug.LogWarning("Unable to load binding overrides from player preferences without a key");
73+
return false;
74+
}
75+
76+
return true;
2177
}
2278
}
2379
}

Assets/Samples/RebindingUI/RebindUISampleActions.inputactions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"id": "7c03e976-d9da-4d96-9bcd-622e98b0f3c3",
127127
"path": "<Mouse>/delta",
128128
"interactions": "",
129-
"processors": "ScaleVector2",
129+
"processors": "ScaleVector2(x=1.5,y=1.5)",
130130
"groups": "Keyboard",
131131
"action": "Look",
132132
"isComposite": false,

Assets/Samples/RebindingUI/RebindingUISampleScene.unity

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ MonoBehaviour:
457457
m_Name:
458458
m_EditorClassIdentifier:
459459
actions: {fileID: -944628639613478452, guid: 7dead05c54ca85b4681351aafd8bd03a, type: 3}
460+
playerPreferenceKey: rebinds
460461
--- !u!114 &51853247
461462
MonoBehaviour:
462463
m_ObjectHideFlags: 0
@@ -982,6 +983,7 @@ GameObject:
982983
m_Component:
983984
- component: {fileID: 244172484}
984985
- component: {fileID: 244172485}
986+
- component: {fileID: 244172486}
985987
m_Layer: 5
986988
m_Name: Slider
987989
m_TagString: Untagged
@@ -1061,7 +1063,33 @@ MonoBehaviour:
10611063
m_Value: 0.5
10621064
m_OnValueChanged:
10631065
m_PersistentCalls:
1064-
m_Calls: []
1066+
m_Calls:
1067+
- m_Target: {fileID: 244172483}
1068+
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
1069+
m_MethodName:
1070+
m_Mode: 1
1071+
m_Arguments:
1072+
m_ObjectArgument: {fileID: 0}
1073+
m_ObjectArgumentAssemblyTypeName:
1074+
m_IntArgument: 0
1075+
m_FloatArgument: 0
1076+
m_StringArgument:
1077+
m_BoolArgument: 0
1078+
m_CallState: 2
1079+
--- !u!114 &244172486
1080+
MonoBehaviour:
1081+
m_ObjectHideFlags: 0
1082+
m_CorrespondingSourceObject: {fileID: 0}
1083+
m_PrefabInstance: {fileID: 0}
1084+
m_PrefabAsset: {fileID: 0}
1085+
m_GameObject: {fileID: 244172483}
1086+
m_Enabled: 1
1087+
m_EditorHideFlags: 0
1088+
m_Script: {fileID: 11500000, guid: b52b7577d0f9e44e99408e2e11257fd6, type: 3}
1089+
m_Name:
1090+
m_EditorClassIdentifier: Unity.InputSystem.RebindingUI::RebindBindingParameter
1091+
m_Action: {fileID: 4485540969121359642, guid: 7dead05c54ca85b4681351aafd8bd03a,
1092+
type: 3}
10651093
--- !u!1 &303988305
10661094
GameObject:
10671095
m_ObjectHideFlags: 0
@@ -5142,8 +5170,8 @@ RectTransform:
51425170
m_ConstrainProportionsScale: 0
51435171
m_Children:
51445172
- {fileID: 674949961}
5145-
- {fileID: 1424978755}
51465173
- {fileID: 244172484}
5174+
- {fileID: 1424978755}
51475175
m_Father: {fileID: 1219085456}
51485176
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
51495177
m_AnchorMin: {x: 0, y: 1}
@@ -5442,7 +5470,7 @@ MonoBehaviour:
54425470
m_OnClick:
54435471
m_PersistentCalls:
54445472
m_Calls:
5445-
- m_Target: {fileID: 1414988297}
5473+
- m_Target: {fileID: 244172483}
54465474
m_TargetAssemblyTypeName:
54475475
m_MethodName: ResetToDefault
54485476
m_Mode: 1

0 commit comments

Comments
 (0)