Skip to content

FIX: Custom processors serialises enum by index rather than by value. #2164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
113 changes: 113 additions & 0 deletions Assets/Tests/InputSystem.Editor/CustomProcessorEnumTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS && UNITY_6000_0_OR_NEWER
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this fix only valid for Unity 6000.0 or newer?


using System;
using NUnit.Framework;
using System.Collections;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Editor;
using UnityEngine.TestTools;
using UnityEngine.UIElements;

public enum SomeEnum
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this enum have to be public?

{
OptionA = 10,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need OptionC and OptionD? I cannot see them being referenced.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(It makes sense to keep OptionA since default)

OptionB = 20,
OptionC = 50,
OptionD = 100
}

#if UNITY_EDITOR
[InitializeOnLoad]
#endif
public class CustomProcessor : InputProcessor<float>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be internal to test assembly right?

{
public SomeEnum SomeEnum;

#if UNITY_EDITOR
static CustomProcessor()
{
Initialize();
}
#endif

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Initialize()
{
InputSystem.RegisterProcessor<CustomProcessor>();
}

public override float Process(float value, InputControl control)
{
return value;
}
}

internal class CustomProcessorEnumTest : UIToolkitBaseTestWindow<InputActionsEditorWindow>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome you added a proper UI-automated test case!

{
InputActionAsset m_Asset;

public override void OneTimeSetUp()
{
base.OneTimeSetUp();
m_Asset = AssetDatabaseUtils.CreateAsset<InputActionAsset>();

var actionMap = m_Asset.AddActionMap("Action Map");

actionMap.AddAction("Action", InputActionType.Value, processors: "Custom(SomeEnum=10)");
}

public override void OneTimeTearDown()
{
AssetDatabaseUtils.Restore();
base.OneTimeTearDown();
}

public override IEnumerator UnitySetup()
{
m_Window = InputActionsEditorWindow.OpenEditor(m_Asset);
yield return base.UnitySetup();
}

[UnityTest]
public IEnumerator CustomProcessorEnum_SerialiseTheValue_InAsset()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest you name the test case according to the requirement or alternatively explain it in a Description attribute, e.g. ProcessorEnum_ShouldSerializeByValue_WhenSerializedToAsset

{
var json = m_Window.currentAssetInEditor.ToJson();

Assert.That(json.Contains("Custom(SomeEnum=10)"), Is.True,
"Serialized JSON does not contain the expected custom processor string for OptionA.");

var dropdownList = m_Window.rootVisualElement.Query<DropdownField>().Where(d => d.choices != null && d.choices.Contains("OptionA") && d.choices.Contains("OptionB")).ToList();
Assume.That(dropdownList.Count > 0, Is.True, "Enum parameter dropdown not found in the UI.");

var dropdown = dropdownList.First();

var newIndex = dropdown.choices.IndexOf("OptionB");
var oldValue = dropdown.value;
var newValue = dropdown.choices[newIndex];

dropdown.Focus();
dropdown.value = newValue;

var changeEvent = ChangeEvent<Enum>.GetPooled(SomeEnum.OptionA, SomeEnum.OptionC);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to write a one-line comment for each parapgraph to summarise the scenario step, e.g. "User clicks Save button to serialise the asset to disc"

changeEvent.target = dropdown;
dropdown.SendEvent(changeEvent);

var saveButton = m_Window.rootVisualElement.Q<Button>("save-asset-toolbar-button");
Assume.That(saveButton, Is.Not.Null, "Save Asset button not found in the UI.");
saveButton.Focus();
SimulateClickOn(saveButton);

Assert.That(dropdown.value, Is.EqualTo(newValue));

var updatedJson = m_Window.currentAssetInEditor.ToJson();

Assert.That(updatedJson.Contains("Custom(SomeEnum=20)"), Is.True, "Serialized JSON does not contain the updated custom processor string for OptionB.");

yield return null;
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed Inspector Window being refreshed all the time when a PlayerInput component is present with Invoke Unity Events nofication mode chosen [ISXB-1448](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1448)
- Fixed an issue where an action with a name containing a slash "/" could not be found via `InputActionAsset.FindAction(string,bool)`. [ISXB-1306](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1306).
- Fixed Gamepad stick up/down inputs that were not recognized in WebGL. [ISXB-1090](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1090)
- Fixed Action with custom processor serialises enums by index rather than by value. [ISXB-1474](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1474)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This URL is public, great. Would generally recommend explaining what fix does and previous symptom, e.g. "Fixed an issue that caused input processors with enum properties to incorrectly serialise by index instead of by value."


## [1.14.0] - 2025-03-20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,11 @@ void OnEditEnd()

if (parameter.isEnum)
{
var intValue = parameter.value.value.ToInt32();
var field = new DropdownField(label.text, parameter.enumNames.Select(x => x.text).ToList(), intValue);
var currentEnumValue = parameter.value.value.ToInt32();
var selectedIndex = parameter.enumValues.IndexOf(currentEnumValue);
var field = new DropdownField(label.text, parameter.enumNames.Select(x => x.text).ToList(), selectedIndex);
field.tooltip = label.tooltip;
field.RegisterValueChangedCallback(evt => OnValueChanged(ref parameter, field.index, closedIndex));
field.RegisterValueChangedCallback(evt => OnValueChanged(ref parameter, parameter.enumValues[field.index], closedIndex));
field.RegisterCallback<BlurEvent>(_ => OnEditEnd());
root.Add(field);
}
Expand Down