Untiy InputSystem(一) 新旧API对比

前言

新的输入系统在跨平台方面便捷了不少,最近公司在弄工具,其中一个功能就是希望实现PC和VR及其他平台的快速切换,输入模块正好用的上,先简单梳理下,后面有空再仔细研究。
一、前置操作
1、官方文档
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Installation.html
2、重点备注
1、PackageManager 正常下载InputSystem,系统会自动设置为新的输入系统,此时如果代码中存在旧的api,比如Input. 系统会报错。
1、可以同时开启新、旧两个InputSystem,PlayerSetting -> OhterSetting -> Active Input handing;
2、EventSystem对象中的输入系统,要修改成为Input System UI Input Module;
3、在2组件中,有个Actoin Asset 对象,是默认的输入配置,这个位置是重点,可以快速实现跨平台输入定义。
二、新旧InputSystem Api对照
//事件判断还是如旧
EventSystem.current.IsPointerOverGameObject()
1、键盘操作
       curKey = Keyboard.current;

       if (curKey.jKey.wasPressedThisFrame) // Input.GetKeyDown()
        {
            Debug.Log("按下J");
        }
        else if (curKey.jKey.wasReleasedThisFrame) // Input.GetKeyUp()
        {
            Debug.Log("松开J");
        }
        else if (curKey.jKey.isPressed) //Input.GetKey()
        {
            Debug.Log("一直按着J"); //一直反复执行
        }
2、鼠标操作
  • 鼠标点击事件
        if (Mouse.current.leftButton.isPressed) // Input.GetMouseButton(0)
        {
            Debug.Log("左键按下,反复执行");
            //获取鼠标位置
            Debug.Log($"鼠标位置:{Mouse.current.position.ReadValue()}");
        }
        if (Mouse.current.leftButton.wasPressedThisFrame) //Input.GetMouseButtonDown(0)
        {
            Debug.Log("左键按下");
        }
        if (Mouse.current.leftButton.wasReleasedThisFrame) //Input.GetMouseButtonUp(0)
        {
            Debug.Log("左键松开");
        }
  • 鼠标滚轮
//获取鼠标滚轮值,新输入系统为Vector2,滚动一下默认是(0,+120),正为往前,负为往后
Mouse.current.scroll.ReadValue();

---
//旧版api,返回为float值,默认为0.1,往前为+0.1,往后为-0.1
Input.GetAxis("Mouse ScrollWheel")
  • 鼠标位置
//获取鼠标位置,返回Vector2
 var pos = Mouse.current.position.ReadValue();
 //旧版
 Input.mousePosition 
  • 鼠标偏移
//鼠标与上一帧的偏移量
Mouse.current.delta.ReadValue();
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值