Input
是 Unity 内置的静态类(属于 UnityEngine
命名空间),用于获取各种输入设备的原始数据(如键盘、鼠标、触摸、手柄等)。它是 Unity 旧输入系统(Legacy Input System)的核心入口,提供了大量静态属性和方法来访问输入状态
Input.touches
:返回当前所有活跃的触摸点数组(Touch[]
类型)。Input.touchCount
:返回当前触摸点的数量
我的上一篇EventSystem这篇文章中解释过的TouchInputModule
类正是通过 Input
类获取原始触摸数据,再将其转换为事件系统可识别的 PointerEventData
// TouchInputModule 内部的 ProcessTouchEvents 简化实现
protected virtual void ProcessTouchEvents()
{
// 通过 Input 类获取所有触摸点
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.touches[i];
int pointerId = touch.fingerId; // 触摸点唯一ID
// 将触摸数据转换为 PointerEventData
PointerEventData eventData;
GetPointerData(pointerId, out eventData, true);
eventData.position = touch.position; // 触摸位置(屏幕坐标)
// 根据触摸状态分发事件(按下、移动、抬起等)
if (touch.phase == TouchPhase.Began)
{
ProcessPointerPress(eventData); // 处理按下事件
}
else if (touch.phase == TouchPhase.Moved)
{
ProcessPointerMove(eventData); // 处理移动事件
}
else if (touch.phase == TouchPhase.Ended)
{
ProcessPointerRelease(eventData); // 处理抬起事件
}
}
}
输入相关直接看代码使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OldInputSystem : MonoBehaviour
{
void BasicInoutCheck()
{
//任意按键被按下
if (Input.anyKey)
{
Debug.Log("任意按键被按下");
}
//任意按键在在当前帧被按下
if (Input.anyKeyDown)
{
Debug.Log("任意按键在在当前帧被按下");
}
//重置所有输入轴
if (Input.GetKeyDown(KeyCode.R))
{
Input.ResetInputAxes();
Debug.Log("重置所有输入轴");
}
}
//鼠标操作
void MouseInputHandling()
{
//鼠标是否存在
if (Input.mousePresent)
{
//鼠标位置
Vector3 mousePos = Input.mousePosition;
//与屏幕的距离
Vector3 distance =
Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0);
Vector2 scroll = Input.mouseScrollDelta;
//鼠标滚轮
if (scroll.y != 0)
{
Debug.Log("鼠标滚轮滚动");
}
//鼠标按键按下 左键 右键 中间
if (Input.GetMouseButtonDown(0)) { }
if (Input.GetMouseButtonUp(1)) { }
if (Input.GetMouseButton(2)) { }
}
}
//键盘操作
void KeyboardInputHandling()
{
//按键检测 KeyCode枚举
if (Input.GetKeyDown(KeyCode.A)) { }
if (Input.GetKeyUp(KeyCode.A)) { }
if (Input.GetKey(KeyCode.A)) { }
if (Input.GetKeyUp(KeyCode.Return))
{
Debug.Log("Enter");
}
//组合键
if (Input.GetKey(KeyCode.LeftControl) && Input.GetKey(KeyCode.S))
{
Debug.Log("Ctrl+s");
}
}
//触摸
void TouchInputHandling()
{
//是否支持触摸
if (Input.touchSupported)
{
//触摸点数量
if (Input.touchCount > 0)
{
//获取第一个触摸点
Touch touch = Input.touches[0];
//触摸位置
Vector2 touchPos = Input.touches[0].position;
//触摸状态 开始 移动 静止 结束 取消
switch (touch.phase)
{
case TouchPhase.Began:
Debug.Log("开始");
break;
case TouchPhase.Moved:
Debug.Log("移动");
break;
case TouchPhase.Stationary:
Debug.Log("静止");
break;
case TouchPhase.Ended:
Debug.Log("结束");
break;
case TouchPhase.Canceled:
Debug.Log("取消");
break;
}
//多点触摸 扁你所有触摸点
foreach (Touch t in Input.touches)
{
Debug.Log(t.fingerId);
}
}
}
}
void ControllerInputHandling()
{
//手柄数量
// Debug.Log($"手柄数量:{Input.joystickCount}"); //AI说2019后的版本可用 (但不知道为什么我的2022版本报错下面的旧版却不报错)
// 获取手柄数量
int joystickCount = Input.GetJoystickNames().Length;
Debug.Log($"连接的手柄数量:{joystickCount}");
//手柄名称列表
string[] joystickNames = Input.GetJoystickNames();
foreach (string name in joystickNames)
{
Debug.Log($"手柄名称: {name}");
}
//手柄按钮 需要在InputManager中配置
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("Fire1按钮按下(通常是Ctrl或手柄按键)");
}
if (Input.GetButtonUp("Jump"))
{
Debug.Log("Jump按钮抬起_通常是空格或手柄按键");
}
//手柄摇杆
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (Mathf.Abs(horizontal) > 0.1f || Mathf.Abs(vertical) > 0.1f)
{
Debug.Log($"摇杆移动: X={horizontal}, Y={vertical}");
}
}
//虚拟轴
void VirtualAxisHandling()
{
//平滑轴
float horizontal = Input.GetAxis("Horizontal");
//原始轴
float vertical = Input.GetAxisRaw("Vertical");
//自定义轴(InputManager中配置)
float customAxis = Input.GetAxis("CustomAxis");
}
}