diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index ebd1576f3c..3032159487 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -20,6 +20,7 @@ however, it has to be formatted properly to pass verification tests. ## [1.12.0] - 2025-01-15 ### Fixed +- 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). - 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). - Reverted changes from 0ddd534d8 (ISXB-746) which introduced a regression [ISXB-1127](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1127). - 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). diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs index 6475b6d400..39a41f311a 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs @@ -1,12 +1,10 @@ -#if UNITY_EDITOR - using System; using System.Collections.Generic; -using System.Linq; using UnityEngine.InputSystem.LowLevel; using UnityEditor; using UnityEngine.UIElements; +#if UNITY_EDITOR namespace UnityEngine.InputSystem.Editor { /// @@ -32,7 +30,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten if (property.propertyType == SerializedPropertyType.Enum) { - property.intValue = EditorGUI.Popup(position, label.text, property.intValue, m_EnumDisplayNames); + property.intValue = m_EnumValues[EditorGUI.Popup(position, label.text, GetEnumIndex(property.intValue), m_EnumDisplayNames)]; } EditorGUI.EndProperty(); @@ -40,9 +38,9 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten private void CreateEnumList() { - var enumNamesAndValues = new Dictionary(); - var enumDisplayNames = Enum.GetNames(typeof(GamepadButton)); - var enumValues = Enum.GetValues(typeof(GamepadButton)).Cast().ToArray(); + string[] enumDisplayNames = Enum.GetNames(typeof(GamepadButton)); + var enumValues = Enum.GetValues(typeof(GamepadButton)); + var enumNamesAndValues = new Dictionary(enumDisplayNames.Length); for (var i = 0; i < enumDisplayNames.Length; ++i) { @@ -76,12 +74,36 @@ private void CreateEnumList() } enumNamesAndValues.Add(enumName, (int)enumValues.GetValue(i)); } - var sortedEntries = enumNamesAndValues.OrderBy(x => x.Value); + SetEnumDisplayNames(enumNamesAndValues); + } + + // Sorts the values so that they get displayed consistently, and assigns them for being drawn. + private void SetEnumDisplayNames(Dictionary enumNamesAndValues) + { + m_EnumValues = new int[enumNamesAndValues.Count]; + enumNamesAndValues.Values.CopyTo(m_EnumValues, 0); + + m_EnumDisplayNames = new string[enumNamesAndValues.Count]; + enumNamesAndValues.Keys.CopyTo(m_EnumDisplayNames, 0); - m_EnumDisplayNames = sortedEntries.Select(x => x.Key).ToArray(); + Array.Sort(m_EnumValues, m_EnumDisplayNames); + } + + // Ensures mapping between displayed value and actual value is consistent. Issues arise when there are gaps in the enum values (ie 0, 1, 13). + private int GetEnumIndex(int enumValue) + { + for (int i = 0; i < m_EnumValues.Length; i++) + { + if (enumValue == m_EnumValues[i]) + { + return i; + } + } + return 0; } + private int[] m_EnumValues; private string[] m_EnumDisplayNames; } } -#endif // UNITY_EDITOR + #endif // UNITY_EDITOR