From 72c24ac8a7ea186d224ac6972ff8aa7e6c718329 Mon Sep 17 00:00:00 2001 From: Vova <3emaster@gmail.com> Date: Thu, 4 Jan 2024 08:48:04 +0200 Subject: [PATCH] new input system movement --- Assets/Scenes/Level 1.unity | 199 +++++++++-------- Assets/Scripts/InputActions.cs | 211 ++++++++++++++++++ Assets/Scripts/InputActions.cs.meta | 2 + ...inputactions => InputActions.inputactions} | 16 +- ...ns.meta => InputActions.inputactions.meta} | 2 +- Assets/Scripts/Player.cs | 12 +- 6 files changed, 339 insertions(+), 103 deletions(-) create mode 100644 Assets/Scripts/InputActions.cs create mode 100644 Assets/Scripts/InputActions.cs.meta rename Assets/Scripts/{Controls.inputactions => InputActions.inputactions} (88%) rename Assets/Scripts/{Controls.inputactions.meta => InputActions.inputactions.meta} (93%) diff --git a/Assets/Scenes/Level 1.unity b/Assets/Scenes/Level 1.unity index 194c62f..a9fa502 100644 --- a/Assets/Scenes/Level 1.unity +++ b/Assets/Scenes/Level 1.unity @@ -2658,6 +2658,7 @@ RectTransform: m_Children: - {fileID: 416410545} - {fileID: 1371746635} + - {fileID: 1196209005} m_Father: {fileID: 1123299336} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} @@ -5166,6 +5167,117 @@ Transform: - {fileID: 517556705} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1196209004 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1196209005} + - component: {fileID: 1196209007} + - component: {fileID: 1196209006} + - component: {fileID: 1196209008} + m_Layer: 0 + m_Name: Image + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1196209005 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1196209004} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 410068209} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 250, y: 250} + m_SizeDelta: {x: 100, y: 100} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1196209006 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1196209004} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 0} + m_Type: 0 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &1196209007 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1196209004} + m_CullTransparentMesh: 1 +--- !u!114 &1196209008 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1196209004} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e9d677f1681015749b15c436eec6d880, type: 3} + m_Name: + m_EditorClassIdentifier: + m_MovementRange: 50 + m_DynamicOriginRange: 100 + m_ControlPath: /leftStick + m_Behaviour: 0 + m_UseIsolatedInputActions: 0 + m_PointerDownAction: + m_Name: + m_Type: 0 + m_ExpectedControlType: + m_Id: 796379a7-aae5-4fb4-bdd5-6bb86d9649ba + m_Processors: + m_Interactions: + m_SingletonActionBindings: [] + m_Flags: 0 + m_PointerMoveAction: + m_Name: + m_Type: 0 + m_ExpectedControlType: + m_Id: 3a6770b9-e924-415b-b3d2-d3cc3ab8a4fe + m_Processors: + m_Interactions: + m_SingletonActionBindings: [] + m_Flags: 0 --- !u!1001 &1207263181 PrefabInstance: m_ObjectHideFlags: 0 @@ -6131,92 +6243,6 @@ Transform: m_CorrespondingSourceObject: {fileID: 7047733282028940670, guid: 934a57b833e872445ac8463836f55a44, type: 3} m_PrefabInstance: {fileID: 2139552518} m_PrefabAsset: {fileID: 0} ---- !u!1 &1452657734 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1452657736} - - component: {fileID: 1452657735} - m_Layer: 0 - m_Name: Circle - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!212 &1452657735 -SpriteRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1452657734} - m_Enabled: 1 - m_CastShadows: 0 - m_ReceiveShadows: 0 - m_DynamicOccludee: 1 - m_StaticShadowCaster: 0 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 0 - m_RayTraceProcedural: 0 - m_RayTracingAccelStructBuildFlagsOverride: 0 - m_RayTracingAccelStructBuildFlags: 1 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 0 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: -2136795773 - m_SortingLayer: 4 - m_SortingOrder: 0 - m_Sprite: {fileID: -2413806693520163455, guid: a86470a33a6bf42c4b3595704624658b, type: 3} - m_Color: {r: 1, g: 1, b: 1, a: 0.5803922} - m_FlipX: 0 - m_FlipY: 0 - m_DrawMode: 0 - m_Size: {x: 1, y: 1} - m_AdaptiveModeThreshold: 0.5 - m_SpriteTileMode: 0 - m_WasSpriteAssigned: 1 - m_MaskInteraction: 0 - m_SpriteSortPoint: 0 ---- !u!4 &1452657736 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1452657734} - serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -7.59, y: -3.8, z: 0} - m_LocalScale: {x: 2, y: 2, z: 2} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &1503237825 PrefabInstance: m_ObjectHideFlags: 0 @@ -7880,4 +7906,3 @@ SceneRoots: - {fileID: 1173618893} - {fileID: 1123299336} - {fileID: 1269535137} - - {fileID: 1452657736} diff --git a/Assets/Scripts/InputActions.cs b/Assets/Scripts/InputActions.cs new file mode 100644 index 0000000..2e4c604 --- /dev/null +++ b/Assets/Scripts/InputActions.cs @@ -0,0 +1,211 @@ +//------------------------------------------------------------------------------ +// +// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator +// version 1.7.0 +// from Assets/Scripts/InputActions.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 @InputActions: IInputActionCollection2, IDisposable +{ + public InputActionAsset asset { get; } + public @InputActions() + { + asset = InputActionAsset.FromJson(@"{ + ""name"": ""InputActions"", + ""maps"": [ + { + ""name"": ""Player"", + ""id"": ""a9631db6-afdf-47e4-a8a3-d63ab06f361c"", + ""actions"": [ + { + ""name"": ""Movement"", + ""type"": ""Value"", + ""id"": ""bc1a3c6a-5454-4e22-a4c4-e507bd5ef719"", + ""expectedControlType"": ""Vector2"", + ""processors"": """", + ""interactions"": """", + ""initialStateCheck"": true + } + ], + ""bindings"": [ + { + ""name"": ""2D Vector"", + ""id"": ""ec20865d-29b5-4923-a398-026e3b6a452a"", + ""path"": ""2DVector"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": true, + ""isPartOfComposite"": false + }, + { + ""name"": ""up"", + ""id"": ""ec9e58f5-3e87-4b8e-98a0-d6f57dee5a62"", + ""path"": ""/upArrow"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""down"", + ""id"": ""6833e932-d60c-49ad-84eb-2f63e3847d53"", + ""path"": ""/downArrow"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""left"", + ""id"": ""aee692a9-6d5f-4fea-b555-8dc6c147efbd"", + ""path"": ""/leftArrow"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + }, + { + ""name"": ""right"", + ""id"": ""ba2ffdd7-4da3-4b20-9b49-14448c0a8a15"", + ""path"": ""/rightArrow"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""Movement"", + ""isComposite"": false, + ""isPartOfComposite"": true + } + ] + } + ], + ""controlSchemes"": [] +}"); + // Player + m_Player = asset.FindActionMap("Player", throwIfNotFound: true); + m_Player_Movement = m_Player.FindAction("Movement", 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_Movement; + public struct PlayerActions + { + private @InputActions m_Wrapper; + public PlayerActions(@InputActions wrapper) { m_Wrapper = wrapper; } + public InputAction @Movement => m_Wrapper.m_Player_Movement; + 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); + @Movement.started += instance.OnMovement; + @Movement.performed += instance.OnMovement; + @Movement.canceled += instance.OnMovement; + } + + private void UnregisterCallbacks(IPlayerActions instance) + { + @Movement.started -= instance.OnMovement; + @Movement.performed -= instance.OnMovement; + @Movement.canceled -= instance.OnMovement; + } + + 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 OnMovement(InputAction.CallbackContext context); + } +} diff --git a/Assets/Scripts/InputActions.cs.meta b/Assets/Scripts/InputActions.cs.meta new file mode 100644 index 0000000..e8ee88e --- /dev/null +++ b/Assets/Scripts/InputActions.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 18dbff63b01a846438c203369e377e9a \ No newline at end of file diff --git a/Assets/Scripts/Controls.inputactions b/Assets/Scripts/InputActions.inputactions similarity index 88% rename from Assets/Scripts/Controls.inputactions rename to Assets/Scripts/InputActions.inputactions index 3c28a9e..6474de6 100644 --- a/Assets/Scripts/Controls.inputactions +++ b/Assets/Scripts/InputActions.inputactions @@ -1,12 +1,12 @@ { - "name": "Controls", + "name": "InputActions", "maps": [ { - "name": "player", + "name": "Player", "id": "a9631db6-afdf-47e4-a8a3-d63ab06f361c", "actions": [ { - "name": "New action", + "name": "Movement", "type": "Value", "id": "bc1a3c6a-5454-4e22-a4c4-e507bd5ef719", "expectedControlType": "Vector2", @@ -23,7 +23,7 @@ "interactions": "", "processors": "", "groups": "", - "action": "New action", + "action": "Movement", "isComposite": true, "isPartOfComposite": false }, @@ -34,7 +34,7 @@ "interactions": "", "processors": "", "groups": "", - "action": "New action", + "action": "Movement", "isComposite": false, "isPartOfComposite": true }, @@ -45,7 +45,7 @@ "interactions": "", "processors": "", "groups": "", - "action": "New action", + "action": "Movement", "isComposite": false, "isPartOfComposite": true }, @@ -56,7 +56,7 @@ "interactions": "", "processors": "", "groups": "", - "action": "New action", + "action": "Movement", "isComposite": false, "isPartOfComposite": true }, @@ -67,7 +67,7 @@ "interactions": "", "processors": "", "groups": "", - "action": "New action", + "action": "Movement", "isComposite": false, "isPartOfComposite": true } diff --git a/Assets/Scripts/Controls.inputactions.meta b/Assets/Scripts/InputActions.inputactions.meta similarity index 93% rename from Assets/Scripts/Controls.inputactions.meta rename to Assets/Scripts/InputActions.inputactions.meta index e5cb8cd..885edd6 100644 --- a/Assets/Scripts/Controls.inputactions.meta +++ b/Assets/Scripts/InputActions.inputactions.meta @@ -8,7 +8,7 @@ ScriptedImporter: assetBundleName: assetBundleVariant: script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3} - generateWrapperCode: 0 + generateWrapperCode: 1 wrapperCodePath: wrapperClassName: wrapperCodeNamespace: diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 850db7e..7cb9a6c 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -1,9 +1,6 @@ using System; using UnityEngine; - - - public class Player : Character { [SerializeField] @@ -30,10 +27,11 @@ public class Player : Character private GameObject _hammer; private bool _isHoldingHammer = true; - public event EventHandler OnPlayerTakeItem; + public InputActions _inputActions = new InputActions(); + private void Awake() { if (Instance != null) @@ -45,8 +43,7 @@ public class Player : Character PlayerPrefs.SetString("lastExitName", string.Empty); Instance = this; DontDestroyOnLoad(gameObject); - - + _inputActions.Enable(); } public void AddCoin() @@ -88,7 +85,8 @@ public class Player : Character _animator.SetTrigger("Body_ThrowHammer"); } } - base.MoveTo(Input.GetAxisRaw("Horizontal"), isAllowVertical? Input.GetAxisRaw("Vertical"):0); + var move=_inputActions.Player.Movement.ReadValue(); + base.MoveTo(move.x, isAllowVertical? move.y:0); } public void ThrowHammerObject()