老规矩,先介绍一下 Unity 的科普小知识:
🎬 博客主页:
🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN🙉
🎄 学习专栏推荐:Unity系统学习专栏
🌲 游戏制作专栏推荐:游戏制作
🌲Unity实战100例专栏推荐:Unity 实战100例 教程
🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
📆 未来很长,值得我们全力奔赴更美好的生活✨
------------------❤️分割线❤️-------------------------
在Unity的新输入系统InputSystem中,获取键盘鼠标的API发生了变化,不再是之前用Input.就可以拿到了。
本文将在InputSystem中获取键盘鼠标的新API做一个简单总结整理。
键盘相关
键盘事件监听
void Update()
{if (Keyboard.current.spaceKey.wasPressedThisFrame){Debug.Log("空格键按下");}if(Keyboard.current.aKey.wasReleasedThisFrame){Debug.Log("A键抬起");}if(Keyboard.current.spaceKey.isPressed){Debug.Log("空格按下");}if(Keyboard.current.anyKey.wasPressedThisFrame){Debug.Log("任意键按下");}
}
键盘事件绑定
void Start(){TextInput += (c) =>{Debug.Log("通过Lambda表达式" + c);};TextInput += KeyboardInput;}private void KeyboardInput(char c){Debug.Log("监听" + c);}
鼠标相关:
鼠标坐标
void Update
{if(Mouse.current.rightButton.wasPressedThisFrame){Debug.Log("鼠标右键按下");}if(Mouse.current.middleButton.wasPressedThisFrame){Debug.Log("鼠标中建按下");}if(Mouse.current.forwardButton.wasPressedThisFrame){Debug.Log("鼠标前键按下");}if(Mouse.current.backButton.wasPressedThisFrame){Debug.Log("鼠标后键按下");}//获取鼠标屏幕坐标(左下角为(0,0)Debug.Log(Mouse.current.position.ReadValue());//两帧之间的偏移Debug.Log(Mouse.current.delta.ReadValue());//获取鼠标滚轮坐标Debug.Log(Mouse.current.scroll.ReadValue());
}
鼠标事件绑定
void InputTest(){GameInput inputAction = new GameInput();//GameInput为场景中的InputSystem控制器inputAction.Enable();inputAction.Gameplay.MouseDown.performed += ctx =>{Debug.Log("按下:" + UnityEngine.InputSystem.Mouse.current.position.ReadValue());};inputAction.Gameplay.MouseDrag.performed += ctx =>{Debug.Log("拖拽:" + UnityEngine.InputSystem.Mouse.current.position.ReadValue());};inputAction.Gameplay.MouseUp.performed += ctx =>{Debug.Log("抬起:" + UnityEngine.InputSystem.Mouse.current.position.ReadValue());};}
触摸屏相关
void Update
{Touchscreen ts = Touchscreen.current;if (ts == null){return;}else{TouchControl tc = ts.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(tc.tapCount);//点击位置Debug.Log(tc.position.ReadValue());//第一次接触位置Debug.Log(tc.startPosition.ReadValue());//得到的范围Debug.Log(tc.radius.ReadValue());//偏移位置Debug.Log(tc.delta.ReadValue());//返回TouchPhase: None,Began,Moved,Ended,Canceled,StationaryDebug.Log(tc.phase.ReadValue());//判断状态UnityEngine.InputSystem.TouchPhase tp = tc.phase.ReadValue();switch (tp){//无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;}
}
手柄相关
Gamepad handle = Gamepad.current;if(handle==null){return;}Vector2 leftDir= handle.leftStick.ReadValue();//左手柄坐标Vector2 rightDir= handle.rightStick.ReadValue();//右手柄坐标//左摇杆按下抬起if(Gamepad.current.leftStickButton.wasPressedThisFrame){}if (Gamepad.current.leftStickButton.wasReleasedThisFrame){}if (Gamepad.current.leftStickButton.isPressed){}//右摇杆按下抬起if (Gamepad.current.rightStickButton.wasPressedThisFrame){}if (Gamepad.current.rightStickButton.wasReleasedThisFrame){}if (Gamepad.current.rightStickButton.isPressed){}if(Gamepad.current.dpad.left.wasPressedThisFrame){}if (Gamepad.current.dpad.left.wasReleasedThisFrame){}if (Gamepad.current.dpad.left.isPressed){}//右侧三角方块/XYAB按键//Gamepad.current.buttonEast;//Gamepad.current.buttonWest;//Gamepad.current.buttonSouth;//Gamepad.current.buttonEast;if (Gamepad.current.buttonNorth.wasPressedThisFrame){}if (Gamepad.current.buttonNorth.wasReleasedThisFrame){}if (Gamepad.current.buttonNorth.isPressed){}//手柄中央键if(Gamepad.current.startButton.wasPressedThisFrame){}if(Gamepad.current.selectButton.wasPressedThisFrame){}//肩键if(Gamepad.current.leftShoulder.wasPressedThisFrame){}if (Gamepad.current.rightShoulder.wasPressedThisFrame){}if(Gamepad.current.leftTrigger.wasPressedThisFrame){}if(Gamepad.current.rightTrigger.wasPressedThisFrame){}
本文发布于:2024-02-03 23:20:30,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170697399051522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |