Unity新输入系统中GetKey等效方法及手柄长按检测与代码改写
嘿,我来帮你搞定Unity新输入系统的这几个问题,一步步拆解:
Unity新输入系统:替代GetKey、手柄长按检测,以及跳跃代码改写
一、GetKey系列方法的等效替代
在新输入系统里,我们不再用Input.GetKey/GetKeyDown/GetKeyUp这类静态API,而是通过Input Action来处理输入,对应关系清晰明了:
Input.GetKeyDown(KeyCode.Space)→ 监听Input Action的Performed事件(单次触发,对应按键按下的瞬间),或者在Update里调用action.WasPressedThisFrame()Input.GetKey(KeyCode.Space)→ 在Update里调用action.IsPressed(),检测按键是否持续按住Input.GetKeyUp(KeyCode.Space)→ 监听Input Action的Cancelled事件,或者用action.WasReleasedThisFrame()
核心思路是:先创建对应的Input Action(比如一个Jump Action),绑定你需要的输入源(空格键、手柄A键等),然后通过读取Action的状态或者绑定事件来处理输入。
二、游戏手柄按钮长按的检测方式
有两种常用实现方式,按需选择:
方式1:手动计时(灵活可控)
在Action的Started事件里记录当前时间,然后在Update中判断从开始按住到现在的时间是否超过你的长按阈值(比如0.5秒):
private float _holdStartTime; private bool _isHoldingButton; [SerializeField] private float _longPressThreshold = 0.5f; // 初始化时绑定事件 private void Awake() { yourJumpAction.started += ctx => { _holdStartTime = Time.time; _isHoldingButton = true; }; yourJumpAction.canceled += ctx => { _isHoldingButton = false; }; } private void Update() { if (_isHoldingButton && Time.time - _holdStartTime >= _longPressThreshold) { // 执行长按逻辑 Debug.Log("手柄按钮已长按"); } }
方式2:使用Input System的Press Interaction(更简洁)
直接在Input Action的设置里添加Press Interaction:
- 打开你的Input Action Asset,找到对应的Jump Action
- 点击Action右侧的
+,选择Add Interaction→Press - 在Press Interaction的设置里,设置
Press Duration为你想要的长按时间(比如0.5秒) - 这样当按住按钮超过设定时间时,Action会触发
Performed事件;如果没到时间就松开,会触发Cancelled事件
这种方式不需要自己写计时逻辑,适合简单的长按需求。
三、旧跳跃代码改写为新输入系统版本
首先你需要先准备好:
- 创建一个Input Action Asset,添加一个
JumpAction,绑定空格键和手柄A键(根据你的手柄类型调整,比如Xbox的A键,PS的X键) - 在脚本中引用这个Input Action,比如通过
[SerializeField] private InputAction _jumpAction;
然后是改写后的代码:
[SerializeField] private InputAction _jumpAction; [SerializeField] private Rigidbody2D _myRigidbody2D; [SerializeField] private float _jumpSpeed; [SerializeField] private float _jumpTime; private bool _isTouchingGround; private bool _isJumping; private float _jumpTimeCounter; private void Awake() { // 绑定事件:按下按钮时触发初始跳跃 _jumpAction.performed += ctx => { if (_isTouchingGround) { _isJumping = true; _jumpTimeCounter = _jumpTime; _myRigidbody2D.velocity = new Vector2(_myRigidbody2D.velocity.x, _jumpSpeed); } }; // 松开按钮时结束跳跃 _jumpAction.canceled += ctx => { _isJumping = false; }; } private void Update() { // 持续按住按钮时,维持跳跃(只要还有跳跃时间) if (_jumpAction.IsPressed() && _isJumping) { if (_jumpTimeCounter > 0) { _myRigidbody2D.velocity = new Vector2(_myRigidbody2D.velocity.x, _jumpSpeed); _jumpTimeCounter -= Time.deltaTime; } else { _isJumping = false; } } } // 记得在启用/禁用时启用/禁用Action private void OnEnable() { _jumpAction.Enable(); } private void OnDisable() { _jumpAction.Disable(); } // 这里假设你有检测地面的逻辑,比如OnCollisionEnter2D等 public void SetIsTouchingGround(bool value) { _isTouchingGround = value; }
代码说明:
- 用
_jumpAction.performed替代原来的Input.GetKeyDown,处理初始跳跃的触发 - 用
_jumpAction.IsPressed()替代Input.GetKey,检测是否持续按住按钮,维持跳跃 - 用
_jumpAction.canceled替代Input.GetKeyUp,处理松开按钮结束跳跃的逻辑 - 必须在
OnEnable/OnDisable里启用/禁用Input Action,避免内存泄漏或无效输入
内容的提问来源于stack exchange,提问作者jaskolajban
相关产品推荐
相关产品推荐

