首页 > 游戏开发 >  Unity Input详解正文

Unity Input详解

1 Input 概念

Unity 的 Input 类用于检测用户的输入。

比如 键盘、鼠标、手柄或触摸屏。

通过这个类可以轻松地获取用户按下的按键、鼠标移动或触摸屏上的手势等,从而让游戏对象响应。


2 鼠标的屏幕坐标

Input.mousePosition

获取鼠标在屏幕上的坐标(以像素为单位)。

屏幕屏幕坐标的原点 是在 屏幕的左下角

往右是 X轴正方向

往上是 Y轴正方向

返回值是 Vector3

但是只有 x 和 y有值,z一直是0 (因为屏幕本来就是2D的 不存在Z轴)

print(Input.mousePosition);


3 检测鼠标输入

3.1 按键

int button 中

    0 表示左键

    1 表示右键

    2 表示中键

Input.GetMouseButton(int button)

    检测鼠标按钮是否按下。

Input.GetMouseButtonDown(int button)

    检测鼠标按钮是否刚刚被按下。

Input.GetMouseButtonUp(int button)

    检测鼠标按钮是否刚刚被松开。

//鼠标按键对应参数值
//0左键 1右键 2中键
//鼠标按下一瞬间
if( Input.GetMouseButtonDown(0) )
{
    print("鼠标左键按下了");
}
       
//鼠标抬起一瞬间
if( Input.GetMouseButtonUp(0) )
{
    print("鼠标左键抬起了");
}
//鼠标长按按下抬起(按住按键不放)
//当按住按键不放时 会一直满足该条件
if( Input.GetMouseButton(1))
{
    print("右键按下");
}


3.2 滚轮(鼠标中键)

Input.mouseScrollDelta 用于获取鼠标滚轮的滚动量。

返回值的形式是 (x, y)

通常只在 y 轴方向有变化(即 (0, y)),因为鼠标滚轮滚动通常沿着垂直方向。

不同的设备或设置可能会导致滚动量的差异

//中键滚动
//-1往下滚  0没有滚  1往上滚
//返回值 是 Vector2
//往下滚 (0.00, -1.00 * 滚动量)
//没有滚 (0.00, 0.00)
//往上滚 (0.00, 1.00 * 滚动量)
print(Input.mouseScrollDelta);


4 检测键盘输入

通过 Input 类检测键盘输入。

常用的方法有

Input.GetKey

检测按键是否处于按下状态

Input.GetKeyDown

检测按键是否刚刚被按下

Input.GetKeyUp

检测按键是否刚刚被松开

//键盘按下
if( Input.GetKeyDown(KeyCode.W) )
{
    print("W键按下");
}
//传入字符串的重载
//传入的字符串 不能大写 不然会报错
//只能传入小写字符串
if( Input.GetKeyDown("w") )
{
    print("W键按下");
}
//键盘抬起
if( Input.GetKeyUp(KeyCode.W) )
{
    print("W键抬起");
}
//键盘长按
if( Input.GetKey(KeyCode.W) )
{
    print("W键长按");
}


4.1 通过 W、A、S、D 控制游戏对象移动

void Update() {
    float moveSpeed = 1f;
    // transform.Translate() 等价于 this.transform.Translate()
    if (Input.GetKey(KeyCode.W)) {
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.S)) {
        transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.A)) {
        transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
    }
    if (Input.GetKey(KeyCode.D)) {
        transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
    }
}


5 检测默认轴输入

Unity提供了更方便的方法来帮助 控制 对象的位移和旋转

通过 Input.GetAxis 和 Input.GetAxisRaw 方法来检测默认的轴输入。

这些轴通常用于处理平滑的移动和控制,例如控制 角色的移动方向 或 摄像机的旋转。

默认的轴名称包括 Horizontal、Vertical、Mouse X、Mouse Y 等。

默认的轴名称可以通过Edit>>Project Settings>>Input Manager查找修改


5.1 键盘按键默认轴输入

Input.GetAxis("Horizontal")

Input.GetAxis("Vertical")

//键盘AD按下时 返回 -1到1之间的变换
//键盘SW按下时 返回 -1到1之间的变换
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// 使用输入值 上下左右移动角色
Vector3 movement = new Vector3(horizontalInput, 0, verticalInput);
transform.Translate(movement * Time.deltaTime * 5);
//GetAxisRaw方法 和 GetAxis使用方式相同
//只不过返回值 只会是 -1 0 1 不会有中间值


5.2 鼠标移动默认轴输入

Input.GetAxis("Mouse X")

Input.GetAxis("Mouse Y")

返回值是 -1 ~ 0 ~ 1 之间 的浮点数
//鼠标横向移动时 -1 到 1 左 右
//print(Input.GetAxis("Mouse X"));
//鼠标竖向移动时  -1 到 1 下 上
//print(Input.GetAxis("Mouse Y"));


5.3 Input.GetAxisRaw()

返回值 只有-1、0、1

float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(horizontalInput, 0, verticalInput);
transform.Translate(movement * Time.deltaTime * 5);


6 任意键

6.1 Input.anyKey

//是否有任意键或鼠标长按
if(Input.anyKey)
{
    print("有一个键长按");
}


6.2 Input.anyKeyDown

Input.inputString

实时检测:Input.inputString 返回的是当前帧中按下的键所对应的字符,只在按下某个键时返回值,在该帧中获取的字符会组成字符串。

字符输入:它只返回字符形式的输入(如字母、数字、符号),不会返回功能键(如 Shift、Ctrl、Alt 等)。

//是否有任意键或鼠标按下
if(Input.anyKeyDown)
{
    print("有一个键 按下");
    //这一帧的键盘输入
    print(Input.inputString);
}


7 检测手柄输入

//得到连接的手柄的所有按钮名字
string[] strs = Input.GetJoystickNames();
//某一个手柄键按下
if( Input.GetButtonDown("Jump") )
{
}
//某一个手柄键抬起
if (Input.GetButtonUp("Jump"))
{
}
//某一个手柄键长按
if (Input.GetButton("Jump"))
{
}


8 移动设备触摸相关

//移动设备触摸相关
if(Input.touchCount > 0)
{
    Touch t1 = Input.touches[0];
    //位置
    print(t1.position);
    //相对上次位置的变化
    print(t1.deltaPosition);
}
//是否启用多点触控
Input.multiTouchEnabled = false;
//陀螺仪(重力感应)
//是否开启陀螺仪 必须开启 才能正常使用
Input.gyro.enabled = true;
//重力加速度向量
print(Input.gyro.gravity);
//旋转速度
print(Input.gyro.rotationRate);
//陀螺仪 当前的旋转四元数 
print(Input.gyro.attitude);