Skip to content

Commit f98f4ec

Browse files
committed
Minor tweaks to rebinding sample and added support for showing disabled state to indicators
1 parent f3fb1f7 commit f98f4ec

File tree

3 files changed

+190
-22
lines changed

3 files changed

+190
-22
lines changed

Assets/Samples/RebindingUI/InputActionIndicator.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using UnityEngine;
3-
using UnityEngine.InputSystem;
42
using UnityEngine.UI;
53

64
namespace UnityEngine.InputSystem.Samples.RebindUI
@@ -20,16 +18,21 @@ public class InputActionIndicator : MonoBehaviour
2018

2119
[Tooltip("The color to show when the associated action has not been performed for the specified duration.")]
2220
public Color inactiveColor = Color.black;
21+
22+
[Tooltip("The color to show when the associated action is disabled")]
23+
public Color disabledColor = Color.red;
2324

2425
[Tooltip("The duration for which the indicator should be lit before becoming completely inactive.")]
2526
public float duration = 1.0f;
2627

2728
private double m_RealTimeLastPerformed;
2829
private Image m_Image;
30+
private Text m_Text;
2931

3032
void Awake()
3133
{
3234
m_Image = GetComponent<Image>();
35+
m_Text = GetComponent<Text>();
3336
Update();
3437
}
3538

@@ -52,11 +55,40 @@ private void OnPerformed(InputAction.CallbackContext obj)
5255

5356
private void Update()
5457
{
55-
var elapsedSincePerformed = Time.realtimeSinceStartupAsDouble - m_RealTimeLastPerformed;
56-
m_Image.color = duration <= 0.0f
57-
? inactiveColor
58-
: Color.Lerp(inactiveColor, activeColor,
59-
(float)Math.Max(0.0, 1.0 - elapsedSincePerformed / duration));
58+
if (action.action.enabled)
59+
{
60+
// Pulse active color if enabled
61+
var elapsedSincePerformed = Time.realtimeSinceStartupAsDouble - m_RealTimeLastPerformed;
62+
m_Image.color = duration <= 0.0f
63+
? inactiveColor
64+
: Color.Lerp(inactiveColor, activeColor,
65+
(float)Math.Max(0.0, 1.0 - elapsedSincePerformed / duration));
66+
}
67+
else
68+
{
69+
// Show disabled indicator if disabled
70+
if (m_Image.color != disabledColor)
71+
m_Image.color = disabledColor;
72+
}
73+
}
74+
75+
// We want the label for the action name to update in edit mode, too, so
76+
// we kick that off from here.
77+
#if UNITY_EDITOR
78+
protected void OnValidate()
79+
{
80+
UpdateActionLabel();
81+
}
82+
#endif
83+
84+
private void UpdateActionLabel()
85+
{
86+
if (m_Text == null)
87+
return;
88+
if (action != null && action.action != null)
89+
m_Text.text = action.name;
90+
else
91+
m_Text.text = string.Empty;
6092
}
6193
}
6294
}

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: 1, g: 0, b: 0, a: 1}
82+
- _Color: {r: 0.99999994, g: 1e-45, b: 1e-45, a: 1}
8383
- _EmissionColor: {r: 0.27450982, g: 0.078431375, b: 0.19607843, a: 1}
8484
m_BuildTextureStacks: []
8585
m_AllowLocking: 1

Assets/Samples/RebindingUI/RebindingUISampleScene.unity

