【发布时间】:2019-04-18 15:06:59
【问题描述】:
我已经在这个项目上工作了几天,我遇到了一个似乎无法解决的错误,因为不仅没有错误消息出现,而且它还“跳过”了我的调试消息并使编辑器本身崩溃。
以下脚本是一个对话框显示器,这显然是导致问题的原因(请原谅混乱的代码,我在尝试解决问题时弄乱了它):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogDisplayer : MonoBehaviour
{
[SerializeField] Dialog[] dialogFiles;
TextMeshPro outputTxt;
bool next, finished;
char comma = (char)44;
char period = (char)46;
// Use this for initialization
void Start()
{
outputTxt = GetComponent<TextMeshPro>();
StartCoroutine(type());
}
IEnumerator type()
{
int dialogIndex = 0;
do
{
foreach (char c in dialogFiles[dialogIndex].dialogText)
{
if (Input.GetKeyDown(KeyCode.Z))
{
outputTxt.text = dialogFiles[dialogIndex].dialogText;
Debug.Log("z pressed in the foreach");
break;
}
outputTxt.text += c;
if (c == ' ')
continue;
if (dialogFiles[dialogIndex].delayforPunctuations)
{
if (c == comma)
yield return new WaitForSeconds(dialogFiles[dialogIndex].delayBetweenLetters + 0.1f);
else if (c == period)
yield return new WaitForSeconds(dialogFiles[dialogIndex].delayBetweenLetters + 0.2f);
else
yield return new WaitForSeconds(dialogFiles[dialogIndex].delayBetweenLetters);
}
else
yield return new WaitForSeconds(dialogFiles[dialogIndex].delayBetweenLetters);
}
Debug.Log("Either finished or broken out of the loop");
while (!finished)
{
Debug.LogWarning("Entering while loop");
if (Input.GetKeyDown(KeyCode.Z))
{
Debug.Log("entered if");
finished = true;
dialogIndex++;
}
Debug.Log("got out");
}
} while (dialogIndex != dialogFiles.Length - 1);
}
}
【问题讨论】:
-
我不是 Unity 专家,但我认为您不想在启动协程时调用
type()(顺便说一句,方法的好名字)。我认为您将方法信息传递给它:StartCoroutine(type);