Skip to content

Commit

Permalink
FIX: Prefabs and missing default control scheme in the inspector view…
Browse files Browse the repository at this point in the history
… of playerinput (ISXB-818) (#1932)

* Fixed missing default control scheme in the inspector view of playerinput
* Fixed inspector of PlayerInput component to handle prefabs
  • Loading branch information
bmalrat authored May 27, 2024
1 parent 3adb0e1 commit cbf4058
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ however, it has to be formatted properly to pass verification tests.
- 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).
- Fixed resource designation for "d_InputControl" icon to address CI failure.
- 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).
- 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)

## [1.8.2] - 2024-04-29

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,42 @@ public override void OnInspectorGUI()
if (m_ControlSchemeOptions != null && m_ControlSchemeOptions.Length > 1) // Don't show if <Any> is the only option.
{
// Default control scheme picker.

var selected = EditorGUILayout.Popup(m_DefaultControlSchemeText, m_SelectedDefaultControlScheme,
m_ControlSchemeOptions);
Color currentBg = GUI.backgroundColor;
// if the invalid DefaultControlSchemeName is selected set the popup draw the BG color in red
if (m_InvalidDefaultControlSchemeName != null && m_SelectedDefaultControlScheme == 1)
GUI.backgroundColor = Color.red;

var rect = EditorGUILayout.GetControlRect();
var label = EditorGUI.BeginProperty(rect, m_DefaultControlSchemeText, m_DefaultControlSchemeProperty);
var selected = EditorGUI.Popup(rect, label, m_SelectedDefaultControlScheme, m_ControlSchemeOptions);
EditorGUI.EndProperty();
if (selected != m_SelectedDefaultControlScheme)
{
if (selected == 0)
{
m_DefaultControlSchemeProperty.stringValue = null;
}
// if there is an invalid default scheme name it will be at rank 1.
// we use m_InvalidDefaultControlSchemeName to prevent usage of the string with "name<Not Found>"
else if (m_InvalidDefaultControlSchemeName != null && selected == 1)
{
m_DefaultControlSchemeProperty.stringValue = m_InvalidDefaultControlSchemeName;
}
else
{
m_DefaultControlSchemeProperty.stringValue =
m_ControlSchemeOptions[selected].text;
m_DefaultControlSchemeProperty.stringValue = m_ControlSchemeOptions[selected].text;
}
m_SelectedDefaultControlScheme = selected;
}
// Restore the initial color
GUI.backgroundColor = currentBg;


rect = EditorGUILayout.GetControlRect();
label = EditorGUI.BeginProperty(rect, m_AutoSwitchText, m_NeverAutoSwitchControlSchemesProperty);
var neverAutoSwitchValueOld = m_NeverAutoSwitchControlSchemesProperty.boolValue;
var neverAutoSwitchValueNew = !EditorGUILayout.Toggle(m_AutoSwitchText, !neverAutoSwitchValueOld);
var neverAutoSwitchValueNew = !EditorGUI.Toggle(rect, label, !neverAutoSwitchValueOld);
EditorGUI.EndProperty();
if (neverAutoSwitchValueOld != neverAutoSwitchValueNew)
{
m_NeverAutoSwitchControlSchemesProperty.boolValue = neverAutoSwitchValueNew;
Expand All @@ -112,9 +129,11 @@ public override void OnInspectorGUI()
if (m_ActionMapOptions != null && m_ActionMapOptions.Length > 0)
{
// Default action map picker.

var selected = EditorGUILayout.Popup(m_DefaultActionMapText, m_SelectedDefaultActionMap,
var rect = EditorGUILayout.GetControlRect();
var label = EditorGUI.BeginProperty(rect, m_DefaultActionMapText, m_DefaultActionMapProperty);
var selected = EditorGUI.Popup(rect, label, m_SelectedDefaultActionMap,
m_ActionMapOptions);
EditorGUI.EndProperty();
if (selected != m_SelectedDefaultActionMap)
{
if (selected == 0)
Expand Down Expand Up @@ -424,6 +443,7 @@ private void OnActionAssetChange()
m_ActionNames = null;
m_SelectedDefaultActionMap = -1;
m_SelectedDefaultControlScheme = -1;
m_InvalidDefaultControlSchemeName = null;
return;
}

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

// Read out control schemes.
var selectedDefaultControlScheme = playerInput.defaultControlScheme;
m_InvalidDefaultControlSchemeName = null;
m_SelectedDefaultControlScheme = 0;
var controlSchemes = asset.controlSchemes;
m_ControlSchemeOptions = new GUIContent[controlSchemes.Count + 1];
m_ControlSchemeOptions[0] = new GUIContent(EditorGUIUtility.TrTextContent("<Any>"));
////TODO: sort alphabetically
for (var i = 0; i < controlSchemes.Count; ++i)
{
var name = controlSchemes[i].name;
m_ControlSchemeOptions[i + 1] = new GUIContent(name);
////TODO: sort alphabetically and ensure that the order is the same in the schemes editor
var controlSchemesNames = asset.controlSchemes.Select(cs => cs.name).ToList();

if (selectedDefaultControlScheme != null && string.Compare(name, selectedDefaultControlScheme,
StringComparison.InvariantCultureIgnoreCase) == 0)
m_SelectedDefaultControlScheme = i + 1;
// try to find the selected Default Control Scheme
if (!string.IsNullOrEmpty(selectedDefaultControlScheme))
{
// +1 since <Any> will be the first in the list
m_SelectedDefaultControlScheme = 1 + controlSchemesNames.FindIndex(name => string.Compare(name, selectedDefaultControlScheme,
StringComparison.InvariantCultureIgnoreCase) == 0);
// if not found, will insert the invalid name next to <Any>
if (m_SelectedDefaultControlScheme == 0)
{
m_InvalidDefaultControlSchemeName = selectedDefaultControlScheme;
m_SelectedDefaultControlScheme = 1;
controlSchemesNames.Insert(0, $"{selectedDefaultControlScheme}{L10n.Tr("<Not Found>")}");
}
}
if (m_SelectedDefaultControlScheme <= 0)
else
{
playerInput.defaultControlScheme = null;
}

m_ControlSchemeOptions = new GUIContent[controlSchemesNames.Count + 1];
m_ControlSchemeOptions[0] = new GUIContent(EditorGUIUtility.TrTextContent("<Any>"));
for (var i = 0; i < controlSchemesNames.Count; ++i)
{
m_ControlSchemeOptions[i + 1] = new GUIContent(controlSchemesNames[i]);
}

// Read out action maps.
var selectedDefaultActionMap = !string.IsNullOrEmpty(playerInput.defaultActionMap)
Expand Down Expand Up @@ -562,6 +596,7 @@ void AddEntry(InputAction action, PlayerInput.ActionEvent actionEvent)
[NonSerialized] private int[] m_ActionMapIndices;
[NonSerialized] private int m_NumActionMaps;
[NonSerialized] private int m_SelectedDefaultControlScheme;
[NonSerialized] private string m_InvalidDefaultControlSchemeName;
[NonSerialized] private GUIContent[] m_ControlSchemeOptions;
[NonSerialized] private int m_SelectedDefaultActionMap;
[NonSerialized] private GUIContent[] m_ActionMapOptions;
Expand Down

0 comments on commit cbf4058

Please sign in to comment.