diff --git a/Assets/Samples/SimpleDemo/SimpleControls.cs b/Assets/Samples/SimpleDemo/SimpleControls.cs index 34b5be9ffa..6c5585d6ca 100644 --- a/Assets/Samples/SimpleDemo/SimpleControls.cs +++ b/Assets/Samples/SimpleDemo/SimpleControls.cs @@ -6,10 +6,10 @@ using UnityEngine.InputSystem; using UnityEngine.InputSystem.Utilities; -public class SimpleControls : IInputActionCollection, IDisposable +public class @SimpleControls: IInputActionCollection, IDisposable { private InputActionAsset asset; - public SimpleControls() + public @SimpleControls() { asset = InputActionAsset.FromJson(@"{ ""name"": ""SimpleControls"", @@ -207,8 +207,8 @@ public void Disable() private readonly InputAction m_gameplay_look; public struct GameplayActions { - private SimpleControls m_Wrapper; - public GameplayActions(SimpleControls wrapper) { m_Wrapper = wrapper; } + private @SimpleControls m_Wrapper; + public GameplayActions(@SimpleControls wrapper) { m_Wrapper = wrapper; } public InputAction @fire => m_Wrapper.m_gameplay_fire; public InputAction @move => m_Wrapper.m_gameplay_move; public InputAction @look => m_Wrapper.m_gameplay_look; @@ -221,28 +221,28 @@ public void SetCallbacks(IGameplayActions instance) { if (m_Wrapper.m_GameplayActionsCallbackInterface != null) { - fire.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire; - fire.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire; - fire.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire; - move.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove; - move.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove; - move.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove; - look.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook; - look.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook; - look.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook; + @fire.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire; + @fire.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire; + @fire.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnFire; + @move.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove; + @move.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove; + @move.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnMove; + @look.started -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook; + @look.performed -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook; + @look.canceled -= m_Wrapper.m_GameplayActionsCallbackInterface.OnLook; } m_Wrapper.m_GameplayActionsCallbackInterface = instance; if (instance != null) { - fire.started += instance.OnFire; - fire.performed += instance.OnFire; - fire.canceled += instance.OnFire; - move.started += instance.OnMove; - move.performed += instance.OnMove; - move.canceled += instance.OnMove; - look.started += instance.OnLook; - look.performed += instance.OnLook; - look.canceled += instance.OnLook; + @fire.started += instance.OnFire; + @fire.performed += instance.OnFire; + @fire.canceled += instance.OnFire; + @move.started += instance.OnMove; + @move.performed += instance.OnMove; + @move.canceled += instance.OnMove; + @look.started += instance.OnLook; + @look.performed += instance.OnLook; + @look.canceled += instance.OnLook; } } } diff --git a/Assets/Tests/InputSystem/CoreTests_Editor.cs b/Assets/Tests/InputSystem/CoreTests_Editor.cs index fd171531a8..2e1bfd2799 100644 --- a/Assets/Tests/InputSystem/CoreTests_Editor.cs +++ b/Assets/Tests/InputSystem/CoreTests_Editor.cs @@ -1808,6 +1808,7 @@ public void Editor_ActionTree_CompositesAreShownWithNiceNames() [TestCase("MyControls (2)", "MyNamespace", "", "MyNamespace.MyControls2")] [TestCase("MyControls (2)", "MyNamespace", "MyClassName", "MyNamespace.MyClassName")] [TestCase("MyControls", "", "MyClassName", "MyClassName")] + [TestCase("interface", "", "class", "class")] // Make sure we can deal with C# reserved keywords. public void Editor_CanGenerateCodeWrapperForInputAsset(string assetName, string namespaceName, string className, string typeName) { var map1 = new InputActionMap("set1"); @@ -1815,6 +1816,8 @@ public void Editor_CanGenerateCodeWrapperForInputAsset(string assetName, string map1.AddAction("action2", binding: "/gamepad/rightStick"); var map2 = new InputActionMap("set2"); map2.AddAction("action1", binding: "/gamepad/buttonSouth"); + // Add an action that has a C# reserved keyword name. + map2.AddAction("return"); var asset = ScriptableObject.CreateInstance(); asset.AddActionMap(map1); asset.AddActionMap(map2); diff --git a/Packages/com.unity.inputsystem/CHANGELOG.md b/Packages/com.unity.inputsystem/CHANGELOG.md index 079c8ea005..b3d56f5ff8 100755 --- a/Packages/com.unity.inputsystem/CHANGELOG.md +++ b/Packages/com.unity.inputsystem/CHANGELOG.md @@ -19,6 +19,7 @@ however, it has to be formatted properly to pass verification tests. #### Actions - Fixed missing keyboard bindings in `DefaultInputActions.inputactions` for navigation in UI. +- Fixed using C# reserved names in .inputactions assets leading to compile errors in generated C# classes (case 1189861). - Assigning a new `InputActionAsset` to a `InputSystemUIInputModule` will no longer look up action names globally but rather only look for actions that are located in action maps with the same name. * Previously, if you e.g. switched from one asset where the `point` action was bound to `UI/Point` to an asset that had no `UI` action map but did have an action called `Point` somewhere else, it would erroneously pick the most likely unrelated `Point` action for use by the UI. - Fixed missing custom editors for `AxisDeadzoneProcessor` and `StickDeadzoneProcessor` that link `min` and `max` values to input settings. diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionCodeGenerator.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionCodeGenerator.cs index b051dedad0..779536b109 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionCodeGenerator.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionCodeGenerator.cs @@ -82,13 +82,13 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options } // Begin class. - writer.WriteLine($"public class {options.className} : IInputActionCollection, IDisposable"); + writer.WriteLine($"public class @{options.className} : IInputActionCollection, IDisposable"); writer.BeginBlock(); writer.WriteLine($"private InputActionAsset asset;"); // Default constructor. - writer.WriteLine($"public {options.className}()"); + writer.WriteLine($"public @{options.className}()"); writer.BeginBlock(); writer.WriteLine($"asset = InputActionAsset.FromJson(@\"{asset.ToJson().Replace("\"", "\"\"")}\");"); @@ -186,8 +186,8 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options writer.BeginBlock(); // Constructor. - writer.WriteLine($"private {options.className} m_Wrapper;"); - writer.WriteLine($"public {mapTypeName}({options.className} wrapper) {{ m_Wrapper = wrapper; }}"); + writer.WriteLine($"private @{options.className} m_Wrapper;"); + writer.WriteLine($"public {mapTypeName}(@{options.className} wrapper) {{ m_Wrapper = wrapper; }}"); // Getter for each action. foreach (var action in map.actions) @@ -223,9 +223,9 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options var actionName = CSharpCodeHelpers.MakeIdentifier(action.name); var actionTypeName = CSharpCodeHelpers.MakeTypeName(action.name); - writer.WriteLine($"{actionName}.started -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};"); - writer.WriteLine($"{actionName}.performed -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};"); - writer.WriteLine($"{actionName}.canceled -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};"); + writer.WriteLine($"@{actionName}.started -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};"); + writer.WriteLine($"@{actionName}.performed -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};"); + writer.WriteLine($"@{actionName}.canceled -= m_Wrapper.m_{mapTypeName}CallbackInterface.On{actionTypeName};"); } writer.EndBlock(); @@ -238,9 +238,9 @@ public static string GenerateWrapperCode(InputActionAsset asset, Options options var actionName = CSharpCodeHelpers.MakeIdentifier(action.name); var actionTypeName = CSharpCodeHelpers.MakeTypeName(action.name); - writer.WriteLine($"{actionName}.started += instance.On{actionTypeName};"); - writer.WriteLine($"{actionName}.performed += instance.On{actionTypeName};"); - writer.WriteLine($"{actionName}.canceled += instance.On{actionTypeName};"); + writer.WriteLine($"@{actionName}.started += instance.On{actionTypeName};"); + writer.WriteLine($"@{actionName}.performed += instance.On{actionTypeName};"); + writer.WriteLine($"@{actionName}.canceled += instance.On{actionTypeName};"); } writer.EndBlock(); writer.EndBlock(); diff --git a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs index 01140b5984..3a0a48301f 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Editor/AssetImporter/InputActionImporter.cs @@ -22,7 +22,7 @@ namespace UnityEngine.InputSystem.Editor [ScriptedImporter(kVersion, InputActionAsset.Extension)] internal class InputActionImporter : ScriptedImporter { - private const int kVersion = 7; + private const int kVersion = 8; private const string kActionIcon = "Packages/com.unity.inputsystem/InputSystem/Editor/Icons/InputAction.png"; private const string kAssetIcon = "Packages/com.unity.inputsystem/InputSystem/Editor/Icons/InputActionAsset.png"; diff --git a/Packages/com.unity.inputsystem/InputSystem/Utilities/CSharpCodeHelpers.cs b/Packages/com.unity.inputsystem/InputSystem/Utilities/CSharpCodeHelpers.cs index c2defc4682..7fffe003d3 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Utilities/CSharpCodeHelpers.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Utilities/CSharpCodeHelpers.cs @@ -40,10 +40,11 @@ public static bool IsEmptyOrProperNamespaceName(string name) return name.Split('.').All(IsProperIdentifier); } + ////TODO: this one should add the @escape automatically so no other code has to worry public static string MakeIdentifier(string name, string suffix = "") { if (string.IsNullOrEmpty(name)) - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); if (char.IsDigit(name[0])) name = "_" + name;