Skip to content

Commit 90a9a24

Browse files
committed
Tweaks to sample, adding missing namespace, changed game manager to use classic state machine for clarity.
1 parent ca3e1cc commit 90a9a24

File tree

6 files changed

+838
-273
lines changed

6 files changed

+838
-273
lines changed

Assets/Samples/RebindingUI/RebindUIActionIndicator.cs

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,59 @@
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))]
11-
public class RebindUIActionIndicator : MonoBehaviour
6+
namespace UnityEngine.InputSystem.Samples.RebindUI
127
{
13-
[Tooltip("Reference to the associated action to be visualized.")]
14-
public InputActionReference action;
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;
27-
28-
void Start()
29-
{
30-
m_Image = GetComponent<Image>();
31-
}
32-
33-
private void OnEnable()
34-
{
35-
action.action.performed += OnPerformed;
36-
action.action.Enable();
37-
}
38-
39-
private void OnDisable()
40-
{
41-
action.action.Disable();
42-
action.action.performed -= OnPerformed;
43-
}
44-
45-
private void OnPerformed(InputAction.CallbackContext obj)
46-
{
47-
m_RealTimeLastPerformed = Time.realtimeSinceStartupAsDouble;
48-
}
49-
50-
private void Update()
8+
/// <summary>
9+
/// A simple visual indicator of action performed.
10+
/// </summary>
11+
/// <remarks>Error handling have been excluded for simplicity.</remarks>
12+
[RequireComponent(typeof(Image))]
13+
public class RebindUIActionIndicator : MonoBehaviour
5114
{
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));
15+
[Tooltip("Reference to the associated action to be visualized.")]
16+
public InputActionReference action;
17+
18+
[Tooltip("The color to show when the associated action is performed.")]
19+
public Color activeColor = Color.green;
20+
21+
[Tooltip("The color to show when the associated action has not been performed for the specified duration.")]
22+
public Color inactiveColor = Color.black;
23+
24+
[Tooltip("The duration for which the indicator should be lit before becoming completely inactive.")]
25+
public float duration = 1.0f;
26+
27+
private double m_RealTimeLastPerformed;
28+
private Image m_Image;
29+
30+
void Start()
31+
{
32+
m_Image = GetComponent<Image>();
33+
}
34+
35+
private void OnEnable()
36+
{
37+
action.action.performed += OnPerformed;
38+
action.action.Enable();
39+
}
40+
41+
private void OnDisable()
42+
{
43+
action.action.Disable();
44+
action.action.performed -= OnPerformed;
45+
}
46+
47+
private void OnPerformed(InputAction.CallbackContext obj)
48+
{
49+
m_RealTimeLastPerformed = Time.realtimeSinceStartupAsDouble;
50+
}
51+
52+
private void Update()
53+
{
54+
var elapsedSincePerformed = Time.realtimeSinceStartupAsDouble - m_RealTimeLastPerformed;
55+
m_Image.color = duration <= 0.0f
56+
? inactiveColor
57+
: Color.Lerp(inactiveColor, activeColor,
58+
(float)Math.Max(0.0, 1.0 - elapsedSincePerformed / duration));
59+
}
5560
}
5661
}
Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,88 @@
1-
using UnityEngine;
21
using UnityEngine.EventSystems;
3-
using UnityEngine.InputSystem;
42

5-
/// <summary>
6-
/// Simple game manager that manages enabling/disabling in-game and UI actions.
7-
/// </summary>
8-
public class RebindUIGameManager : MonoBehaviour
3+
namespace UnityEngine.InputSystem.Samples.RebindUI
94
{
10-
public GameObject menu;
11-
public InputActionAsset gameplayActions;
12-
public InputActionReference menuAction;
13-
public InputActionReference exitMenuAction;
14-
public GameObject initiallySelectedGameObject;
15-
16-
void Start()
5+
/// <summary>
6+
/// Simple game manager that manages enabling/disabling in-game and UI actions.
7+
/// </summary>
8+
public class RebindUIGameManager : MonoBehaviour
179
{
18-
// Let menu initially be disabled
19-
menu.SetActive(false);
10+
public GameObject menu;
11+
public InputActionAsset gameplayActions;
12+
public InputActionReference menuAction;
13+
public InputActionReference exitMenuAction;
2014

21-
// Let gameplay actions be initially enabled
22-
gameplayActions.Enable();
23-
}
15+
private enum GameState
16+
{
17+
Initializing,
18+
Playing,
19+
RebindingMenu
20+
}
2421

25-
private void OnEnable()
26-
{
27-
menuAction.action.performed += OnMenu;
28-
exitMenuAction.action.performed += OnExitMenu;
29-
}
22+
private GameState m_CurrentState = GameState.Initializing;
3023

31-
private void OnDisable()
32-
{
33-
menuAction.action.performed -= OnMenu;
34-
exitMenuAction.action.performed -= OnExitMenu;
35-
}
24+
void Start()
25+
{
26+
SetState(GameState.Playing);
3627

37-
private void OnMenu(InputAction.CallbackContext obj)
38-
{
39-
// Disable gameplay actions while in menu
40-
gameplayActions.Disable();
28+
// Let menu initially be disabled
29+
menu.SetActive(false);
4130

42-
// Enable menu if currently not active
43-
menu.SetActive(true);
31+
// Let gameplay actions be initially enabled
32+
gameplayActions.Enable();
33+
}
4434

45-
// Make sure EventSystem has a selection to allow gamepad navigation
46-
if (EventSystem.current.currentSelectedGameObject == null)
47-
EventSystem.current.SetSelectedGameObject(EventSystem.current.firstSelectedGameObject);
48-
}
35+
private void SetState(GameState newState)
36+
{
37+
// Abort if there is no change to state
38+
if (newState == m_CurrentState)
39+
return;
4940

50-
private void OnExitMenu(InputAction.CallbackContext obj)
51-
{
52-
// TODO We cannot do this without first cancelling rebinding
41+
switch (newState)
42+
{
43+
// Entering game mode: enable in-game actions, show menu
44+
case GameState.Playing:
45+
gameplayActions.Enable();
46+
menu.SetActive(false);
47+
break;
48+
49+
// Entering menu: disable in-game actions, hide menu, make sure we have selection
50+
case GameState.RebindingMenu:
51+
gameplayActions.Disable();
52+
menu.SetActive(true);
53+
if (EventSystem.current.currentSelectedGameObject == null)
54+
EventSystem.current.SetSelectedGameObject(EventSystem.current.firstSelectedGameObject);
55+
break;
56+
57+
case GameState.Initializing:
58+
default:
59+
break;
60+
}
61+
62+
// Update current state
63+
m_CurrentState = newState;
64+
}
65+
66+
private void OnMenu(InputAction.CallbackContext obj)
67+
{
68+
SetState(GameState.RebindingMenu);
69+
}
5370

54-
if (!menu.activeInHierarchy)
55-
return;
71+
private void OnExitMenu(InputAction.CallbackContext obj)
72+
{
73+
SetState(GameState.Playing);
74+
}
5675

57-
// Hide menu
58-
menu.SetActive(false);
76+
private void OnEnable()
77+
{
78+
menuAction.action.performed += OnMenu;
79+
exitMenuAction.action.performed += OnExitMenu;
80+
}
5981

60-
// Reenable gameplay actions
61-
gameplayActions.Enable();
82+
private void OnDisable()
83+
{
84+
menuAction.action.performed -= OnMenu;
85+
exitMenuAction.action.performed -= OnExitMenu;
86+
}
6287
}
6388
}

