1. 旧输入系统(Input Manager)
1.1 键盘输入
Unity 的旧输入系统使用Input类来处理键盘输入。可以通过Input.GetKey、Input.GetKeyDown和Input.GetKeyUp方法来检测键盘按键的状态。
Input.GetKey:检测某个按键是否被按住。Input.GetKeyDown:检测某个按键是否刚刚被按下。Input.GetKeyUp:检测某个按键是否刚刚被释放。
代码示例:
using UnityEngine;
public class KeyboardInputExample : MonoBehaviour
{
void Update()
{
// 检测W键是否被按住
if (Input.GetKey(KeyCode.W))
{
Debug.Log("W键被按住");
}
// 检测Space键是否刚刚被按下
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Space键刚刚被按下");
}
// 检测A键是否刚刚被释放
if (Input.GetKeyUp(KeyCode.A))
{
Debug.Log("A键刚刚被释放");
}
}
}
1.2 鼠标输入
鼠标输入同样可以使用Input类来处理。常用的方法有Input.GetMouseButton、Input.GetMouseButtonDown和Input.GetMouseButtonUp,以及获取鼠标位置的Input.mousePosition。
Input.GetMouseButton:检测鼠标某个按键是否被按住。Input.GetMouseButtonDown:检测鼠标某个按键是否刚刚被按下。Input.GetMouseButtonUp:检测鼠标某个按键是否刚刚被释放。
代码示例:
using UnityEngine;
public class MouseInputExample : MonoBehaviour
{
void Update()
{
// 检测鼠标左键是否被按住
if (Input.GetMouseButton(0))
{
Debug.Log("鼠标左键被按住");
}
// 检测鼠标右键是否刚刚被按下
if (Input.GetMouseButtonDown(1))
{
Debug.Log("鼠标右键刚刚被按下");
}
// 检测鼠标中键是否刚刚被释放
if (Input.GetMouseButtonUp(2))
{
Debug.Log("鼠标中键刚刚被释放");
}
// 获取鼠标位置
Vector3 mousePosition = Input.mousePosition;
Debug.Log("鼠标位置: " + mousePosition);
}
}
1.3 虚拟轴输入
虚拟轴是 Unity 旧输入系统中用于处理方向输入的一种方式,常见的虚拟轴有Horizontal(水平方向)和Vertical(垂直方向)。可以通过Input.GetAxis和Input.GetAxisRaw方法来获取虚拟轴的值。
Input.GetAxis:返回一个平滑处理后的值,范围在 -1 到 1 之间。Input.GetAxisRaw:返回未经平滑处理的值,只有 -1、0 或 1。
代码示例:
using UnityEngine;
public class VirtualAxisInputExample : MonoBehaviour
{
void Update()
{
// 获取水平虚拟轴的值
float horizontalInput = Input.GetAxis("Horizontal");
Debug.Log("水平虚拟轴的值: " + horizontalInput);
// 获取垂直虚拟轴的原始值
float verticalInputRaw = Input.GetAxisRaw("Vertical");
Debug.Log("垂直虚拟轴的原始值: " + verticalInputRaw);
}
}
1.4 手机触屏输入
在旧输入系统中,可以使用Input.touches数组来处理手机触屏输入。每个Touch对象包含了触摸点的状态、位置等信息。
代码示例:
using UnityEngine;
public class TouchInputExample : MonoBehaviour
{
void Update()
{
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.touches[i];
switch (touch.phase)
{
case TouchPhase.Began:
Debug.Log("触摸点 " + i + " 刚刚开始触摸");
break;
case TouchPhase.Moved:
Debug.Log("触摸点 " + i + " 正在移动");
break;
case TouchPhase.Stationary:
Debug.Log("触摸点 " + i + " 静止不动");
break;
case TouchPhase.Ended:
Debug.Log("触摸点 " + i + " 触摸结束");
break;
case TouchPhase.Canceled:
Debug.Log("触摸点 " + i + " 触摸被取消");
break;
}
}
}
}
}
2. 新输入系统(Input System)
2.1 安装和配置新输入系统
在 Unity 2023 中,新输入系统需要手动安装。可以通过Window -> Package Manager,搜索Input System并安装。安装完成后,需要在Project Settings -> Player -> Other Settings中,将Active Input Handling设置为Both或Input System Package。
2.2 创建输入动作
在Assets目录下右键 -> Create -> Input Actions,创建一个新的输入动作文件。在输入动作编辑器中,可以定义各种输入动作,如键盘按键、鼠标点击、触屏手势等。
2.3 代码实现
以下是一个使用新输入系统处理键盘、鼠标和触屏输入的示例:
using UnityEngine;
using UnityEngine.InputSystem;
public class NewInputSystemExample : MonoBehaviour
{
private PlayerInputActions playerInputActions;
private void Awake()
{
playerInputActions = new PlayerInputActions();
}
private void OnEnable()
{
playerInputActions.Player.Enable();
playerInputActions.Player.Move.performed += OnMove;
playerInputActions.Player.Fire.performed += OnFire;
playerInputActions.Player.Touch.performed += OnTouch;
}
private void OnDisable()
{
playerInputActions.Player.Disable();
playerInputActions.Player.Move.performed -= OnMove;
playerInputActions.Player.Fire.performed -= OnFire;
playerInputActions.Player.Touch.performed -= OnTouch;
}
private void OnMove(InputAction.CallbackContext context)
{
Vector2 moveInput = context.ReadValue<Vector2>();
Debug.Log("移动输入: " + moveInput);
}
private void OnFire(InputAction.CallbackContext context)
{
Debug.Log("开火输入");
}
private void OnTouch(InputAction.CallbackContext context)
{
Vector2 touchPosition = context.ReadValue<Vector2>();
Debug.Log("触摸位置: " + touchPosition);
}
}
在上述代码中,PlayerInputActions是根据输入动作文件自动生成的类。通过订阅输入动作的performed事件,可以在输入发生时执行相应的逻辑。
3.结语
以上就是 Unity 2023 中通过键盘、鼠标、虚拟轴和手机触屏进行输入控制的详细介绍和代码示例。根据项目需求,可以选择使用旧输入系统或新输入系统。



977

被折叠的 条评论
为什么被折叠?



