From 4a2f04bc036ccebacf1947e770ed953edb92566a Mon Sep 17 00:00:00 2001 From: BuyMyMojo Date: Sun, 26 Feb 2023 02:42:14 +1100 Subject: [PATCH] Move to new input system --- Assets/PlayerInputActions.cs | 211 ++++++++++++++++++++ Assets/PlayerInputActions.cs.meta | 11 + Assets/PlayerInputActions.inputactions | 78 ++++++++ Assets/PlayerInputActions.inputactions.meta | 14 ++ Assets/Scripts/GameInput.cs | 27 +-- 5 files changed, 323 insertions(+), 18 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 0000000..ef7e4bd --- /dev/null +++ b/Assets/PlayerInputActions.cs @@ -0,0 +1,211 @@ +//------------------------------------------------------------------------------ +// +// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator +// version 1.5.0 +// 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"": ""f8e0b637-f75b-461a-9bb5-2d30dff139f5"", + ""actions"": [ + { + ""name"": ""Move"", + ""type"": ""Value"", + ""id"": ""cbd84e00-2ff4-4f1a-aba7-e70ef32df0dd"", + ""expectedControlType"": ""Vector2"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": true + } + ], + ""bindings"": [ + { + ""name"": ""WASD"", + ""id"": ""88272aa4-9de4-4969-bf34-a8cf0ed36447"", + ""path"": ""2DVector"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Move"", + ""isComposite"": true, + ""isPartOfComposite"": false + }, + { + ""name"": ""up"", + ""id"": ""79392300-286e-4514-9d4d-80dcf0228dde"", + ""path"": ""/w"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Move"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""down"", + ""id"": ""2b3f2378-274e-4c40-a504-6f8a9acecee6"", + ""path"": ""/s"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Move"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""left"", + ""id"": ""fe756558-4d52-48da-8920-d4d0cda44f93"", + ""path"": ""/a"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Move"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""right"", + ""id"": ""fdfbd92c-6699-4560-a60e-bd40d65c4130"", + ""path"": ""/d"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Move"", + ""isComposite"": false, + ""isPartOfComposite"": true + } + ] + } + ], + ""controlSchemes"": [] +}"); + // Player + m_Player = asset.FindActionMap("Player", throwIfNotFound: true); + m_Player_Move = m_Player.FindAction("Move", 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_Move; + public struct PlayerActions + { + private @PlayerInputActions m_Wrapper; + public PlayerActions(@PlayerInputActions wrapper) { m_Wrapper = wrapper; } + public InputAction @Move => m_Wrapper.m_Player_Move; + 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); + @Move.started += instance.OnMove; + @Move.performed += instance.OnMove; + @Move.canceled += instance.OnMove; + } + + private void UnregisterCallbacks(IPlayerActions instance) + { + @Move.started -= instance.OnMove; + @Move.performed -= instance.OnMove; + @Move.canceled -= instance.OnMove; + } + + 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 OnMove(InputAction.CallbackContext context); + } +} diff --git a/Assets/PlayerInputActions.cs.meta b/Assets/PlayerInputActions.cs.meta new file mode 100644 index 0000000..dd9753e --- /dev/null +++ b/Assets/PlayerInputActions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1e57aba2789be6d4492072a4cd75c6a6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PlayerInputActions.inputactions b/Assets/PlayerInputActions.inputactions new file mode 100644 index 0000000..8bdff39 --- /dev/null +++ b/Assets/PlayerInputActions.inputactions @@ -0,0 +1,78 @@ +{ + "name": "PlayerInputActions", + "maps": [ + { + "name": "Player", + "id": "f8e0b637-f75b-461a-9bb5-2d30dff139f5", + "actions": [ + { + "name": "Move", + "type": "Value", + "id": "cbd84e00-2ff4-4f1a-aba7-e70ef32df0dd", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + } + ], + "bindings": [ + { + "name": "WASD", + "id": "88272aa4-9de4-4969-bf34-a8cf0ed36447", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "79392300-286e-4514-9d4d-80dcf0228dde", + "path": "/w", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "2b3f2378-274e-4c40-a504-6f8a9acecee6", + "path": "/s", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "fe756558-4d52-48da-8920-d4d0cda44f93", + "path": "/a", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "fdfbd92c-6699-4560-a60e-bd40d65c4130", + "path": "/d", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + } + ] + } + ], + "controlSchemes": [] +} \ No newline at end of file diff --git a/Assets/PlayerInputActions.inputactions.meta b/Assets/PlayerInputActions.inputactions.meta new file mode 100644 index 0000000..3df8cd5 --- /dev/null +++ b/Assets/PlayerInputActions.inputactions.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: 2cc88895124e29e43bcdf35cd0025738 +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/GameInput.cs b/Assets/Scripts/GameInput.cs index d8bbc9a..33869ca 100644 --- a/Assets/Scripts/GameInput.cs +++ b/Assets/Scripts/GameInput.cs @@ -4,26 +4,17 @@ using UnityEngine; public class GameInput : MonoBehaviour { + + private PlayerInputActions playerInputActions; + + private void Awake() + { + playerInputActions = new PlayerInputActions(); + playerInputActions.Player.Enable(); + } public Vector2 GetMovementVectorNormalized() { - Vector2 inputVector = new Vector2(0, 0); - - if (Input.GetKey(KeyCode.W)) - { - inputVector.y = +1; - } - if (Input.GetKey(KeyCode.S)) - { - inputVector.y = -1; - } - if (Input.GetKey(KeyCode.A)) - { - inputVector.x = -1; - } - if (Input.GetKey(KeyCode.D)) - { - inputVector.x = +1; - } + Vector2 inputVector = playerInputActions.Player.Move.ReadValue(); inputVector = inputVector.normalized;