使用此 UI 时 Unity 崩溃



using UnityEngine.UI; 使用 UnityEngine;

公开课教程:单行为 { 公共文本教程文本;

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";
}
}
}
}
}

}

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

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

如果您希望文本在第一次完成操作后消失,则应添加布尔变量。

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;
}

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

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

最新更新