Skip to content

Commit 6a81821

Browse files
committed
Add error handling while reading inputs
1 parent dc89fd9 commit 6a81821

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

Runtime/NavigationStack.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class NavigationStack : MonoBehaviour
4646
/// off the stack.
4747
/// </summary>
4848
[Tooltip("The input button that handles backwards navigation by popping items off the stack.")]
49-
public string backNavigationInputButton = "Cancel";
49+
public string legacyBackNavigationInput = "Cancel";
5050
#endif
5151

5252
/// <summary>
@@ -116,8 +116,17 @@ private void OnDisable()
116116
#if ENABLE_LEGACY_INPUT_MANAGER
117117
private void Update()
118118
{
119-
if (Input.GetButtonDown(backNavigationInputButton)) {
120-
Pop();
119+
try
120+
{
121+
if (!string.IsNullOrEmpty(legacyBackNavigationInput) && Input.GetButtonDown(legacyBackNavigationInput)) {
122+
Pop();
123+
}
124+
}
125+
catch
126+
{
127+
#if UNITY_EDITOR || DEVELOPMENT_BUILD
128+
Debug.LogWarning($"[NavigationStack]: Input button '{legacyBackNavigationInput}' is not setup.\nDefine the input in the Input Manager settings accessed from the menu: Edit > Project Settings");
129+
#endif
121130
}
122131
}
123132
#endif

Runtime/ScrollWithInput.cs

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,26 @@ public class ScrollWithInput : MonoBehaviour
2020
/// </summary>
2121
public ScrollRect scrollRect { get; private set; }
2222

23-
/// <summary>
24-
/// The direction to scroll the ScrollRect.
25-
/// </summary>
26-
[Tooltip("The direction to scroll the ScrollRect.")]
27-
public ScrollDirection scrollDirection = ScrollDirection.Vertical;
28-
2923
#if ENABLE_INPUT_SYSTEM
30-
3124
/// <summary>
3225
/// The input action that handles scrolling.
3326
/// </summary>
3427
[Tooltip("The input action that handles scrolling.")]
3528
public InputAction scrollInput = new InputAction("ScrollInput", InputActionType.Value, null, null, null, "Vector2");
29+
#endif
3630

37-
#elif ENABLE_LEGACY_INPUT_MANAGER
38-
31+
#if ENABLE_LEGACY_INPUT_MANAGER
3932
/// <summary>
40-
/// The input axis that handles scrolling in the y-axis.
33+
/// The legacy input axis that handles scrolling in the y-axis.
4134
/// </summary>
42-
[Tooltip("The input axis that handles scrolling in the y-axis.")]
43-
public string scrollInputAxisY = "";
35+
[Tooltip("The legacy input axis that handles scrolling in the y-axis.")]
36+
public string legacyScrollInputY = "";
4437

4538
/// <summary>
46-
/// The input axis that handles scrolling in the x-axis.
39+
/// The legacy input axis that handles scrolling in the x-axis.
4740
/// </summary>
48-
[Tooltip("The input axis that handles scrolling in the x-axis.")]
49-
public string scrollInputAxisX = "";
50-
41+
[Tooltip("The legacy input axis that handles scrolling in the x-axis.")]
42+
public string legacyScrollInputX = "";
5143
#endif
5244

5345
/// <summary>
@@ -56,6 +48,12 @@ public class ScrollWithInput : MonoBehaviour
5648
[Tooltip("The sensitivity multiplier applied to the input.")]
5749
public float sensitivity = 1f;
5850

51+
/// <summary>
52+
/// The direction to scroll the ScrollRect.
53+
/// </summary>
54+
[Tooltip("The direction to scroll the ScrollRect.")]
55+
public ScrollDirection direction = ScrollDirection.Vertical;
56+
5957
private void Awake()
6058
{
6159
scrollRect = GetComponent<ScrollRect>();
@@ -96,17 +94,19 @@ private void Update()
9694

9795
#if ENABLE_INPUT_SYSTEM
9896
input = scrollInput.ReadValue<Vector2>();
99-
#elif ENABLE_LEGACY_INPUT_MANAGER
100-
if (scrollInputAxisY != "") {
101-
input.y = Input.GetAxis(scrollInputAxisY);
97+
#endif
98+
99+
#if ENABLE_LEGACY_INPUT_MANAGER
100+
if (input.y == 0f) {
101+
input.y = GetAxis(legacyScrollInputX);
102102
}
103103

104-
if (scrollInputAxisX != "") {
105-
input.x = Input.GetAxis(scrollInputAxisX);
104+
if (input.x == 0f) {
105+
input.x = GetAxis(legacyScrollInputY);
106106
}
107107
#endif
108108

109-
switch (scrollDirection)
109+
switch (direction)
110110
{
111111
case ScrollDirection.Vertical:
112112
input.x = 0f;
@@ -121,6 +121,28 @@ private void Update()
121121
}
122122
}
123123

124+
#if ENABLE_LEGACY_INPUT_MANAGER
125+
private float GetAxis(string inputName)
126+
{
127+
float value = 0f;
128+
129+
try
130+
{
131+
if (!string.IsNullOrEmpty(inputName)) {
132+
value = Input.GetAxis(inputName);
133+
}
134+
}
135+
catch
136+
{
137+
#if UNITY_EDITOR || DEVELOPMENT_BUILD
138+
Debug.LogWarning($"[ScrollWithInput]: Input axis '{inputName}' is not setup.\nDefine the input in the Input Manager settings accessed from the menu: Edit > Project Settings");
139+
#endif
140+
}
141+
142+
return value;
143+
}
144+
#endif
145+
124146
}
125147

126148
}

0 commit comments

Comments
 (0)