Unity InputSystem 实战(一)
Unity InputSystem 实战(二)
本篇文章实战基于Unity2023.1.0a26版本:1.5版本官网地址
Unity推出的新版的输入系统,旧版输入系统很大概率要弃用了,所以抓紧来学习下吧
注意:新的输入系统需要Unity 2019.4+和.NET 4运行时。它在使用旧的.NET 3.5运行时的项目中不起作用
打开Window > Package Manager
后续也可以在 Edit > Project Settings > Player 中找到 Active Input Handling选项,选择使用旧版输入系统还是新版输入系统,亦或是两个同时存在
Project面板中右键 Create > Input Actions,就会生成一个inputactions文件,取名为GameControls
接下来双击 GameControls.inputactions 配置文件
我们实现一个控制玩家上下左右移动的Action
为什么这样设置呢?你可以发现ActionType有三种类型,根据大概的说明,可以知道我们需要连续输入,并且移动是需要获得一个Vector2类型的参数(x,y)
Value | 主要用于状态连续更改的输入,例如鼠标的移动,手柄的遥感。如果有多个设备绑定这个Action,只会发送其中一个设备(最受控制的)的输入 |
---|---|
Button | 用于每次按下时触发的Action |
Pass-Through | 和Value一样,区别在于如果有多个设备绑定这个Action,会发送所有设备的输入 |
binding | 给Action添加binding的设备操作 |
---|---|
Add Binding | 普通的绑定,可以绑定一个按钮,光标,遥杆等 |
Add Up\Down\Left\Right Composite | 四个按钮的组合,返回值为Vector2,例如WASD |
Add Binding With One Modifier | 需要同时按下两个按钮的组合,例如ctrl+1 |
Add Bingding With Two Modifiers | 需要同时按下三个按钮的组合,例如shift+ctrl+1 |
现在我们就添加好了PC端键盘输入
移动端相关的设置就完成了,我们设置了两种平台,后续大家可以根据情况自由配置想要的平台
using UnityEngine;
public class InputManager : MonoBehaviour
{
public static GameControls gameControls { get; private set; }
private void OnEnable()
{
if (gameControls == null)
gameControls = new GameControls();
gameControls.Enable();
}
private void OnDisable()
{
if (gameControls != null)
gameControls.Disable();
}
}
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("主角移速")]
public float moveSpeed;
private Rigidbody2D playerRB;
// 缓存移动方向
private float input_X;
private float input_Y;
private Vector2 input_MoveDir;
private void Awake()
{
playerRB = GetComponent();
}
private void Update()
{
PlayerInput();
}
private void FixedUpdate()
{
Move();
}
private void PlayerInput()
{
input_X = Input.GetAxisRaw("Horizontal");
input_Y = Input.GetAxisRaw("Vertical");
if (input_X != 0 && input_Y != 0)
{
input_X = input_X * 0.7f;
input_Y = input_Y * 0.7f;
}
input_MoveDir = new Vector2(input_X, input_Y);
}
private void Move()
{
playerRB.MovePosition(playerRB.position + input_MoveDir * moveSpeed * Time.deltaTime);
}
}
Phase | Description |
---|---|
Disabled | 操作已禁用,无法接收输入 |
Waiting | 操作已启用,并且正在积极等待输入 |
Started | 输入系统已接收到启动与动作交互的输入 |
Performed | 已完成与操作的交互并持续监听 |
Canceled | 已取消与操作的交互 |
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("主角移速")]
public float moveSpeed;
private Rigidbody2D playerRB;
// 缓存移动方向
private float input_X;
private float input_Y;
private Vector2 input_MoveDir;
private void Awake()
{
playerRB = GetComponent();
}
void Start()
{
InputManager.gameControls.Player.Move.performed += OnMovePerformed;
}
private void OnDisable()
{
InputManager.gameControls.Player.Move.performed -= OnMovePerformed;
}
private void OnMovePerformed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Vector2 moveDir = obj.ReadValue();
input_X = moveDir.x;
input_Y = moveDir.y;
if (input_X != 0 && input_Y != 0)
{
input_X = input_X * 0.7f;
input_Y = input_Y * 0.7f;
}
input_MoveDir = new Vector2(input_X, input_Y);
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
playerRB.MovePosition(playerRB.position + input_MoveDir * moveSpeed * Time.deltaTime);
}
}
using System;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("主角移速")]
public float moveSpeed;
private Rigidbody2D playerRB;
// 缓存移动方向
private float input_X;
private float input_Y;
private Vector2 input_MoveDir;
private void Awake()
{
playerRB = GetComponent();
}
void Start()
{
InputManager.gameControls.Player.Move.performed += OnMovePerformed;
InputManager.gameControls.Player.Move.canceled += OnMoveCanceled;
}
private void OnDisable()
{
InputManager.gameControls.Player.Move.performed -= OnMovePerformed;
InputManager.gameControls.Player.Move.canceled -= OnMoveCanceled;
}
private void OnMovePerformed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Vector2 moveDir = obj.ReadValue();
input_X = moveDir.x;
input_Y = moveDir.y;
if (input_X != 0 && input_Y != 0)
{
input_X = input_X * 0.7f;
input_Y = input_Y * 0.7f;
}
input_MoveDir = new Vector2(input_X, input_Y);
}
private void OnMoveCanceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
input_X = 0;
input_Y = 0;
input_MoveDir = Vector2.zero;
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
playerRB.MovePosition(playerRB.position + input_MoveDir * moveSpeed * Time.deltaTime);
}
}
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
private void Update()
{
foreach (var touch in Touch.activeTouches)
Debug.Log($"{touch.touchId}: {touch.screenPosition},{touch.phase}");
}