Skip to content

Commit cbf4058

Browse files
authored
FIX: Prefabs and missing default control scheme in the inspector view of playerinput (ISXB-818) (#1932)
* Fixed missing default control scheme in the inspector view of playerinput * Fixed inspector of PlayerInput component to handle prefabs
1 parent 3adb0e1 commit cbf4058

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ however, it has to be formatted properly to pass verification tests.
1515
- Fixed an issue where a composite binding would not be consecutively triggered after ResetDevice() has been called from the associated action handler [ISXB-746](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-746).
1616
- Fixed resource designation for "d_InputControl" icon to address CI failure.
1717
- Fixed an issue where a composite binding would not be consecutively triggered after disabling actions while there are action modifiers in progress [ISXB-505](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-505).
18+
- Fixed prefabs and missing default control scheme used by PlayerInput component are now correctly shown in the inspector [ISXB-818](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-818)
1819

1920
## [1.8.2] - 2024-04-29
2021

Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/PlayerInputEditor.cs

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,42 @@ public override void OnInspectorGUI()
8484
if (m_ControlSchemeOptions != null && m_ControlSchemeOptions.Length > 1) // Don't show if <Any> is the only option.
8585
{
8686
// Default control scheme picker.
87-
88-
var selected = EditorGUILayout.Popup(m_DefaultControlSchemeText, m_SelectedDefaultControlScheme,
89-
m_ControlSchemeOptions);
87+
Color currentBg = GUI.backgroundColor;
88+
// if the invalid DefaultControlSchemeName is selected set the popup draw the BG color in red
89+
if (m_InvalidDefaultControlSchemeName != null && m_SelectedDefaultControlScheme == 1)
90+
GUI.backgroundColor = Color.red;
91+
92+
var rect = EditorGUILayout.GetControlRect();
93+
var label = EditorGUI.BeginProperty(rect, m_DefaultControlSchemeText, m_DefaultControlSchemeProperty);
94+
var selected = EditorGUI.Popup(rect, label, m_SelectedDefaultControlScheme, m_ControlSchemeOptions);
95+
EditorGUI.EndProperty();
9096
if (selected != m_SelectedDefaultControlScheme)
9197
{
9298
if (selected == 0)
9399
{
94100
m_DefaultControlSchemeProperty.stringValue = null;
95101
}
102+
// if there is an invalid default scheme name it will be at rank 1.
103+
// we use m_InvalidDefaultControlSchemeName to prevent usage of the string with "name<Not Found>"
104+
else if (m_InvalidDefaultControlSchemeName != null && selected == 1)
105+
{
106+
m_DefaultControlSchemeProperty.stringValue = m_InvalidDefaultControlSchemeName;
107+
}
96108
else
97109
{
98-
m_DefaultControlSchemeProperty.stringValue =
99-
m_ControlSchemeOptions[selected].text;
110+
m_DefaultControlSchemeProperty.stringValue = m_ControlSchemeOptions[selected].text;
100111
}
101112
m_SelectedDefaultControlScheme = selected;
102113
}
114+
// Restore the initial color
115+
GUI.backgroundColor = currentBg;
103116

117+
118+
rect = EditorGUILayout.GetControlRect();
119+
label = EditorGUI.BeginProperty(rect, m_AutoSwitchText, m_NeverAutoSwitchControlSchemesProperty);
104120
var neverAutoSwitchValueOld = m_NeverAutoSwitchControlSchemesProperty.boolValue;
105-
var neverAutoSwitchValueNew = !EditorGUILayout.Toggle(m_AutoSwitchText, !neverAutoSwitchValueOld);
121+
var neverAutoSwitchValueNew = !EditorGUI.Toggle(rect, label, !neverAutoSwitchValueOld);
122+
EditorGUI.EndProperty();
106123
if (neverAutoSwitchValueOld != neverAutoSwitchValueNew)
107124
{
108125
m_NeverAutoSwitchControlSchemesProperty.boolValue = neverAutoSwitchValueNew;
@@ -112,9 +129,11 @@ public override void OnInspectorGUI()
112129
if (m_ActionMapOptions != null && m_ActionMapOptions.Length > 0)
113130
{
114131
// Default action map picker.
115-
116-
var selected = EditorGUILayout.Popup(m_DefaultActionMapText, m_SelectedDefaultActionMap,
132+
var rect = EditorGUILayout.GetControlRect();
133+
var label = EditorGUI.BeginProperty(rect, m_DefaultActionMapText, m_DefaultActionMapProperty);
134+
var selected = EditorGUI.Popup(rect, label, m_SelectedDefaultActionMap,
117135
m_ActionMapOptions);
136+
EditorGUI.EndProperty();
118137
if (selected != m_SelectedDefaultActionMap)
119138
{
120139
if (selected == 0)
@@ -424,6 +443,7 @@ private void OnActionAssetChange()
424443
m_ActionNames = null;
425444
m_SelectedDefaultActionMap = -1;
426445
m_SelectedDefaultControlScheme = -1;
446+
m_InvalidDefaultControlSchemeName = null;
427447
return;
428448
}
429449

@@ -486,22 +506,36 @@ void AddEntry(InputAction action, PlayerInput.ActionEvent actionEvent)
486506

487507
// Read out control schemes.
488508
var selectedDefaultControlScheme = playerInput.defaultControlScheme;
509+
m_InvalidDefaultControlSchemeName = null;
489510
m_SelectedDefaultControlScheme = 0;
490-
var controlSchemes = asset.controlSchemes;
491-
m_ControlSchemeOptions = new GUIContent[controlSchemes.Count + 1];
492-
m_ControlSchemeOptions[0] = new GUIContent(EditorGUIUtility.TrTextContent("<Any>"));
493-
////TODO: sort alphabetically
494-
for (var i = 0; i < controlSchemes.Count; ++i)
495-
{
496-
var name = controlSchemes[i].name;
497-
m_ControlSchemeOptions[i + 1] = new GUIContent(name);
511+
////TODO: sort alphabetically and ensure that the order is the same in the schemes editor
512+
var controlSchemesNames = asset.controlSchemes.Select(cs => cs.name).ToList();
498513

499-
if (selectedDefaultControlScheme != null && string.Compare(name, selectedDefaultControlScheme,
500-
StringComparison.InvariantCultureIgnoreCase) == 0)
501-
m_SelectedDefaultControlScheme = i + 1;
514+
// try to find the selected Default Control Scheme
515+
if (!string.IsNullOrEmpty(selectedDefaultControlScheme))
516+
{
517+
// +1 since <Any> will be the first in the list
518+
m_SelectedDefaultControlScheme = 1 + controlSchemesNames.FindIndex(name => string.Compare(name, selectedDefaultControlScheme,
519+
StringComparison.InvariantCultureIgnoreCase) == 0);
520+
// if not found, will insert the invalid name next to <Any>
521+
if (m_SelectedDefaultControlScheme == 0)
522+
{
523+
m_InvalidDefaultControlSchemeName = selectedDefaultControlScheme;
524+
m_SelectedDefaultControlScheme = 1;
525+
controlSchemesNames.Insert(0, $"{selectedDefaultControlScheme}{L10n.Tr("<Not Found>")}");
526+
}
502527
}
503-
if (m_SelectedDefaultControlScheme <= 0)
528+
else
529+
{
504530
playerInput.defaultControlScheme = null;
531+
}
532+
533+
m_ControlSchemeOptions = new GUIContent[controlSchemesNames.Count + 1];
534+
m_ControlSchemeOptions[0] = new GUIContent(EditorGUIUtility.TrTextContent("<Any>"));
535+
for (var i = 0; i < controlSchemesNames.Count; ++i)
536+
{
537+
m_ControlSchemeOptions[i + 1] = new GUIContent(controlSchemesNames[i]);
538+
}
505539

506540
// Read out action maps.
507541
var selectedDefaultActionMap = !string.IsNullOrEmpty(playerInput.defaultActionMap)
@@ -562,6 +596,7 @@ void AddEntry(InputAction action, PlayerInput.ActionEvent actionEvent)
562596
[NonSerialized] private int[] m_ActionMapIndices;
563597
[NonSerialized] private int m_NumActionMaps;
564598
[NonSerialized] private int m_SelectedDefaultControlScheme;
599+
[NonSerialized] private string m_InvalidDefaultControlSchemeName;
565600
[NonSerialized] private GUIContent[] m_ControlSchemeOptions;
566601
[NonSerialized] private int m_SelectedDefaultActionMap;
567602
[NonSerialized] private GUIContent[] m_ActionMapOptions;

0 commit comments

Comments
 (0)