Skip to content

Commit ff2ac9d

Browse files
FIX: GamepadButton.LeftTrigger and GamepadButton.RightTrigger enum resolving to wrong integers (ISXB-1270) (#2100)
1 parent f156ad3 commit ff2ac9d

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ however, it has to be formatted properly to pass verification tests.
2020
## [1.12.0] - 2025-01-15
2121

2222
### Fixed
23+
- Fixed GamepadButton.LeftTrigger and GamepadButton.RightTrigger enum values not matching displayed dropdown values in editor when using GamepadButtonPropertyDrawer [ISXB-1270](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1270).
2324
- Fixed an issue causing the Action context menu to not show on right click when right clicking an action in the Input Action Editor [ISXB-1134](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1134).
2425
- Reverted changes from 0ddd534d8 (ISXB-746) which introduced a regression [ISXB-1127](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1127).
2526
- Fixed `ArgumentNullException: Value cannot be null.` during the migration of Project-wide Input Actions from `InputManager.asset` to `InputSystem_Actions.inputactions` asset which lead do the lost of the configuration [ISXB-1105](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1105).

Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
#if UNITY_EDITOR
2-
31
using System;
42
using System.Collections.Generic;
5-
using System.Linq;
63
using UnityEngine.InputSystem.LowLevel;
74
using UnityEditor;
85
using UnityEngine.UIElements;
96

7+
#if UNITY_EDITOR
108
namespace UnityEngine.InputSystem.Editor
119
{
1210
/// <summary>
@@ -32,17 +30,17 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
3230

3331
if (property.propertyType == SerializedPropertyType.Enum)
3432
{
35-
property.intValue = EditorGUI.Popup(position, label.text, property.intValue, m_EnumDisplayNames);
33+
property.intValue = m_EnumValues[EditorGUI.Popup(position, label.text, GetEnumIndex(property.intValue), m_EnumDisplayNames)];
3634
}
3735

3836
EditorGUI.EndProperty();
3937
}
4038

4139
private void CreateEnumList()
4240
{
43-
var enumNamesAndValues = new Dictionary<string, int>();
44-
var enumDisplayNames = Enum.GetNames(typeof(GamepadButton));
45-
var enumValues = Enum.GetValues(typeof(GamepadButton)).Cast<GamepadButton>().ToArray();
41+
string[] enumDisplayNames = Enum.GetNames(typeof(GamepadButton));
42+
var enumValues = Enum.GetValues(typeof(GamepadButton));
43+
var enumNamesAndValues = new Dictionary<string, int>(enumDisplayNames.Length);
4644

4745
for (var i = 0; i < enumDisplayNames.Length; ++i)
4846
{
@@ -76,12 +74,36 @@ private void CreateEnumList()
7674
}
7775
enumNamesAndValues.Add(enumName, (int)enumValues.GetValue(i));
7876
}
79-
var sortedEntries = enumNamesAndValues.OrderBy(x => x.Value);
77+
SetEnumDisplayNames(enumNamesAndValues);
78+
}
79+
80+
// Sorts the values so that they get displayed consistently, and assigns them for being drawn.
81+
private void SetEnumDisplayNames(Dictionary<string, int> enumNamesAndValues)
82+
{
83+
m_EnumValues = new int[enumNamesAndValues.Count];
84+
enumNamesAndValues.Values.CopyTo(m_EnumValues, 0);
85+
86+
m_EnumDisplayNames = new string[enumNamesAndValues.Count];
87+
enumNamesAndValues.Keys.CopyTo(m_EnumDisplayNames, 0);
8088

81-
m_EnumDisplayNames = sortedEntries.Select(x => x.Key).ToArray();
89+
Array.Sort(m_EnumValues, m_EnumDisplayNames);
90+
}
91+
92+
// Ensures mapping between displayed value and actual value is consistent. Issues arise when there are gaps in the enum values (ie 0, 1, 13).
93+
private int GetEnumIndex(int enumValue)
94+
{
95+
for (int i = 0; i < m_EnumValues.Length; i++)
96+
{
97+
if (enumValue == m_EnumValues[i])
98+
{
99+
return i;
100+
}
101+
}
102+
return 0;
82103
}
83104

105+
private int[] m_EnumValues;
84106
private string[] m_EnumDisplayNames;
85107
}
86108
}
87-
#endif // UNITY_EDITOR
109+
#endif // UNITY_EDITOR

0 commit comments

Comments
 (0)