unity基础——接受键盘和鼠标等输入

1. 旧输入系统(Input Manager)

1.1 键盘输入

        Unity 的旧输入系统使用Input类来处理键盘输入。可以通过Input.GetKeyInput.GetKeyDownInput.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.GetMouseButtonInput.GetMouseButtonDownInput.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.GetAxisInput.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设置为BothInput 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 中通过键盘、鼠标、虚拟轴和手机触屏进行输入控制的详细介绍和代码示例。根据项目需求,可以选择使用旧输入系统或新输入系统。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值