New Input System

Polish
Sascha 2023-06-19 16:16:24 +07:00
parent 70e59eeaae
commit 193db85940
9 changed files with 547 additions and 7 deletions

@ -0,0 +1,323 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
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"": ""<Keyboard>/w"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""CameraMovement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""df4bfb4f-545e-4e1e-9d71-054e257d7c76"",
""path"": ""<Keyboard>/s"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""CameraMovement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""1d645d09-e51c-4beb-9733-e7a2587d0d01"",
""path"": ""<Keyboard>/a"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""CameraMovement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""724c5104-33c0-44fe-88ba-a23037925ea9"",
""path"": ""<Keyboard>/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"": ""<Keyboard>/e"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""CameraRotate"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""positive"",
""id"": ""8126d771-a0cb-4485-a176-7cb6ba72a170"",
""path"": ""<Keyboard>/q"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""CameraRotate"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": """",
""id"": ""7d750995-de9a-4511-b593-00673c503e02"",
""path"": ""<Mouse>/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"": ""<Mouse>/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<InputDevice>? devices
{
get => asset.devices;
set => asset.devices = value;
}
public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;
public bool Contains(InputAction action)
{
return asset.Contains(action);
}
public IEnumerator<InputAction> GetEnumerator()
{
return asset.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Enable()
{
asset.Enable();
}
public void Disable()
{
asset.Disable();
}
public IEnumerable<InputBinding> 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<IPlayerActions> m_PlayerActionsCallbackInterfaces = new List<IPlayerActions>();
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);
}
}

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d99443e8a27fbb45783355c973509b87

@ -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": "<Keyboard>/w",
"interactions": "",
"processors": "",
"groups": "",
"action": "CameraMovement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "down",
"id": "df4bfb4f-545e-4e1e-9d71-054e257d7c76",
"path": "<Keyboard>/s",
"interactions": "",
"processors": "",
"groups": "",
"action": "CameraMovement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "left",
"id": "1d645d09-e51c-4beb-9733-e7a2587d0d01",
"path": "<Keyboard>/a",
"interactions": "",
"processors": "",
"groups": "",
"action": "CameraMovement",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "right",
"id": "724c5104-33c0-44fe-88ba-a23037925ea9",
"path": "<Keyboard>/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": "<Keyboard>/e",
"interactions": "",
"processors": "",
"groups": "",
"action": "CameraRotate",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "positive",
"id": "8126d771-a0cb-4485-a176-7cb6ba72a170",
"path": "<Keyboard>/q",
"interactions": "",
"processors": "",
"groups": "",
"action": "CameraRotate",
"isComposite": false,
"isPartOfComposite": true
},
{
"name": "",
"id": "7d750995-de9a-4511-b593-00673c503e02",
"path": "<Mouse>/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": "<Mouse>/leftButton",
"interactions": "",
"processors": "",
"groups": "",
"action": "Click",
"isComposite": false,
"isPartOfComposite": false
}
]
}
],
"controlSchemes": []
}

@ -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:

@ -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;

@ -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<Vector2>();
#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<float>();
#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<float>();
#else
return Input.mouseScrollDelta.y switch {
> 0 => -1f,
< 0 => +1f,
_ => 0f
};
#endif
}
}

@ -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",

@ -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,

@ -934,7 +934,7 @@ PlayerSettings:
qnxGraphicConfPath:
apiCompatibilityLevel: 6
captureStartupLogs: {}
activeInputHandler: 0
activeInputHandler: 2
windowsGamepadBackendHint: 0
cloudProjectId: ab8efc0e-da20-4850-93a6-a4cbeb8383f8
framebufferDepthMemorylessMode: 0