Skip to content

Commit 3ad7c58

Browse files
committed
Improvements to rebinding sample to increase visibility of potential side-effects and be more similar to a real game.
1 parent d1932d5 commit 3ad7c58

11 files changed

+2612
-340
lines changed

Assets/Samples/RebindingUI/ActionIndicator.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,31 @@
33
using UnityEngine.InputSystem;
44
using UnityEngine.UI;
55

6+
/// <summary>
7+
/// A simple visual indicator of action performed.
8+
/// </summary>
9+
/// <remarks>Error handling have been excluded for simplicity.</remarks>
10+
[RequireComponent(typeof(Image))]
611
public class ActionUIIndicator : MonoBehaviour
712
{
13+
[Tooltip("Reference to the associated action to be visualized.")]
814
public InputActionReference action;
9-
private Image image;
15+
16+
[Tooltip("The color to show when the associated action is performed.")]
17+
public Color activeColor = Color.green;
18+
19+
[Tooltip("The color to show when the associated action has not been performed for the specified duration.")]
20+
public Color inactiveColor = Color.black;
21+
22+
[Tooltip("The duration for which the indicator should be lit before becoming completely inactive.")]
23+
public float duration = 1.0f;
24+
25+
private double m_RealTimeLastPerformed;
26+
private Image m_Image;
1027

1128
void Start()
1229
{
13-
image = GetComponent<Image>();
30+
m_Image = GetComponent<Image>();
1431
}
1532

1633
private void OnEnable()
@@ -27,19 +44,13 @@ private void OnDisable()
2744

2845
private void OnPerformed(InputAction.CallbackContext obj)
2946
{
30-
image.color = ColorWithAlpha(1.0f);
31-
}
32-
33-
private Color ColorWithAlpha(float alpha)
34-
{
35-
var color = image.color;
36-
return new Color(color.r, color.g, color.b, alpha);
47+
m_RealTimeLastPerformed = Time.realtimeSinceStartupAsDouble;
3748
}
3849

3950
private void Update()
4051
{
41-
var color = image.color;
42-
if (color.a > 0.0f)
43-
image.color = ColorWithAlpha(Mathf.Max(image.color.a - Time.deltaTime * 1.0f, 0.0f));
52+
var elapsedSincePerformed = Time.realtimeSinceStartupAsDouble - m_RealTimeLastPerformed;
53+
m_Image.color = duration <= 0.0f ? inactiveColor : Color.Lerp(inactiveColor, activeColor,
54+
(float)Math.Max(0.0, 1.0 - elapsedSincePerformed / duration));
4455
}
4556
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using UnityEngine;
2+
using UnityEngine.EventSystems;
3+
using UnityEngine.InputSystem;
4+
5+
/// <summary>
6+
/// Simple game manager that manages enabling/disabling in-game and UI actions.
7+
/// </summary>
8+
public class GameManager : MonoBehaviour
9+
{
10+
// TODO Its still an issue if we assign UI cancel button in rebinding,
11+
// same goes for keyboard ESC
12+
13+
public GameObject menu;
14+
public InputActionAsset gameplayActions;
15+
public InputActionReference menuAction;
16+
public InputActionReference exitMenuAction;
17+
public GameObject initiallySelectedGameObject;
18+
19+
void Start()
20+
{
21+
// Let menu initially be disabled
22+
menu.SetActive(false);
23+
24+
// Let gameplay actions be initially enabled
25+
gameplayActions.Enable();
26+
}
27+
28+
private void OnEnable()
29+
{
30+
menuAction.action.performed += OnMenu;
31+
exitMenuAction.action.performed += OnExitMenu;
32+
}
33+
34+
private void OnDisable()
35+
{
36+
menuAction.action.performed -= OnMenu;
37+
exitMenuAction.action.performed -= OnExitMenu;
38+
}
39+
40+
private void OnMenu(InputAction.CallbackContext obj)
41+
{
42+
// Disable gameplay actions while in menu
43+
gameplayActions.Disable();
44+
45+
// Enable menu if currently not active
46+
menu.SetActive(true);
47+
48+
// Make sure EventSystem has a selection to allow gamepad navigation
49+
if (EventSystem.current.currentSelectedGameObject == null)
50+
EventSystem.current.SetSelectedGameObject(EventSystem.current.firstSelectedGameObject);
51+
}
52+
53+
private void OnExitMenu(InputAction.CallbackContext obj)
54+
{
55+
if (!menu.activeInHierarchy)
56+
return;
57+
58+
// Hide menu
59+
menu.SetActive(false);
60+
61+
// Reenable gameplay actions
62+
gameplayActions.Enable();
63+
}
64+
}

Assets/Samples/RebindingUI/GameManager.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/RebindActionUI.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,17 @@ private void PerformInteractiveRebind(InputAction action, int bindingIndex, bool
256256
{
257257
m_RebindOperation?.Cancel(); // Will null out m_RebindOperation.
258258

259+
var actionWasEnabledPriorToRebind = action.enabled; // Allow restoring enabled state
260+
259261
void CleanUp()
260262
{
261263
m_RebindOperation?.Dispose();
262264
m_RebindOperation = null;
263265

264-
action.actionMap.Enable();
266+
// Restore action enabled state based on state prior to rebind
267+
if (actionWasEnabledPriorToRebind)
268+
action.actionMap.Enable();
269+
265270
m_UIInputActionMap?.Enable();
266271
}
267272

@@ -274,8 +279,9 @@ void CleanUp()
274279
// character to jump.
275280
//
276281
// In this example, we explicitly disable both the UI input action map and
277-
// the action map containing the target action.
278-
action.actionMap.Disable();
282+
// the action map containing the target action if it was initially enabled.
283+
if (actionWasEnabledPriorToRebind)
284+
action.actionMap.Disable();
279285
m_UIInputActionMap?.Disable();
280286

281287
// Configure the rebind.

0 commit comments

Comments
 (0)