找回密码
 立即注册
查看: 336|回复: 0

InputSystem 新版Unity输入系统学习(1)

[复制链接]
发表于 2023-4-1 13:50 | 显示全部楼层 |阅读模式
这个学习可能会比较长,先把一个类的写完,然后再考虑分P
1inputSystem需要unity2019.4以上版本+.net4runtime
它相对于老的输入系统更具拓展性和可自定义的替代方案。
inputSystem是Unity提供的一套更加方便用于在多平台操作模式间转换的系统。

使用方式:

导入inputSystem包,导入完成后会提示重启Unity,这是正常的。
然后右键Asset空白区,便可以在右键菜单中选择inputActions来创建一个空表。



可以在Path处选择这个操作的目标按键--现在选择的是鼠标左键LeftButton

配置文件配置完成后可以创建playerinput脚本并将配置挂载在其上来使用


——————————
新老输入系统的区别

老输入系统我们需要自己去写很多的检测代码来判断设备输入,并且处理对应逻辑。
而新输入系统不仅可以像老输入系统一样使用,还可以使用配置输入的概念,让我们可以在Unity内进行输入配置文件,不需要写代码来判断,(无限的#ifelse 有救了)。只需要把工作重心放在逻辑处理上。
——————
新老输入系统切换,可以在设置中修改启动那种输入模式,可以同时启用,也可以只启用其中之一,每次启用后会重启Unity。
//设置位置 file - build setting -Player Setting -other - Active input Handli
——————————————————
使用新版本inputSystem进行简单的按键功能添加。(代码型)

//获取输入设备
        Keyboard Key = Keyboard.current;
        //按键按下 -- 首先获取键盘设备,然后通过键盘设备.出所需要的按键。
        if (Key.aKey.wasPressedThisFrame)//判断这个按键是否按下。 wasPressedThisFrame 按下
        {
            print("按下");
        }
        //按键抬起
        if (Key.aKey.wasReleasedThisFrame)//判断这个按键是否抬起 wasReleasedThisFrame 抬起
        {
            print("抬起");
        }
        //按键长按
        if (Key.aKey.isPressed)//判断这个按键是否长按 isPressed 判断按下中
        {
            print("长按");
        }

        //通过事件监听 -- 这个C是调用的按键名字
        Key.onTextInput += (c) => {
            print("事件监听");
        };
--特殊任意键被按下 anyKey = 任意按键, :注无法得到具体按下了那个键,适合用于执行按下任意键进行操作逻辑的地方。
if(Keyboard.current.anyKey.wasPressedThisFrame)
{
     print("任意键被按下");
}
——————————————————————————————————
鼠标输入方法——获取鼠标控制

Mouse mouse = Mouse.current;
        //鼠标左键 mouse.leftButton
        //鼠标右键 mouse.rightButton
        //鼠标中键 mouse.middleButton
        //鼠标向前向后键 mouse.forwardButton mouse.backButton
        if (mouse.leftButton.wasPressedThisFrame) //鼠标左键按下
        {  
        }
        if (mouse.leftButton.wasReleasedThisFrame) //鼠标左键抬起
        {
        }
        if (mouse.leftButton.isPressed) //鼠标左键长按
        {
        }
//获取当前鼠标位置
mouse.position.ReadValue();
//获取当前鼠标移动向量
mouse.delta.ReadValue()
//获取鼠标中键滚轮方向向量
mouse.scroll.ReadValue()触屏输入方法 -- 获取手指组

命名空间引用
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;      


Touchscreen touch = Touchscreen.current;//获取当前运行环境的触控组件
        if (touch == null)//设备可能是空 -- 例如在手机端获取pc键盘
        {
            return;
        }
        Debug.Log("当前手指数量" + touch.touches.Count);
        if (touch.touches.Count <= 1) return;
        Debug.Log("得到第一根手指(触碰点)" + touch.touches[0]);
        foreach (var item in touch.touches)//进行遍历获取所有手指信息
        {
            Debug.Log(item);
        }
        TouchControl tc = touch.touches[0];//获取手指点击信息
        if (tc.press.wasPressedThisFrame)//判断指定手指按下
        {
            Debug.Log("按下");
        }
        if (tc.press.wasReleasedThisFrame)//抬起
        {
            Debug.Log("抬起");
        }
        if (tc.press.isPressed) //长按
        {
            Debug.Log("长按");

            if (tc.tap.isPressed)//点击手势
            {
                Debug.Log("点击手势");
            }
            Debug.Log(tc.tapCount);//判断点击次数
        }
        //手指位置信息
        Debug.Log(tc.position.ReadValue());
        //接触区域大小
        Debug.Log(tc.radius.ReadValue());
        //手指偏移量
        Debug.Log(tc.delta.ReadValue());
        //当前手指状态 -- 这个地方有不明确引用 ,需要明确给到命名空间
        UnityEngine.InputSystem.TouchPhase localPhase = tc.phase.ReadValue();
        switch (localPhase)//通过这个方法获取手指状态
        {
            case UnityEngine.InputSystem.TouchPhase.None://空,无点击
                break;
            case UnityEngine.InputSystem.TouchPhase.Began://开始点击
                break;
            case UnityEngine.InputSystem.TouchPhase.Moved://移动中
                break;
            case UnityEngine.InputSystem.TouchPhase.Ended://结束
                break;
            case UnityEngine.InputSystem.TouchPhase.Canceled://取消,
                break;
            case UnityEngine.InputSystem.TouchPhase.Stationary://静止
                break;
        }其他输入

Unity - Manual: Input System
一些冷门的输入方式可以直接查找官网,类似触碰笔,陀螺仪,光照传感器之类的。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-14 20:37 , Processed in 0.112166 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表