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 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6504c53
Added test to cover the fix
AswinRajGopal Apr 7, 2025
e9cff62
Determine index from enum value and serialise accordingly for the asset.
AswinRajGopal Apr 7, 2025
7284150
Fix test failure after fix, had to focus the dropdown field before ch…
AswinRajGopal Apr 17, 2025
73890bd
Determine index from enum value and serialise accordingly for the asset.
AswinRajGopal Apr 7, 2025
9020c5e
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Apr 17, 2025
9819aab
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
AswinRajGopal Apr 17, 2025
43278ad
Update CHANGELOG.md
AswinRajGopal Apr 19, 2025
a06e9d3
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
Pauliusd01 Apr 23, 2025
580e931
Address PR feedbacks, changelog changed, test name modified, comments…
AswinRajGopal Apr 23, 2025
774190a
Address PR feedbacks, changelog changed, test name modified, comments…
AswinRajGopal Apr 23, 2025
6d50a6b
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Apr 23, 2025
7c7b927
Upgrade compatibility to fill the dropdown field with value
AswinRajGopal May 16, 2025
a521b32
Implement migrator for the old input action asset as we now serialize…
AswinRajGopal May 30, 2025
8c65e40
Removed additional version constant and used the existing k_Version t…
AswinRajGopal May 31, 2025
37d1dfb
Handle migration for LoadFromJson aswell and used NameAndParamerter p…
AswinRajGopal Jun 4, 2025
8bc3ef0
Remove log statements and unused header.
AswinRajGopal Jun 4, 2025
359f03d
Fix test failures and compiler errors.
AswinRajGopal Jun 5, 2025
b41abf8
Addressed PR comments, changed the migration logic a bit to deal with…
AswinRajGopal Jun 10, 2025
8d168c8
Removed empty lines, reverted kVersion to private.
AswinRajGopal Jun 10, 2025
a81282c
Removed empty lines, reverted kVersion to private.
AswinRajGopal Jun 10, 2025
b3f63c9
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Jun 10, 2025
40ce21e
Moved the optimization into migration function, assigned proper versi…
AswinRajGopal Jun 10, 2025
261cbd6
Fix test failure checking an empty json, omitt versioning.
AswinRajGopal Jun 10, 2025
42bb20c
Remove commented code and fix misplaced enums.
AswinRajGopal Jun 10, 2025
8acf7fe
Merge branch 'develop' into isxb-1482/fix-custom-processor-serializes…
ekcoh Jun 10, 2025
51994b7
Removed nullabe version fileds due to readability issues and boxing.
AswinRajGopal Jun 18, 2025
fbbacad
Patched the failing tests due the version introduction.
AswinRajGopal Jun 18, 2025
101dd7c
Merge branch 'isxb-1482/fix-custom-processor-serializesbyIndex' of ht…
AswinRajGopal Jun 18, 2025
1f6bd45
Revert "Patched the failing tests due the version introduction."
AswinRajGopal Jun 20, 2025
42807fd
Fix failing tests by introducing versioning.
AswinRajGopal Jun 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions Assets/Tests/InputSystem.Editor/CustomProcessorEnumTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS && 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;

internal enum SomeEnum
{
OptionA = 10,
OptionB = 20
}

