【问题标题】:Unity Crashes when Using this UI使用此 UI 时 Unity 崩溃
【发布时间】:2020-06-29 00:14:25
【问题描述】:

使用 UnityEngine.UI; 使用 UnityEngine;

公开课教程:MonoBehaviour { public Text tutorialText;

void Update()
{
    tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
    if (Input.GetKeyDown(KeyCode.Space))
    {
        while (true)
        {
            tutorialText.text = "Now Shoot";
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                while (true)
                {
                    tutorialText.text = "Nice";
                }
            }
        }
    }
}

}

【问题讨论】:

  • 删除两个while (true)
  • 我想把它变成一个教程。如果我删除它,它会显示“现在拍摄”一帧并切换回“按 [SPACE] 跳转...”

标签: unity3d


【解决方案1】:

将您的代码更改为该代码,因为现在您处于无限循环中,它会一次又一次地将您的教程文本设置为“Now Shoot”,直到 Unity 崩溃。

if (Input.GetKeyDown(KeyCode.Space))
{
   tutorialText.text = "Now Shoot";
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
   tutorialText.text = "Nice";
}

如果您希望文本在您第一次执行操作后消失,您应该添加 bool 变量。

bool jumped = false;
bool hasShot = false;

然后像这样在你的代码中实现这些布尔值:

if (Input.GetKeyDown(KeyCode.Space))
{
   if (jumped)
      return;

   tutorialText.text = "Now Shoot";
   jumped = true;
}
else if (Input.GetKeyDown(KeyCode.Mouse0))
{
   if (hasShot || !jumped)
      return;

   tutorialText.text = "Nice";
   hasShot = true;
}

您还需要将第一个文本定义移出 Update,因为 Update 是在每一帧中调用的。改为将其放入void Start()

void Start() {
   tutorialText.text = "Press [SPACE] to Jump... (obviously...)";
}

【讨论】:

  • 我不确定我是否做错了。但是我复制了你的脚本。现在我又遇到了另一个问题。它切换一帧到“现在拍摄”,然后回到“按 [SPACE] 跳转...(显然...)”
  • 编辑了我的帖子,您需要将 tutorialtext 的第一个定义从 update 移到 start 方法中。
  • 有点用。但我有最后一个“错误”。如果我一开始就开枪,它会跳过前两个定义并跳转到“Nice”
猜你喜欢
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多