From 193db8594075ec0dbd01c8bf84b1112b6f5df1b7 Mon Sep 17 00:00:00 2001 From: Sascha Date: Mon, 19 Jun 2023 16:16:24 +0200 Subject: [PATCH] New Input System --- Assets/PlayerInputActions.cs | 323 ++++++++++++++++++++ Assets/PlayerInputActions.cs.meta | 2 + Assets/PlayerInputActions.inputactions | 160 ++++++++++ Assets/PlayerInputActions.inputactions.meta | 14 + Assets/Scripts/Actions/UnitActionSystem.cs | 4 +- Assets/Scripts/InputManager.cs | 39 ++- Packages/manifest.json | 1 + Packages/packages-lock.json | 9 + ProjectSettings/ProjectSettings.asset | 2 +- 9 files changed, 547 insertions(+), 7 deletions(-) create mode 100644 Assets/PlayerInputActions.cs create mode 100644 Assets/PlayerInputActions.cs.meta create mode 100644 Assets/PlayerInputActions.inputactions create mode 100644 Assets/PlayerInputActions.inputactions.meta diff --git a/Assets/PlayerInputActions.cs b/Assets/PlayerInputActions.cs new file mode 100644 index 00000000..3c732bed --- /dev/null +++ b/Assets/PlayerInputActions.cs @@ -0,0 +1,323 @@ +//------------------------------------------------------------------------------ +// +// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator +// version 1.6.1 +// from Assets/PlayerInputActions.inputactions +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine.InputSystem; +using UnityEngine.InputSystem.Utilities; + +public partial class @PlayerInputActions: IInputActionCollection2, IDisposable +{ + public InputActionAsset asset { get; } + public @PlayerInputActions() + { + asset = InputActionAsset.FromJson(@"{ + ""name"": ""PlayerInputActions"", + ""maps"": [ + { + ""name"": ""Player"", + ""id"": ""8be8563d-f86a-4180-9497-327208207374"", + ""actions"": [ + { + ""name"": ""CameraMovement"", + ""type"": ""Value"", + ""id"": ""3f95d77d-eac9-4f68-afc7-920a3cf21cf4"", + ""expectedControlType"": ""Vector2"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": true + }, + { + ""name"": ""CameraRotate"", + ""type"": ""Value"", + ""id"": ""c2380b6a-5e64-438b-87a5-cfb0fc964439"", + ""expectedControlType"": ""Axis"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": true + }, + { + ""name"": ""CameraZoom"", + ""type"": ""Value"", + ""id"": ""98f04da7-af9e-43e7-86cf-14dffc7c11f3"", + ""expectedControlType"": ""Axis"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": true + }, + { + ""name"": ""Click"", + ""type"": ""Button"", + ""id"": ""ea8ff401-d690-47f2-9be5-538a55f1b170"", + ""expectedControlType"": ""Button"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": false + } + ], + ""bindings"": [ + { + ""name"": ""WASD"", + ""id"": ""46d35c2a-f806-4a90-a753-9d04ac7ae4e8"", + ""path"": ""2DVector"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraMovement"", + ""isComposite"": true, + ""isPartOfComposite"": false + }, + { + ""name"": ""up"", + ""id"": ""8450ee4c-47a0-4423-af62-2461fbd6c186"", + ""path"": ""/w"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraMovement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""down"", + ""id"": ""df4bfb4f-545e-4e1e-9d71-054e257d7c76"", + ""path"": ""/s"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraMovement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""left"", + ""id"": ""1d645d09-e51c-4beb-9733-e7a2587d0d01"", + ""path"": ""/a"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraMovement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""right"", + ""id"": ""724c5104-33c0-44fe-88ba-a23037925ea9"", + ""path"": ""/d"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraMovement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""1D Axis"", + ""id"": ""fefcfddb-2d96-4572-a396-5994ca7d56d8"", + ""path"": ""1DAxis"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraRotate"", + ""isComposite"": true, + ""isPartOfComposite"": false + }, + { + ""name"": ""negative"", + ""id"": ""15042b53-da04-4ef2-95ef-d87cfe970ae6"", + ""path"": ""/e"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraRotate"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""positive"", + ""id"": ""8126d771-a0cb-4485-a176-7cb6ba72a170"", + ""path"": ""/q"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""CameraRotate"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": """", + ""id"": ""7d750995-de9a-4511-b593-00673c503e02"", + ""path"": ""/scroll/y"", + ""interactions"": """", + ""processors"": ""Clamp(min=-1,max=1),Invert"", + ""groups"": """", + ""action"": ""CameraZoom"", + ""isComposite"": false, + ""isPartOfComposite"": false + }, + { + ""name"": """", + ""id"": ""54249f40-90d3-4e81-83bf-352bedf3bdf3"", + ""path"": ""/leftButton"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Click"", + ""isComposite"": false, + ""isPartOfComposite"": false + } + ] + } + ], + ""controlSchemes"": [] +}"); + // Player + m_Player = asset.FindActionMap("Player", throwIfNotFound: true); + m_Player_CameraMovement = m_Player.FindAction("CameraMovement", throwIfNotFound: true); + m_Player_CameraRotate = m_Player.FindAction("CameraRotate", throwIfNotFound: true); + m_Player_CameraZoom = m_Player.FindAction("CameraZoom", throwIfNotFound: true); + m_Player_Click = m_Player.FindAction("Click", throwIfNotFound: true); + } + + public void Dispose() + { + UnityEngine.Object.Destroy(asset); + } + + public InputBinding? bindingMask + { + get => asset.bindingMask; + set => asset.bindingMask = value; + } + + public ReadOnlyArray? devices + { + get => asset.devices; + set => asset.devices = value; + } + + public ReadOnlyArray controlSchemes => asset.controlSchemes; + + public bool Contains(InputAction action) + { + return asset.Contains(action); + } + + public IEnumerator GetEnumerator() + { + return asset.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Enable() + { + asset.Enable(); + } + + public void Disable() + { + asset.Disable(); + } + + public IEnumerable bindings => asset.bindings; + + public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false) + { + return asset.FindAction(actionNameOrId, throwIfNotFound); + } + + public int FindBinding(InputBinding bindingMask, out InputAction action) + { + return asset.FindBinding(bindingMask, out action); + } + + // Player + private readonly InputActionMap m_Player; + private List m_PlayerActionsCallbackInterfaces = new List(); + private readonly InputAction m_Player_CameraMovement; + private readonly InputAction m_Player_CameraRotate; + private readonly InputAction m_Player_CameraZoom; + private readonly InputAction m_Player_Click; + public struct PlayerActions + { + private @PlayerInputActions m_Wrapper; + public PlayerActions(@PlayerInputActions wrapper) { m_Wrapper = wrapper; } + public InputAction @CameraMovement => m_Wrapper.m_Player_CameraMovement; + public InputAction @CameraRotate => m_Wrapper.m_Player_CameraRotate; + public InputAction @CameraZoom => m_Wrapper.m_Player_CameraZoom; + public InputAction @Click => m_Wrapper.m_Player_Click; + public InputActionMap Get() { return m_Wrapper.m_Player; } + public void Enable() { Get().Enable(); } + public void Disable() { Get().Disable(); } + public bool enabled => Get().enabled; + public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); } + public void AddCallbacks(IPlayerActions instance) + { + if (instance == null || m_Wrapper.m_PlayerActionsCallbackInterfaces.Contains(instance)) return; + m_Wrapper.m_PlayerActionsCallbackInterfaces.Add(instance); + @CameraMovement.started += instance.OnCameraMovement; + @CameraMovement.performed += instance.OnCameraMovement; + @CameraMovement.canceled += instance.OnCameraMovement; + @CameraRotate.started += instance.OnCameraRotate; + @CameraRotate.performed += instance.OnCameraRotate; + @CameraRotate.canceled += instance.OnCameraRotate; + @CameraZoom.started += instance.OnCameraZoom; + @CameraZoom.performed += instance.OnCameraZoom; + @CameraZoom.canceled += instance.OnCameraZoom; + @Click.started += instance.OnClick; + @Click.performed += instance.OnClick; + @Click.canceled += instance.OnClick; + } + + private void UnregisterCallbacks(IPlayerActions instance) + { + @CameraMovement.started -= instance.OnCameraMovement; + @CameraMovement.performed -= instance.OnCameraMovement; + @CameraMovement.canceled -= instance.OnCameraMovement; + @CameraRotate.started -= instance.OnCameraRotate; + @CameraRotate.performed -= instance.OnCameraRotate; + @CameraRotate.canceled -= instance.OnCameraRotate; + @CameraZoom.started -= instance.OnCameraZoom; + @CameraZoom.performed -= instance.OnCameraZoom; + @CameraZoom.canceled -= instance.OnCameraZoom; + @Click.started -= instance.OnClick; + @Click.performed -= instance.OnClick; + @Click.canceled -= instance.OnClick; + } + + public void RemoveCallbacks(IPlayerActions instance) + { + if (m_Wrapper.m_PlayerActionsCallbackInterfaces.Remove(instance)) + UnregisterCallbacks(instance); + } + + public void SetCallbacks(IPlayerActions instance) + { + foreach (var item in m_Wrapper.m_PlayerActionsCallbackInterfaces) + UnregisterCallbacks(item); + m_Wrapper.m_PlayerActionsCallbackInterfaces.Clear(); + AddCallbacks(instance); + } + } + public PlayerActions @Player => new PlayerActions(this); + public interface IPlayerActions + { + void OnCameraMovement(InputAction.CallbackContext context); + void OnCameraRotate(InputAction.CallbackContext context); + void OnCameraZoom(InputAction.CallbackContext context); + void OnClick(InputAction.CallbackContext context); + } +} diff --git a/Assets/PlayerInputActions.cs.meta b/Assets/PlayerInputActions.cs.meta new file mode 100644 index 00000000..e6c133ac --- /dev/null +++ b/Assets/PlayerInputActions.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: d99443e8a27fbb45783355c973509b87 \ No newline at end of file diff --git a/Assets/PlayerInputActions.inputactions b/Assets/PlayerInputActions.inputactions new file mode 100644 index 00000000..56e50bc6 --- /dev/null +++ b/Assets/PlayerInputActions.inputactions @@ -0,0 +1,160 @@ +{ + "name": "PlayerInputActions", + "maps": [ + { + "name": "Player", + "id": "8be8563d-f86a-4180-9497-327208207374", + "actions": [ + { + "name": "CameraMovement", + "type": "Value", + "id": "3f95d77d-eac9-4f68-afc7-920a3cf21cf4", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "CameraRotate", + "type": "Value", + "id": "c2380b6a-5e64-438b-87a5-cfb0fc964439", + "expectedControlType": "Axis", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "CameraZoom", + "type": "Value", + "id": "98f04da7-af9e-43e7-86cf-14dffc7c11f3", + "expectedControlType": "Axis", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "Click", + "type": "Button", + "id": "ea8ff401-d690-47f2-9be5-538a55f1b170", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + } + ], + "bindings": [ + { + "name": "WASD", + "id": "46d35c2a-f806-4a90-a753-9d04ac7ae4e8", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraMovement", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "8450ee4c-47a0-4423-af62-2461fbd6c186", + "path": "/w", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraMovement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "df4bfb4f-545e-4e1e-9d71-054e257d7c76", + "path": "/s", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraMovement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "1d645d09-e51c-4beb-9733-e7a2587d0d01", + "path": "/a", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraMovement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "724c5104-33c0-44fe-88ba-a23037925ea9", + "path": "/d", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraMovement", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "1D Axis", + "id": "fefcfddb-2d96-4572-a396-5994ca7d56d8", + "path": "1DAxis", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraRotate", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "negative", + "id": "15042b53-da04-4ef2-95ef-d87cfe970ae6", + "path": "/e", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraRotate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "positive", + "id": "8126d771-a0cb-4485-a176-7cb6ba72a170", + "path": "/q", + "interactions": "", + "processors": "", + "groups": "", + "action": "CameraRotate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "", + "id": "7d750995-de9a-4511-b593-00673c503e02", + "path": "/scroll/y", + "interactions": "", + "processors": "Clamp(min=-1,max=1),Invert", + "groups": "", + "action": "CameraZoom", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "54249f40-90d3-4e81-83bf-352bedf3bdf3", + "path": "/leftButton", + "interactions": "", + "processors": "", + "groups": "", + "action": "Click", + "isComposite": false, + "isPartOfComposite": false + } + ] + } + ], + "controlSchemes": [] +} \ No newline at end of file diff --git a/Assets/PlayerInputActions.inputactions.meta b/Assets/PlayerInputActions.inputactions.meta new file mode 100644 index 00000000..7dfa1c28 --- /dev/null +++ b/Assets/PlayerInputActions.inputactions.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: 2e82aa231a37a3532bcb2a8422616cd7 +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3} + generateWrapperCode: 1 + wrapperCodePath: + wrapperClassName: + wrapperCodeNamespace: diff --git a/Assets/Scripts/Actions/UnitActionSystem.cs b/Assets/Scripts/Actions/UnitActionSystem.cs index f57e870f..cb5b4160 100644 --- a/Assets/Scripts/Actions/UnitActionSystem.cs +++ b/Assets/Scripts/Actions/UnitActionSystem.cs @@ -60,7 +60,7 @@ namespace Actions { } private void HandleSelectedAction() { - if (!InputManager.Instance.IsMouseButtonDown(0)) return; + if (!InputManager.Instance.IsMouseButtonDownThisFrame()) return; GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition()); if (!SelectedAction.IsValidActionGridPosition(mouseGridPosition)) return; if (!selectedUnit.TrySpendActionPointsToTakeAction(SelectedAction)) return; @@ -79,7 +79,7 @@ namespace Actions { public event EventHandler OnActionStarted; private bool TryHandleUnitSelection() { - if (!InputManager.Instance.IsMouseButtonDown(0)) return false; + if (!InputManager.Instance.IsMouseButtonDownThisFrame()) return false; if (!Physics.Raycast(Camera.main.ScreenPointToRay(InputManager.Instance.GetMouseScreenPosition()), out RaycastHit raycastHit, float.MaxValue, unitsLayerMask)) return false; if (!raycastHit.transform.TryGetComponent(out Unit unit) || unit is null) return false; if (unit == selectedUnit) return false; diff --git a/Assets/Scripts/InputManager.cs b/Assets/Scripts/InputManager.cs index 80109fe3..48b827fc 100644 --- a/Assets/Scripts/InputManager.cs +++ b/Assets/Scripts/InputManager.cs @@ -1,6 +1,9 @@ +#define USE_NEW_INPUT_SYSTEM using UnityEngine; +using UnityEngine.InputSystem; public class InputManager : MonoBehaviour { + private PlayerInputActions playerInputActions; public static InputManager Instance { get; private set; } private void Awake() { @@ -11,31 +14,59 @@ public class InputManager : MonoBehaviour { } Instance = this; + playerInputActions = new(); + playerInputActions.Player.Enable(); } - public Vector2 GetMouseScreenPosition() => Input.mousePosition; - public bool IsMouseButtonDown(int button) => Input.GetMouseButtonDown(button); + public Vector2 GetMouseScreenPosition() { +#if USE_NEW_INPUT_SYSTEM + return Mouse.current.position.ReadValue(); +#else + return Input.mousePosition; +#endif + } + + public bool IsMouseButtonDownThisFrame() { +#if USE_NEW_INPUT_SYSTEM + return playerInputActions.Player.Click.WasPressedThisFrame(); +#else + return Input.GetMouseButtonDown(0); +#endif + } public Vector2 GetCameraMoveVector() { +#if USE_NEW_INPUT_SYSTEM + return playerInputActions.Player.CameraMovement.ReadValue(); +#else Vector2 inputMoveDir = new(); if (Input.GetKey(KeyCode.W)) inputMoveDir.y = +1f; if (Input.GetKey(KeyCode.S)) inputMoveDir.y = -1f; if (Input.GetKey(KeyCode.A)) inputMoveDir.x = -1f; if (Input.GetKey(KeyCode.D)) inputMoveDir.x = +1f; return inputMoveDir; +#endif } public float GetCameraRotateAmount() { +#if USE_NEW_INPUT_SYSTEM + return playerInputActions.Player.CameraRotate.ReadValue(); +#else float rotateAmount = 0f; if (Input.GetKey(KeyCode.Q)) rotateAmount = +1f; if (Input.GetKey(KeyCode.E)) rotateAmount = -1f; return rotateAmount; +#endif } - public float GetCameraZoomAmount() => - Input.mouseScrollDelta.y switch { + public float GetCameraZoomAmount() { +#if USE_NEW_INPUT_SYSTEM + return playerInputActions.Player.CameraZoom.ReadValue(); +#else + return Input.mouseScrollDelta.y switch { > 0 => -1f, < 0 => +1f, _ => 0f }; +#endif + } } \ No newline at end of file diff --git a/Packages/manifest.json b/Packages/manifest.json index 65ff7c34..ff6d1ac9 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -3,6 +3,7 @@ "com.unity.2d.sprite": "1.0.0", "com.unity.cinemachine": "2.9.7", "com.unity.ide.rider": "3.0.24", + "com.unity.inputsystem": "1.6.1", "com.unity.probuilder": "5.0.7", "com.unity.render-pipelines.universal": "16.0.2", "com.unity.timeline": "1.8.3", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index a60645db..c162e5dd 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -40,6 +40,15 @@ }, "url": "https://packages.unity.com" }, + "com.unity.inputsystem": { + "version": "1.6.1", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.modules.uielements": "1.0.0" + }, + "url": "https://packages.unity.com" + }, "com.unity.mathematics": { "version": "1.2.6", "depth": 1, diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 417d1330..4b30b159 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -934,7 +934,7 @@ PlayerSettings: qnxGraphicConfPath: apiCompatibilityLevel: 6 captureStartupLogs: {} - activeInputHandler: 0 + activeInputHandler: 2 windowsGamepadBackendHint: 0 cloudProjectId: ab8efc0e-da20-4850-93a6-a4cbeb8383f8 framebufferDepthMemorylessMode: 0