-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathInputActionIndicator.cs
More file actions
99 lines (84 loc) · 3.25 KB
/
InputActionIndicator.cs
File metadata and controls
99 lines (84 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using UnityEngine.UI;
namespace UnityEngine.InputSystem.Samples.RebindUI
{
/// <summary>
/// A simple visual indicator of action performed.
/// </summary>
/// <remarks>Error handling have been excluded for simplicity.</remarks>
[RequireComponent(typeof(Image))]
public class InputActionIndicator : MonoBehaviour
{
[Tooltip("Reference to the associated action to be visualized.")]
public InputActionReference action;
[Tooltip("The color to show when the associated action is performed.")]
public Color activeColor = Color.green;
[Tooltip("The color to show when the associated action has not been performed for the specified duration.")]
public Color inactiveColor = Color.black;
[Tooltip("The color to show when the associated action is disabled")]
public Color disabledColor = Color.red;
[Tooltip("The duration for which the indicator should be lit before becoming completely inactive.")]
public float duration = 1.0f;
public Image performedIndicator;
public Image pressedIndicator;
public Text label;
private double m_RealTimeLastPerformed;
private void OnEnable()
{
if (action != null && action.action != null)
{
action.action.performed += OnPerformed;
}
}
private void OnDisable()
{
if (action != null && action.action != null)
action.action.performed -= OnPerformed;
}
private void OnPerformed(InputAction.CallbackContext obj)
{
m_RealTimeLastPerformed = Time.realtimeSinceStartupAsDouble;
}
private void Update()
{
if (action.action.enabled)
{
// Pulse active color if enabled and performed
var elapsedSincePerformed = Time.realtimeSinceStartupAsDouble - m_RealTimeLastPerformed;
if (performedIndicator)
{
performedIndicator.color = duration <= 0.0f
? inactiveColor
: Color.Lerp(inactiveColor, activeColor,
(float)Math.Max(0.0, 1.0 - elapsedSincePerformed / duration));
}
if (pressedIndicator)
pressedIndicator.color = action.action.IsPressed() ? activeColor : inactiveColor;
}
else
{
// Show disabled indicator if disabled
if (performedIndicator && performedIndicator.color != disabledColor)
performedIndicator.color = disabledColor;
if (pressedIndicator && pressedIndicator.color != disabledColor)
pressedIndicator.color = disabledColor;
}
}
// Also update action label in edit-mode
#if UNITY_EDITOR
protected void OnValidate()
{
UpdateActionLabel();
}
#endif
private void UpdateActionLabel()
{
if (label == null)
return;
if (action != null && action.action != null)
label.text = action.action.name;
else
label.text = string.Empty;
}
}
}