Assets/Samples/RebindingUI/RebindUIPrefab.prefab

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ MonoBehaviour:
9898
m_SelectOnRight: {fileID: 0}
9999
m_Transition: 1
100100
m_Colors:
101-
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
102-
m_HighlightedColor: {r: 0.7882353, g: 0.9019608, b: 1, a: 1}
101+
m_NormalColor: {r: 0.25, g: 0.3071429, b: 0.45, a: 1}
102+
m_HighlightedColor: {r: 0.36173913, g: 0.4408696, b: 0.65, a: 1}
103103
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
104-
m_SelectedColor: {r: 0.078431375, g: 0.64705884, b: 1, a: 1}
104+
m_SelectedColor: {r: 0.8679245, g: 0.4550042, b: 0.045033824, a: 1}
105105
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
106106
m_ColorMultiplier: 1
107107
m_FadeDuration: 0.1
@@ -191,7 +191,7 @@ MonoBehaviour:
191191
m_Name:
192192
m_EditorClassIdentifier:
193193
m_Material: {fileID: 0}
194-
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
194+
m_Color: {r: 0.8901961, g: 0.8901961, b: 0.8901961, a: 1}
195195
m_RaycastTarget: 1
196196
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
197197
m_Maskable: 1
@@ -270,7 +270,7 @@ MonoBehaviour:
270270
m_Name:
271271
m_EditorClassIdentifier:
272272
m_Material: {fileID: 0}
273-
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
273+
m_Color: {r: 0.8901961, g: 0.8901961, b: 0.8901961, a: 1}
274274
m_RaycastTarget: 1
275275
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
276276
m_Maskable: 1
@@ -457,7 +457,7 @@ MonoBehaviour:
457457
m_Name:
458458
m_EditorClassIdentifier:
459459
m_Material: {fileID: 0}
460-
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
460+
m_Color: {r: 0.8901961, g: 0.8901961, b: 0.8901961, a: 1}
461461
m_RaycastTarget: 1
462462
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
463463
m_Maskable: 1
@@ -467,7 +467,7 @@ MonoBehaviour:
467467
m_FontData:
468468
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
469469
m_FontSize: 14
470-
m_FontStyle: 0
470+
m_FontStyle: 1
471471
m_BestFit: 0
472472
m_MinSize: 10
473473
m_MaxSize: 40
@@ -576,10 +576,10 @@ MonoBehaviour:
576576
m_SelectOnRight: {fileID: 0}
577577
m_Transition: 1
578578
m_Colors:
579-
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
580-
m_HighlightedColor: {r: 0.7877358, g: 0.89941245, b: 1, a: 1}
579+
m_NormalColor: {r: 0.2509804, g: 0.30588236, b: 0.4509804, a: 1}
580+
m_HighlightedColor: {r: 0.36078432, g: 0.4392157, b: 0.6509804, a: 1}
581581
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
582-
m_SelectedColor: {r: 0.078431375, g: 0.64705884, b: 1, a: 1}
582+
m_SelectedColor: {r: 0.8666667, g: 0.45490196, b: 0.043137256, a: 1}
583583
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
584584
m_ColorMultiplier: 1
585585
m_FadeDuration: 0.1

Assets/Samples/RebindingUI/RebindingMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Material:
7979
- _UVSec: 0
8080
- _ZWrite: 1
8181
m_Colors:
82-
- _Color: {r: 0.20000027, g: 0.20000029, b: 0.99999887, a: 1}
82+
- _Color: {r: 1, g: 0, b: 0, a: 1}
8383
- _EmissionColor: {r: 0.27450982, g: 0.078431375, b: 0.19607843, a: 1}
8484
m_BuildTextureStacks: []
8585
m_AllowLocking: 1

0 commit comments

Comments
 (0)