Unity新(Input System)老(Input Manager)输入系统代码对比

以下介绍都是基于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();

获取鼠标右键抬起

//Old
        if (Input.GetMouseButtonUp(1)) DoSomething();
//New
        if (Mouse.current.rightButton.wasReleasedThisFrame) DoSomething();

获取鼠标中间按着

扫描二维码关注公众号,回复: 16867246 查看本文章
//Old
        if (Input.GetMouseButton(2)) DoSomething();
//New
        if (Mouse.current.middleButton.ReadValue() > 0.5f) DoSomething();

猜你喜欢

转载自blog.csdn.net/ttod/article/details/128573068
今日推荐