Skip to content

Commit fe3e1ee

Browse files
Fixed GamepadButton.LeftTrigger and GamepadButton.RightTrigger enum values not matching displayed dropdown values in editor when using GamepadButtonPropertyDrawer.
1 parent 6b042c2 commit fe3e1ee

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ however, it has to be formatted properly to pass verification tests.
1111
## [Unreleased] - yyyy-mm-dd
1212

1313
### Fixed
14+
- 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).
1415
- 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).
1516
- Reverted changes from 0ddd534d8 (ISXB-746) which introduced a regression [ISXB-1127](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1127).
1617
- 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: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
#if UNITY_EDITOR
2-
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using UnityEngine.InputSystem.LowLevel;
7-
using UnityEditor;
8-
using UnityEngine.UIElements;
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine.InputSystem.LowLevel;
4+
using UnityEditor;
5+
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,53 @@ 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+
private void SetEnumDisplayNames(Dictionary<string, int> enumNamesAndValues)
81+
{
82+
m_EnumValues = new int[enumNamesAndValues.Count];
83+
84+
m_EnumDisplayNames = new string[enumNamesAndValues.Count];
85+
int currentIndex = 0;
86+
87+
int tempInt;
88+
string tempString;
8089

81-
m_EnumDisplayNames = sortedEntries.Select(x => x.Key).ToArray();
90+
foreach (KeyValuePair<string, int> kvp in enumNamesAndValues)
91+
{
92+
int tempIndex = currentIndex;
93+
m_EnumDisplayNames[currentIndex] = kvp.Key;
94+
m_EnumValues[currentIndex] = kvp.Value;
95+
while (tempIndex != 0 && m_EnumValues[currentIndex] < m_EnumValues[tempIndex - 1])
96+
{
97+
tempInt = m_EnumValues[tempIndex - 1];
98+
m_EnumValues[tempIndex - 1] = m_EnumValues[currentIndex];
99+
m_EnumValues[currentIndex] = tempInt;
100+
101+
tempString = m_EnumDisplayNames[tempIndex - 1];
102+
m_EnumDisplayNames[tempIndex - 1] = m_EnumDisplayNames[currentIndex];
103+
m_EnumDisplayNames[currentIndex] = tempString;
104+
tempIndex--;
105+
}
106+
currentIndex++;
107+
}
108+
}
109+
110+
private int GetEnumIndex(int enumValue)
111+
{
112+
for (int i = 0; i<m_EnumValues.Length; i++)
113+
{
114+
if (enumValue == m_EnumValues[i])
115+
{
116+
return i;
117+
}
118+
}
119+
return 0;
82120
}
83121

122+
private int[] m_EnumValues;
84123
private string[] m_EnumDisplayNames;
85-
}
86-
}
87-
#endif // UNITY_EDITOR
124+
}
125+
}
126+
#endif // UNITY_EDITOR

0 commit comments

Comments
 (0)