From efdaf0cc09edb62102f65833ec5f952d353acda5 Mon Sep 17 00:00:00 2001 From: Scott Doxey Date: Tue, 19 Jan 2021 02:10:33 -0500 Subject: [PATCH] Added support for the New Unity Input System. Added local getters to wrap input API references. Added Unity.InputSystem to assembley reference. Added com.unity.inputsystem package. --- .../CandyCoded.Forms/CandyCoded.Forms.asmdef | 3 +- .../Plugins/CandyCoded.Forms/Scripts/Form.cs | 31 ++++++++++++++++--- Packages/manifest.json | 5 +-- Packages/packages-lock.json | 9 +++++- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Assets/Plugins/CandyCoded.Forms/CandyCoded.Forms.asmdef b/Assets/Plugins/CandyCoded.Forms/CandyCoded.Forms.asmdef index 5e9886e..45aa63d 100644 --- a/Assets/Plugins/CandyCoded.Forms/CandyCoded.Forms.asmdef +++ b/Assets/Plugins/CandyCoded.Forms/CandyCoded.Forms.asmdef @@ -1,7 +1,8 @@ { "name": "CandyCoded.Forms", "references": [ - "UnityEngine.UI" + "UnityEngine.UI", + "Unity.InputSystem" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs index c688274..fcc224d 100644 --- a/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs +++ b/Assets/Plugins/CandyCoded.Forms/Scripts/Form.cs @@ -7,6 +7,9 @@ using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; +#if ENABLE_INPUT_SYSTEM +using UnityEngine.InputSystem; +#endif using UnityEngine.UI; namespace CandyCoded.Forms @@ -30,6 +33,26 @@ public class Form : MonoBehaviour private Form _parentForm; +#if ENABLE_INPUT_SYSTEM + private bool _isTabKeyDown => Keyboard.current.tabKey.wasPressedThisFrame; + + private bool _isReturnKeyDown => Keyboard.current.enterKey.wasPressedThisFrame; + + private bool _isLeftShiftHeld => Keyboard.current.leftShiftKey.isPressed; + + private bool _isRightShiftHeld => Keyboard.current.rightShiftKey.isPressed; +#else + private bool _isTabKeyDown => Input.GetKeyDown(KeyCode.Tab); + + private bool _isReturnKeyDown => Input.GetKeyDown(KeyCode.Return); + + private bool _isLeftShiftHeld => Input.GetKey(KeyCode.LeftShift); + + private bool _isRightShiftHeld => Input.GetKey(KeyCode.RightShift); +#endif + + private bool _isShiftHeld => _isLeftShiftHeld || _isRightShiftHeld; + private void Awake() { @@ -64,12 +87,12 @@ private void Update() return; } - if (Input.GetKeyDown(KeyCode.Tab)) + if (_isTabKeyDown) { HandleTabPress(selectable, allSelectable); } - if (Input.GetKeyDown(KeyCode.Return)) + if (_isReturnKeyDown) { HandleReturnPress(); } @@ -120,9 +143,7 @@ private void HandleTabPress(Selectable selectable, Selectable[] allSelectable) var prevSelectable = selectable.FindSelectableOnUp() ?? allSelectable.Last(); var nextSelectable = selectable.FindSelectableOnDown() ?? allSelectable.First(); - var next = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) - ? prevSelectable - : nextSelectable; + var next = _isShiftHeld ? prevSelectable : nextSelectable; _eventSystem.SetSelectedGameObject(next.gameObject, null); diff --git a/Packages/manifest.json b/Packages/manifest.json index 48b28ca..cdc0015 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -7,7 +7,9 @@ "com.unity.collab-proxy": "1.2.16", "com.unity.ide.rider": "1.1.4", "com.unity.ide.vscode": "1.2.0", + "com.unity.inputsystem": "1.0.1", "com.unity.multiplayer-hlapi": "1.0.6", + "com.unity.nuget.newtonsoft-json": "2.0.0", "com.unity.purchasing": "2.0.6", "com.unity.test-framework": "1.1.14", "com.unity.textmeshpro": "2.0.1", @@ -44,7 +46,6 @@ "com.unity.modules.video": "1.0.0", "com.unity.modules.vr": "1.0.0", "com.unity.modules.wind": "1.0.0", - "com.unity.modules.xr": "1.0.0", - "com.unity.nuget.newtonsoft-json": "2.0.0" + "com.unity.modules.xr": "1.0.0" } } diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index d771acd..e34a693 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -38,7 +38,7 @@ "url": "https://packages.unity.com" }, "com.unity.ext.nunit": { - "version": "1.0.5", + "version": "1.0.6", "depth": 2, "source": "registry", "dependencies": {}, @@ -60,6 +60,13 @@ "dependencies": {}, "url": "https://packages.unity.com" }, + "com.unity.inputsystem": { + "version": "1.0.1", + "depth": 0, + "source": "registry", + "dependencies": {}, + "url": "https://packages.unity.com" + }, "com.unity.multiplayer-hlapi": { "version": "1.0.6", "depth": 0,