From 12aee65d290e56cdf084087f8d1fe364094f4b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Tue, 4 Nov 2025 10:01:36 +0100 Subject: [PATCH 1/4] FIX: Fix for non-deterministic behaviour in asset import stages. --- .../AssetImporter/InputActionImporter.cs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs index 9aa690b466..61df055c2a 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs @@ -119,13 +119,6 @@ public override void OnImportAsset(AssetImportContext ctx) foreach (var callback in s_OnImportCallbacks) callback(); - - var asset = CreateFromJson(ctx); - if (asset == null) - return; - - if (m_GenerateWrapperCode) - GenerateWrapperCode(ctx, asset, m_WrapperCodeNamespace, m_WrapperClassName, m_WrapperCodePath); } internal static void SetupAsset(InputActionAsset asset) @@ -201,7 +194,7 @@ private static void CreateInputActionReferences(InputActionAsset asset, AddObjec } } - private static void GenerateWrapperCode(AssetImportContext ctx, InputActionAsset asset, string codeNamespace, string codeClassName, string codePath) + private static void GenerateWrapperCode(string assetPath, InputActionAsset asset, string codeNamespace, string codeClassName, string codePath) { var maps = asset.actionMaps; // When using code generation, it is an error for any action map to be named the same as the asset itself. @@ -210,7 +203,7 @@ private static void GenerateWrapperCode(AssetImportContext ctx, InputActionAsset if (maps.Any(x => CSharpCodeHelpers.MakeTypeName(x.name) == className || CSharpCodeHelpers.MakeIdentifier(x.name) == className)) { - ctx.LogImportError( + Debug.LogError( $"{asset.name}: An action map in an .inputactions asset cannot be named the same as the asset itself if 'Generate C# Class' is used. " + "You can rename the action map in the asset, rename the asset itself or assign a different C# class name in the import settings."); return; @@ -220,7 +213,6 @@ private static void GenerateWrapperCode(AssetImportContext ctx, InputActionAsset if (string.IsNullOrEmpty(wrapperFilePath)) { // Placed next to .inputactions file. - var assetPath = ctx.assetPath; var directory = Path.GetDirectoryName(assetPath); var fileName = Path.GetFileNameWithoutExtension(assetPath); wrapperFilePath = Path.Combine(directory, fileName) + ".cs"; @@ -229,7 +221,6 @@ private static void GenerateWrapperCode(AssetImportContext ctx, InputActionAsset wrapperFilePath.StartsWith("../") || wrapperFilePath.StartsWith("..\\")) { // User-specified file relative to location of .inputactions file. - var assetPath = ctx.assetPath; var directory = Path.GetDirectoryName(assetPath); wrapperFilePath = Path.Combine(directory, wrapperFilePath); } @@ -258,7 +249,7 @@ private static void GenerateWrapperCode(AssetImportContext ctx, InputActionAsset var options = new InputActionCodeGenerator.Options { - sourceAssetPath = ctx.assetPath, + sourceAssetPath = assetPath, namespaceName = codeNamespace, className = codeClassName, }; @@ -378,13 +369,23 @@ private static bool ContainsInputActionAssetPath(string[] assetPaths) private class InputActionJsonNameModifierAssetProcessor : AssetPostprocessor { private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, - string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) + string[] movedAssets, string[] movedFromAssetPaths) { var needToInvalidate = false; foreach (var assetPath in importedAssets) { if (IsInputActionAssetPath(assetPath)) { + // Generate C# code from asset if configured via importer settings. + // We generate from a parsed temporary asset here since loading the asset won't work here. + var importer = GetAtPath(assetPath) as InputActionImporter; + if (importer != null) + { + var asset = InputActionAsset.FromJson(File.ReadAllText(assetPath)); + if (importer.m_GenerateWrapperCode) + GenerateWrapperCode(assetPath, asset, importer.m_WrapperCodeNamespace, importer.m_WrapperClassName, importer.m_WrapperCodePath); + } + needToInvalidate = true; CheckAndRenameJsonNameIfDifferent(assetPath); } From 7688165d1670e4bc4f6260ad7e38a9d8f81ce2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Tue, 4 Nov 2025 10:52:55 +0100 Subject: [PATCH 2/4] Updated CHANGELOG and fixed formatting. --- Packages/com.unity.inputsystem/CHANGELOG.md | 1 + .../InputSystem/Editor/AssetImporter/InputActionImporter.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index a6a60d0769..a771fcba2f 100644 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -38,6 +38,7 @@ however, it has to be formatted properly to pass verification tests. - Limited the Add Control Scheme popup window max size to be its parent window, so that it won't resize beyond the visible area. [ISXB-1733](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1733) - Fixed an issue where the action icon would shrink or disappear from UI when an action has a very long name. [ISXB-1650](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1650). - Fixed upgrading input actions containing multiple processors [ISXB-1674](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1674). +- Fixed an issue that led to non-deterministic behaviour during `.inputaction` asset imports that could lead to corrupted assets or Unity hanging during asset import when "Generate C# Scripts" was active in importer settings. [ISXB-1746](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1746) ## [1.15.0] - 2025-10-03 diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs index 61df055c2a..9e3dddfb00 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs @@ -385,7 +385,7 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del if (importer.m_GenerateWrapperCode) GenerateWrapperCode(assetPath, asset, importer.m_WrapperCodeNamespace, importer.m_WrapperClassName, importer.m_WrapperCodePath); } - + needToInvalidate = true; CheckAndRenameJsonNameIfDifferent(assetPath); } From 08ebd9eac77b3c09c54b1bc552abd68a63e8d69a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Tue, 4 Nov 2025 11:05:00 +0100 Subject: [PATCH 3/4] Corrected accidentally removed statement. --- .../InputSystem/Editor/AssetImporter/InputActionImporter.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs index 9e3dddfb00..6cbe089c78 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs @@ -119,6 +119,8 @@ public override void OnImportAsset(AssetImportContext ctx) foreach (var callback in s_OnImportCallbacks) callback(); + + CreateFromJson(ctx); } internal static void SetupAsset(InputActionAsset asset) @@ -254,6 +256,7 @@ private static void GenerateWrapperCode(string assetPath, InputActionAsset asset className = codeClassName, }; + if (InputActionCodeGenerator.GenerateWrapperCode(wrapperFilePath, asset, options)) { // This isn't ideal and may have side effects, but we cannot avoid compiling again. From 87babcc37337611d0f2605840a7caa5fa099de5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Sidenvall?= Date: Tue, 4 Nov 2025 12:00:33 +0100 Subject: [PATCH 4/4] Corrected some mistakes and reverted error log type regression. --- .../AssetImporter/InputActionImporter.cs | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs index 6cbe089c78..27211e519d 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs @@ -120,7 +120,16 @@ public override void OnImportAsset(AssetImportContext ctx) foreach (var callback in s_OnImportCallbacks) callback(); - CreateFromJson(ctx); + var asset = CreateFromJson(ctx); + if (asset == null) + return; + + if (m_GenerateWrapperCode && HasMapWithSameNameAsAsset(asset, m_WrapperClassName)) + { + ctx.LogImportError( + $"{asset.name}: An action map in an .inputactions asset cannot be named the same as the asset itself if 'Generate C# Class' is used. " + + "You can rename the action map in the asset, rename the asset itself or assign a different C# class name in the import settings."); + } } internal static void SetupAsset(InputActionAsset asset) @@ -196,20 +205,20 @@ private static void CreateInputActionReferences(InputActionAsset asset, AddObjec } } - private static void GenerateWrapperCode(string assetPath, InputActionAsset asset, string codeNamespace, string codeClassName, string codePath) + private static bool HasMapWithSameNameAsAsset(InputActionAsset asset, string codeClassName) { - var maps = asset.actionMaps; // When using code generation, it is an error for any action map to be named the same as the asset itself. // https://fogbugz.unity3d.com/f/cases/1212052/ var className = !string.IsNullOrEmpty(codeClassName) ? codeClassName : CSharpCodeHelpers.MakeTypeName(asset.name); - if (maps.Any(x => - CSharpCodeHelpers.MakeTypeName(x.name) == className || CSharpCodeHelpers.MakeIdentifier(x.name) == className)) - { - Debug.LogError( - $"{asset.name}: An action map in an .inputactions asset cannot be named the same as the asset itself if 'Generate C# Class' is used. " - + "You can rename the action map in the asset, rename the asset itself or assign a different C# class name in the import settings."); + return (asset.actionMaps.Any(x => + CSharpCodeHelpers.MakeTypeName(x.name) == className || + CSharpCodeHelpers.MakeIdentifier(x.name) == className)); + } + + private static void GenerateWrapperCode(string assetPath, InputActionAsset asset, string codeNamespace, string codeClassName, string codePath) + { + if (HasMapWithSameNameAsAsset(asset, codeClassName)) return; - } var wrapperFilePath = codePath; if (string.IsNullOrEmpty(wrapperFilePath)) @@ -382,11 +391,18 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del // Generate C# code from asset if configured via importer settings. // We generate from a parsed temporary asset here since loading the asset won't work here. var importer = GetAtPath(assetPath) as InputActionImporter; - if (importer != null) + if (importer != null && importer.m_GenerateWrapperCode) { - var asset = InputActionAsset.FromJson(File.ReadAllText(assetPath)); - if (importer.m_GenerateWrapperCode) - GenerateWrapperCode(assetPath, asset, importer.m_WrapperCodeNamespace, importer.m_WrapperClassName, importer.m_WrapperCodePath); + try + { + var asset = InputActionAsset.FromJson(File.ReadAllText(assetPath)); + GenerateWrapperCode(assetPath, asset, importer.m_WrapperCodeNamespace, + importer.m_WrapperClassName, importer.m_WrapperCodePath); + } + catch (Exception e) + { + Debug.LogException(e); + } } needToInvalidate = true;