You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity C#实现可点击长按按键功能问题求助(类删除键逻辑)

问题分析与修复方案

核心问题

  1. 共用延迟变量:用同一个allowNextKeyboardInput处理Escape(减血)和Return(加血)两个按键,导致两个按键的状态互相干扰,比如按Escape设置的延迟会被Return操作覆盖,直接打乱长按延迟逻辑。
  2. 逻辑分支冲突:按键释放时的重置逻辑会在另一个按键操作时误触发,导致延迟变量被强制置0,长按还没到延迟时间就开始重复执行。

修复后的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
[RequireComponent(typeof(AudioSource))]

public class GMScript : MonoBehaviour
{
    public GameObject bomb;
    public GameObject explosionPrefab;
    public int bombHealth = 10;
    public TMP_Text bombHealthText;
    public TMP_Text bombHealthNumber;

    public float initialDelay = 1.0f; // 可自行调整长按触发前的延迟时长
    public float repeatingRate = 0.5f;
    // 给每个按键单独设置延迟变量,避免互相干扰
    private float escapeNextInputTime = 0.0f;
    private float returnNextInputTime = 0.0f;

    public AudioSource explosion;

    void Start()
    {
        explosion = GetComponent<AudioSource>();    
    }

    void Update()
    {
        // Escape键(减血)逻辑
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            RemoveHealth();
            // 设置首次长按触发的延迟时间
            escapeNextInputTime = Time.time + initialDelay;
        }
        // 长按且到达延迟时间后,重复执行
        else if(Time.time > escapeNextInputTime && Input.GetKey(KeyCode.Escape))
        {
            RemoveHealth();   
            // 更新下一次执行的时间
            escapeNextInputTime += repeatingRate;
        }
        // 释放按键时重置延迟变量
        else if (!Input.GetKey(KeyCode.Escape)) 
        { 
            escapeNextInputTime = 0f;
        }

        // Return键(加血)逻辑
        if(Input.GetKeyDown(KeyCode.Return))
        {
            AddHealth();
            returnNextInputTime = Time.time + initialDelay;
        }
        else if(Time.time > returnNextInputTime && Input.GetKey(KeyCode.Return))
        {
            AddHealth();
            returnNextInputTime += repeatingRate;
        }
        else if (!Input.GetKey(KeyCode.Return)) 
        { 
            returnNextInputTime = 0f;
        }
    }

    void RemoveHealth()
    {
        if (bombHealth <= 0) return;
        
        bombHealth -= 1;
        UpdateText(bombHealth);

        if (bomb.transform.position.x == 15) 
        {
            Debug.Log("炸弹已经被销毁");
            return;
        }

        if (bombHealth == 0)
        {
            explosion.Play();
            DestroyObject(bomb);
        }   
    }
    
    void AddHealth()
    {
        bombHealth += 1;
        UpdateText(bombHealth);
    }

    void UpdateText(int txt)
    {
        bombHealthNumber.text = txt.ToString();
    }

    void DestroyObject(GameObject obj)
    {
        Instantiate(explosionPrefab, bomb.transform.position, Quaternion.identity);
        bomb.transform.position = new Vector3(15.0f ,0.0f ,0.0f);
        Debug.Log(obj.name + " 已被销毁");
    }
}

关键修改点

  • 给Escape和Return键分别创建独立的延迟变量,彻底解决按键间的状态干扰问题。
  • 调整每个按键的逻辑分支顺序,确保短按仅触发一次、长按等待设定延迟后再重复执行的流程正确。
  • 优化RemoveHealth方法的判断逻辑,减少冗余代码与无效输出。
  • 将默认的initialDelay从10秒调整为1秒,更贴合常规长按操作的用户体验,可根据需求自行修改。

内容的提问来源于stack exchange,提问作者ThatGreenLeaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:14:53