简体   繁体   English

按键后如何执行操作?

[英]How to perform an action after key pressed?

I tried to hide a text in Unity after caps was pressed, but it doesn't work, it stops before "while".按下大写字母后,我试图在 Unity 中隐藏文本,但它不起作用,它在“while”之前停止。 I'm quite a not up to par programmer, so anyone more experienced?我是一个不合格的程序员,所以有谁更有经验吗?

private float TurnOffInfoText()
    {
       
      
        bool IsCapsPressed =   Input.GetKeyDown(KeyCode.CapsLock);
       
        while (IsCapsPressed == true)
        {
            
            EndOfGameText.enabled = false;
        }
      
        return 0;
        
}

Why does this even return a value?为什么这甚至会返回一个值?

Also your while loop would completely freeze the entire App and even the Unity Editor application!此外,您的while循环将完全冻结整个应用程序甚至 Unity 编辑器应用程序! Within the loop the IsCapsPressed value is never ever changed!在循环内, IsCapsPressed值永远不会改变!

I don't see where your method is called from but if you never experienced a freeze so far then "luckily" the key never went down in the same frame so far.我看不到你的方法是从哪里调用的,但如果你到目前为止从未经历过冻结,那么“幸运的是”到目前为止,密钥从未在同一帧中下降。

Usually you would rather poll the input every frame.通常,您宁愿每帧轮询输入。 By a simple look into the API for Input.GetKeyDown :通过简单查看 API 的Input.GetKeyDown

private void Update ()
{
    if(Input.GetKeyDown(KeyCode.CapsLock))
    {  
        EndOfGameText.enabled = false;
    }      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM