前言
新的输入系统在跨平台方面便捷了不少,最近公司在弄工具,其中一个功能就是希望实现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();