#if UNITY_EDITOR
[InitializeOnLoad]
#endif
internal class CustomProcessor : InputProcessor<float>
{
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>
{
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 ProcessorEnum_ShouldSerializeByValue_WhenSerializedToAsset()
{
// Serialize current asset to JSON, and check that initial JSON contains default enum value for OptionA
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.");

// Query the dropdown with exactly two enum choices and check that the drop down is present in the UI
var dropdownList = m_Window.rootVisualElement.Query<DropdownField>().Where(d => d.choices.Count == 2).ToList();
Assume.That(dropdownList.Count > 0, Is.True, "Enum parameter dropdown not found in the UI.");

// Determine the new value to be set in the dropdown, focus the dropdown before dispatching the change
var dropdown = dropdownList.First();
var newValue = dropdown.choices[1];
dropdown.Focus();
dropdown.value = newValue;

// Create and send a change event from OptionA to OptionB
var changeEvent = ChangeEvent<Enum>.GetPooled(SomeEnum.OptionA, SomeEnum.OptionB);
changeEvent.target = dropdown;
dropdown.SendEvent(changeEvent);

// Find the save button in the window, focus and click the save button to persist the changes
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));

// Verify that the updated JSON contains the new enum value for OpitonB
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.

4 changes: 2 additions & 2 deletions Assets/Tests/InputSystem/CoreTests_Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5215,8 +5215,8 @@ public void Actions_CanConvertAssetToAndFromJson()
static string MinimalJson(string name = null)
{
if (name != null)
return "{\n \"name\": \"" + name + "\",\n \"maps\": [],\n \"controlSchemes\": []\n}";
return "{\n \"maps\": [],\n \"controlSchemes\": []\n}";
return "{\n \"version\": 0,\n \"name\": \"" + name + "\",\n \"maps\": [],\n \"controlSchemes\": []\n}";
return "{\n \"version\": 0,\n \"maps\": [],\n \"controlSchemes\": []\n}";
}

[Test]
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ however, it has to be formatted properly to pass verification tests.
- Fixed PlayerInput component automatically switching away from the default ActionMap set to 'None'.
- Fixed a console error being shown when targeting visionOS builds in 2022.3.
- Fixed a Tap Interaction issue with analog controls. The Tap interaction would keep re-starting after timeout. [ISXB-627](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-627)
- Fixed an issue that caused input processors with enum properties to incorrectly serialise by index instead of by value [ISXB-1474](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1474)

## [1.14.0] - 2025-03-20

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine.InputSystem.Editor;
using UnityEngine.InputSystem.Utilities;

////TODO: make the FindAction logic available on any IEnumerable<InputAction> and IInputActionCollection via extension methods
Expand Down Expand Up @@ -275,6 +278,21 @@
return action;
}
}
/// <summary>
/// File‐format version constants for InputActionAsset JSON.
/// </summary>
static class JsonVersion
{
/// <summary>The original JSON version format for InputActionAsset.</summary>
public const int Version0 = 0;

/// <summary>Updated JSON version format for InputActionAsset.</summary>
/// <remarks>Changes representation of parameter values from being serialized by value to being serialized by value.</remarks>
public const int Version1 = 1;

/// <summary>The current version.</summary>
public const int Current = Version1;
}

