以下介绍都是基于Unity2022版本
一、键盘操作
当w键按下时
//Old
if (Input.GetKeyDown(KeyCode.W)) DoSomething();
//New
if(Keyboard.current.wKey.wasPressedThisFrame) DoSomething();
当w键抬起时
//Old
if (Input.GetKeyUp(KeyCode.W)) DoSomething();
//New
if (Keyboard.current.wKey.wasReleasedThisFrame) DoSomething();
当w键按着时
//Old
if (Input.GetKey(KeyCode.W)) DoSomething();
//New
if (Keyboard.current.wKey.ReadValue() > 0.5f) DoSomething();
二、鼠标操作
获取鼠标位置
//Old
Vector3 mousePos = Input.mousePosition;
//New
mousePos = Mouse.current.position.ReadValue();
获取鼠标滚轮
//Old
float scroll = Input.GetAxis("Scroll Wheel");
//New
scroll = Mouse.current.scroll.ReadValue().y;
获取鼠标左键按下
//Old
if (Input.GetMouseButtonDown(0)) DoSomething();
//New
if (Mouse.current.leftButton.wasPressedThisFrame) DoSomething();
获取鼠标右键抬起文章来源:https://www.toymoban.com/news/detail-597312.html
//Old
if (Input.GetMouseButtonUp(1)) DoSomething();
//New
if (Mouse.current.rightButton.wasReleasedThisFrame) DoSomething();
获取鼠标中间按着文章来源地址https://www.toymoban.com/news/detail-597312.html
//Old
if (Input.GetMouseButton(2)) DoSomething();
//New
if (Mouse.current.middleButton.ReadValue() > 0.5f) DoSomething();
到了这里,关于Unity新(Input System)老(Input Manager)输入系统代码对比的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!