Lines changed: 150 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,9 +2141,9 @@ MonoBehaviour:
21412141
m_HorizontalOverflow: 0
21422142
m_VerticalOverflow: 0
21432143
m_LineSpacing: 1
2144-
m_Text: Basic rebinding menu with the left side rebinding the <b>Keyboard & Mouse</b>
2145-
control scheme and the right side rebinding the <b>Gamepad</b> scheme. Note that
2146-
actions triggering are indicated at the bottom of the screen.
2144+
m_Text: "Basic rebinding menu that allows rebinding of the <b>Keyboard & Mouse</b>
2145+
and <b>Gamepad</b> control schemes. \nNote that actions triggering are indicated
2146+
at the bottom of the screen also when the menu is visible."
21472147
--- !u!222 &607293546
21482148
CanvasRenderer:
21492149
m_ObjectHideFlags: 0
@@ -3102,8 +3102,8 @@ RectTransform:
31023102
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
31033103
m_AnchorMin: {x: 0.5, y: 1}
31043104
m_AnchorMax: {x: 0.5, y: 1}
3105-
m_AnchoredPosition: {x: 0, y: -87}
3106-
m_SizeDelta: {x: 400, y: 2}
3105+
m_AnchoredPosition: {x: 51.43, y: -87}
3106+
m_SizeDelta: {x: 460, y: 2}
31073107
m_Pivot: {x: 0.5, y: 0.5}
31083108
--- !u!114 &862809804
31093109
MonoBehaviour:
@@ -3222,6 +3222,141 @@ CanvasRenderer:
32223222
m_PrefabAsset: {fileID: 0}
32233223
m_GameObject: {fileID: 899779638}
32243224
m_CullTransparentMesh: 0
3225+
--- !u!1001 &920225868
3226+
PrefabInstance:
3227+
m_ObjectHideFlags: 0
3228+
serializedVersion: 2
3229+
m_Modification:
3230+
serializedVersion: 3
3231+
m_TransformParent: {fileID: 1592902109}
3232+
m_Modifications:
3233+
- target: {fileID: 1255350527062619510, guid: 0648a5c163cef41038b804a34b4b80e7,
3234+
type: 3}
3235+
propertyPath: m_Name
3236+
value: MenuIndicator
3237+
objectReference: {fileID: 0}
3238+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3239+
type: 3}
3240+
propertyPath: m_Pivot.x
3241+
value: 0.5
3242+
objectReference: {fileID: 0}
3243+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3244+
type: 3}
3245+
propertyPath: m_Pivot.y
3246+
value: 0.5
3247+
objectReference: {fileID: 0}
3248+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3249+
type: 3}
3250+
propertyPath: m_AnchorMax.x
3251+
value: 0.5
3252+
objectReference: {fileID: 0}
3253+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3254+
type: 3}
3255+
propertyPath: m_AnchorMax.y
3256+
value: 0
3257+
objectReference: {fileID: 0}
3258+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3259+
type: 3}
3260+
propertyPath: m_AnchorMin.x
3261+
value: 0.5
3262+
objectReference: {fileID: 0}
3263+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3264+
type: 3}
3265+
propertyPath: m_AnchorMin.y
3266+
value: 0
3267+
objectReference: {fileID: 0}
3268+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3269+
type: 3}
3270+
propertyPath: m_SizeDelta.x
3271+
value: 30
3272+
objectReference: {fileID: 0}
3273+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3274+
type: 3}
3275+
propertyPath: m_SizeDelta.y
3276+
value: 30
3277+
objectReference: {fileID: 0}
3278+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3279+
type: 3}
3280+
propertyPath: m_LocalPosition.x
3281+
value: 0
3282+
objectReference: {fileID: 0}
3283+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3284+
type: 3}
3285+
propertyPath: m_LocalPosition.y
3286+
value: 0
3287+
objectReference: {fileID: 0}
3288+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3289+
type: 3}
3290+
propertyPath: m_LocalPosition.z
3291+
value: 0
3292+
objectReference: {fileID: 0}
3293+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3294+
type: 3}
3295+
propertyPath: m_LocalRotation.w
3296+
value: 1
3297+
objectReference: {fileID: 0}
3298+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3299+
type: 3}
3300+
propertyPath: m_LocalRotation.x
3301+
value: -0
3302+
objectReference: {fileID: 0}
3303+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3304+
type: 3}
3305+
propertyPath: m_LocalRotation.y
3306+
value: -0
3307+
objectReference: {fileID: 0}
3308+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3309+
type: 3}
3310+
propertyPath: m_LocalRotation.z
3311+
value: -0
3312+
objectReference: {fileID: 0}
3313+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3314+
type: 3}
3315+
propertyPath: m_AnchoredPosition.x
3316+
value: 250
3317+
objectReference: {fileID: 0}
3318+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3319+
type: 3}
3320+
propertyPath: m_AnchoredPosition.y
3321+
value: 40
3322+
objectReference: {fileID: 0}
3323+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3324+
type: 3}
3325+
propertyPath: m_LocalEulerAnglesHint.x
3326+
value: 0
3327+
objectReference: {fileID: 0}
3328+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3329+
type: 3}
3330+
propertyPath: m_LocalEulerAnglesHint.y
3331+
value: 0
3332+
objectReference: {fileID: 0}
3333+
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3334+
type: 3}
3335+
propertyPath: m_LocalEulerAnglesHint.z
3336+
value: 0
3337+
objectReference: {fileID: 0}
3338+
- target: {fileID: 4072204658071074964, guid: 0648a5c163cef41038b804a34b4b80e7,
3339+
type: 3}
3340+
propertyPath: m_Text
3341+
value: Menu
3342+
objectReference: {fileID: 0}
3343+
- target: {fileID: 8586258032687207620, guid: 0648a5c163cef41038b804a34b4b80e7,
3344+
type: 3}
3345+
propertyPath: action
3346+
value:
3347+
objectReference: {fileID: -2195523558945053078, guid: 7dead05c54ca85b4681351aafd8bd03a,
3348+
type: 3}
3349+
m_RemovedComponents: []
3350+
m_RemovedGameObjects: []
3351+
m_AddedGameObjects: []
3352+
m_AddedComponents: []
3353+
m_SourcePrefab: {fileID: 100100000, guid: 0648a5c163cef41038b804a34b4b80e7, type: 3}
3354+
--- !u!224 &920225869 stripped
3355+
RectTransform:
3356+
m_CorrespondingSourceObject: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
3357+
type: 3}
3358+
m_PrefabInstance: {fileID: 920225868}
3359+
m_PrefabAsset: {fileID: 0}
32253360
--- !u!1001 &982377775
32263361
PrefabInstance:
32273362
m_ObjectHideFlags: 0
@@ -3912,7 +4047,7 @@ PrefabInstance:
39124047
- target: {fileID: 1255350527062619510, guid: 0648a5c163cef41038b804a34b4b80e7,
39134048
type: 3}
39144049
propertyPath: m_Name
3915-
value: RebindUIActionIndicator
4050+
value: UseIndicator
39164051
objectReference: {fileID: 0}
39174052
- target: {fileID: 1777307397533242038, guid: 0648a5c163cef41038b804a34b4b80e7,
39184053
type: 3}
@@ -4325,8 +4460,8 @@ RectTransform:
43254460
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
43264461
m_AnchorMin: {x: 0.5, y: 1}
43274462
m_AnchorMax: {x: 0.5, y: 1}
4328-
m_AnchoredPosition: {x: 0, y: -87}
4329-
m_SizeDelta: {x: 400, y: 2}
4463+
m_AnchoredPosition: {x: 0.6000061, y: -87}
4464+
m_SizeDelta: {x: 289.6, y: 2}
43304465
m_Pivot: {x: 0.5, y: 0.5}
43314466
--- !u!114 &1207403195
43324467
MonoBehaviour:
@@ -4400,8 +4535,8 @@ RectTransform:
44004535
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
44014536
m_AnchorMin: {x: 0.5, y: 1}
44024537
m_AnchorMax: {x: 0.5, y: 1}
4403-
m_AnchoredPosition: {x: 0, y: -69.1}
4404-
m_SizeDelta: {x: 573.5, y: 31.700012}
4538+
m_AnchoredPosition: {x: 63.5, y: -69.1}
4539+
m_SizeDelta: {x: 534.5, y: 31.700012}
44054540
m_Pivot: {x: 0.5, y: 0.5}
44064541
--- !u!114 &1208536040
44074542
MonoBehaviour:
@@ -4436,7 +4571,7 @@ MonoBehaviour:
44364571
m_HorizontalOverflow: 0
44374572
m_VerticalOverflow: 0
44384573
m_LineSpacing: 1
4439-
m_Text: Actions
4574+
m_Text: Gameplay & Common Actions
44404575
--- !u!222 &1208536041
44414576
CanvasRenderer:
44424577
m_ObjectHideFlags: 0
@@ -4597,9 +4732,9 @@ MonoBehaviour:
45974732
m_HorizontalOverflow: 0
45984733
m_VerticalOverflow: 1
45994734
m_LineSpacing: 1
4600-
m_Text: It is up to you whether to do something like WASD as a single composite
4601-
like above (in which case the individual parts will get polled one after the
4602-
other) or as four separate part bindings.
4735+
m_Text: It is up to you whether to do somthing like e.g. WASD as a single composite
4736+
like to the right (in which case the individual parts will get assigned one after
4737+
the other) or as four separate partial bindings.
46034738
--- !u!222 &1237134520
46044739
CanvasRenderer:
46054740
m_ObjectHideFlags: 0
@@ -4728,6 +4863,7 @@ RectTransform:
47284863
- {fileID: 1653112498}
47294864
- {fileID: 113688689}
47304865
- {fileID: 1125279441}
4866+
- {fileID: 920225869}
47314867
- {fileID: 862809803}
47324868
m_Father: {fileID: 861395295}
47334869
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

0 commit comments

Comments
 (0)