/// <summary>
/// Return a JSON representation of the asset.
Expand All @@ -296,8 +314,10 @@
/// <seealso cref="FromJson"/>
public string ToJson()
{
var hasContent = m_ActionMaps.LengthSafe() > 0 || m_ControlSchemes.LengthSafe() > 0;
return JsonUtility.ToJson(new WriteFileJson
{
version = hasContent ? JsonVersion.Current : JsonVersion.Version0,
name = name,
maps = InputActionMap.WriteFileJson.FromMaps(m_ActionMaps).maps,
controlSchemes = InputControlScheme.SchemeJson.ToJson(m_ControlSchemes),
Expand Down Expand Up @@ -379,6 +399,7 @@
throw new ArgumentNullException(nameof(json));

var parsedJson = JsonUtility.FromJson<ReadFileJson>(json);
MigrateJson(ref parsedJson);
parsedJson.ToAsset(this);
}

Expand Down Expand Up @@ -950,6 +971,7 @@
[Serializable]
internal struct WriteFileJson
{
public int version;
public string name;
public InputActionMap.WriteMapJson[] maps;
public InputControlScheme.SchemeJson[] controlSchemes;
Expand All @@ -965,6 +987,7 @@
[Serializable]
internal struct ReadFileJson
{
public int version;
public string name;
public InputActionMap.ReadMapJson[] maps;
public InputControlScheme.SchemeJson[] controlSchemes;
Expand All @@ -981,5 +1004,73 @@
map.m_Asset = asset;
}
}

/// <summary>
/// If parsedJson.version is older than Current, rewrite every
/// action.processors entry to replace “enumName(Ordinal=…)” with
/// “enumName(Value=…)” and bump parsedJson.version.
/// </summary>
internal void MigrateJson(ref ReadFileJson parsedJson)
{
if (parsedJson.version >= JsonVersion.Version1)
return;
if ((parsedJson.maps?.Length ?? 0) > 0 && (parsedJson.version) < JsonVersion.Version1)
{
for (var mi = 0; mi < parsedJson.maps.Length; ++mi)
{
var mapJson = parsedJson.maps[mi];
for (var ai = 0; ai < mapJson.actions.Length; ++ai)
{
var actionJson = mapJson.actions[ai];
var raw = actionJson.processors;
if (string.IsNullOrEmpty(raw))
continue;

var list = NameAndParameters.ParseMultiple(raw).ToList();
var rebuilt = new List<string>(list.Count);
foreach (var nap in list)
{
var procType = InputSystem.TryGetProcessor(nap.name);
if (nap.parameters.Count == 0 || procType == null)
{
rebuilt.Add(nap.ToString());
continue;

Check warning on line 1037 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1029-L1037

Added lines #L1029 - L1037 were not covered by tests
}

var dict = nap.parameters.ToDictionary(p => p.name, p => p.value.ToString());
var anyChanged = false;
foreach (var field in procType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => f.FieldType.IsEnum))
{
if (dict.TryGetValue(field.Name, out var ordS) && int.TryParse(ordS, out var ord))
{
var values = Enum.GetValues(field.FieldType).Cast<object>().ToArray();
if (ord >= 0 && ord < values.Length)
{
dict[field.Name] = Convert.ToInt32(values[ord]).ToString();
anyChanged = true;
}
}
}

Check warning on line 1053 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1040-L1053

Added lines #L1040 - L1053 were not covered by tests

if (!anyChanged)
{
rebuilt.Add(nap.ToString());
}

Check warning on line 1058 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1055-L1058

Added lines #L1055 - L1058 were not covered by tests
else
{
var paramText = string.Join(",", dict.Select(kv => $"{kv.Key}={kv.Value}"));
rebuilt.Add($"{nap.name}({paramText})");
}
}

Check warning on line 1064 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1060-L1064

Added lines #L1060 - L1064 were not covered by tests

actionJson.processors = string.Join(";", rebuilt);
mapJson.actions[ai] = actionJson;
}

Check warning on line 1068 in Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs#L1066-L1068

Added lines #L1066 - L1068 were not covered by tests
parsedJson.maps[mi] = mapJson;
}
}
// Bump the version so we never re-migrate
parsedJson.version = JsonVersion.Version1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should probably be JSonVersion.Current

Copy link
Collaborator

Choose a reason for hiding this comment

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

In my perspective this looks correct if this migration step only handles v0 to v1. It would need to be followed by a v1 to v2 if Current was e.g. 2

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah I guess I can see that, I was expecting that after running this function we would always end up on the latest version anyways, and we would always need to remember to update the version here... but its a nitpick

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. If we’re already at or beyond the Current format, early out. This means parsedJson.version will never be re-migrated.
  2. Now, version-specific migration. Only assets with at least one map and older than Version1 need the enum-by-value migration.

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,26 @@

if (parameter.isEnum)
{
var intValue = parameter.value.value.ToInt32();
var field = new DropdownField(label.text, parameter.enumNames.Select(x => x.text).ToList(), intValue);
field.tooltip = label.tooltip;
field.RegisterValueChangedCallback(evt => OnValueChanged(ref parameter, field.index, closedIndex));
field.RegisterCallback<BlurEvent>(_ => OnEditEnd());
var names = parameter.enumNames.Select(c => c.text).ToList();
var rawValue = parameter.value.value.ToInt32();
var selectedIndex = parameter.enumValues.IndexOf(rawValue);
if (selectedIndex < 0 || selectedIndex >= names.Count)
selectedIndex = 0;

Check warning on line 285 in Packages/com.unity.inputsystem/InputSystem/Editor/AssetEditor/ParameterListView.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Packages/com.unity.inputsystem/InputSystem/Editor/AssetEditor/ParameterListView.cs#L285

Added line #L285 was not covered by tests

var field = new DropdownField(label.text, names, selectedIndex)
{
tooltip = label.tooltip
};

field.RegisterValueChangedCallback(evt =>
{
var newBackingValue = parameter.enumValues[field.index];
parameter.value.value = PrimitiveValue.FromObject(newBackingValue).ConvertTo(parameter.value.type);
m_Parameters[closedIndex] = parameter;
onChange?.Invoke();
});

field.RegisterCallback<BlurEvent>(_ => onChange?.Invoke());
root.Add(field);
}
else if (parameter.value.type == TypeCode.Int64 || parameter.value.type == TypeCode.UInt64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace UnityEngine.InputSystem.Editor
[ScriptedImporter(kVersion, InputActionAsset.Extension)]
internal class InputActionImporter : ScriptedImporter
{
private const int kVersion = 13;
private const int kVersion = 14;

[SerializeField] private bool m_GenerateWrapperCode;
[SerializeField] private string m_WrapperCodePath;
Expand Down Expand Up @@ -66,7 +66,6 @@ private static InputActionAsset CreateFromJson(AssetImportContext context)
{
// Attempt to parse JSON
asset.LoadFromJson(content);

// Make sure action map names are unique within JSON file
var names = new HashSet<string>();
foreach (var map in asset.actionMaps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class DefaultInputActions : IInputActionCollection2, IDisposable
public @DefaultInputActions()
{
asset = InputActionAsset.FromJson(@"{
""version"": 1,
""name"": ""DefaultInputActions"",
""maps"": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"name": "DefaultInputActions",
"maps": [
{
Expand Down