From fe3e1ee54d825f0d005de9c7fa7a193419d2a739 Mon Sep 17 00:00:00 2001 From: Adrian Koretski Date: Mon, 23 Dec 2024 16:04:00 -0500 Subject: [PATCH 1/3] Fixed GamepadButton.LeftTrigger and GamepadButton.RightTrigger enum values not matching displayed dropdown values in editor when using GamepadButtonPropertyDrawer. --- Packages/com.unity.inputsystem/CHANGELOG.md | 1 + .../GamepadButtonPropertyDrawer.cs | 73 ++++++++++++++----- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 7d11b796b5..e9d0a4fd0c 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -11,6 +11,7 @@ however, it has to be formatted properly to pass verification tests. ## [Unreleased] - yyyy-mm-dd ### 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..e59bf5ca4d 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; + using System; + using System.Collections.Generic; + 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,53 @@ private void CreateEnumList() } enumNamesAndValues.Add(enumName, (int)enumValues.GetValue(i)); } - var sortedEntries = enumNamesAndValues.OrderBy(x => x.Value); + SetEnumDisplayNames(enumNamesAndValues); + } + + private void SetEnumDisplayNames(Dictionary enumNamesAndValues) + { + m_EnumValues = new int[enumNamesAndValues.Count]; + + m_EnumDisplayNames = new string[enumNamesAndValues.Count]; + int currentIndex = 0; + + int tempInt; + string tempString; - m_EnumDisplayNames = sortedEntries.Select(x => x.Key).ToArray(); + foreach (KeyValuePair kvp in enumNamesAndValues) + { + int tempIndex = currentIndex; + m_EnumDisplayNames[currentIndex] = kvp.Key; + m_EnumValues[currentIndex] = kvp.Value; + while (tempIndex != 0 && m_EnumValues[currentIndex] < m_EnumValues[tempIndex - 1]) + { + tempInt = m_EnumValues[tempIndex - 1]; + m_EnumValues[tempIndex - 1] = m_EnumValues[currentIndex]; + m_EnumValues[currentIndex] = tempInt; + + tempString = m_EnumDisplayNames[tempIndex - 1]; + m_EnumDisplayNames[tempIndex - 1] = m_EnumDisplayNames[currentIndex]; + m_EnumDisplayNames[currentIndex] = tempString; + tempIndex--; + } + currentIndex++; + } + } + + private int GetEnumIndex(int enumValue) + { + for (int i = 0; i Date: Tue, 21 Jan 2025 12:15:11 -0500 Subject: [PATCH 2/3] Simplified sorting system for GamepadButtonPropertyDrawer and added a few clarifying comments. --- .../GamepadButtonPropertyDrawer.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs index e59bf5ca4d..9697041651 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs @@ -77,36 +77,19 @@ private void CreateEnumList() 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]; - int currentIndex = 0; + enumNamesAndValues.Keys.CopyTo(m_EnumDisplayNames, 0); - int tempInt; - string tempString; - - foreach (KeyValuePair kvp in enumNamesAndValues) - { - int tempIndex = currentIndex; - m_EnumDisplayNames[currentIndex] = kvp.Key; - m_EnumValues[currentIndex] = kvp.Value; - while (tempIndex != 0 && m_EnumValues[currentIndex] < m_EnumValues[tempIndex - 1]) - { - tempInt = m_EnumValues[tempIndex - 1]; - m_EnumValues[tempIndex - 1] = m_EnumValues[currentIndex]; - m_EnumValues[currentIndex] = tempInt; - - tempString = m_EnumDisplayNames[tempIndex - 1]; - m_EnumDisplayNames[tempIndex - 1] = m_EnumDisplayNames[currentIndex]; - m_EnumDisplayNames[currentIndex] = tempString; - tempIndex--; - } - currentIndex++; - } + 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 Date: Wed, 22 Jan 2025 11:29:14 -0500 Subject: [PATCH 3/3] Fixed formatting in GamepadButtonPropertyDrawer.cs --- .../GamepadButtonPropertyDrawer.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs index 9697041651..39a41f311a 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/PropertyDrawers/GamepadButtonPropertyDrawer.cs @@ -1,8 +1,8 @@ - using System; - using System.Collections.Generic; - using UnityEngine.InputSystem.LowLevel; - using UnityEditor; - using UnityEngine.UIElements; +using System; +using System.Collections.Generic; +using UnityEngine.InputSystem.LowLevel; +using UnityEditor; +using UnityEngine.UIElements; #if UNITY_EDITOR namespace UnityEngine.InputSystem.Editor @@ -77,7 +77,7 @@ private void CreateEnumList() SetEnumDisplayNames(enumNamesAndValues); } - // Sorts the values so that they get displayed consistently, and assigns them for being drawn. + // 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]; @@ -89,10 +89,10 @@ private void SetEnumDisplayNames(Dictionary enumNamesAndValues) 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). + // 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