我的脚本无法按预期运行,也使统一编辑器崩溃

时间:2018-11-15 13:36:37

标签: c# unity3d

我已经在这个项目上工作了几天,我遇到了一个似乎无法解决的错误,因为不仅没有错误消息出现,而且还会“跳过”我的调试消息并使编辑器本身崩溃

以下脚本是一个对话框显示程序,它显然是导致问题的原因(原谅混乱的代码,在尝试解决问题时我将其弄乱了):

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

2 个答案:

答案 0 :(得分:0)

从我所见....

英语:当dialogIndex不等于dialogFiles.length减一时,脚本崩溃。

一次:

while (dialogIndex != dialogFiles.Length - 1);

是正确的,它进入一个无限循环。一旦找到dialogFiles,就没有方向可言。长度-1不再等于dialogIndex。

不确定在此处的最后一行代码要做什么,但是计算机也没有,一旦满足这些条件,计算机就会一直等待指令。……永远。

它必须是

while (dialogIndex != dialogFiles.Length - 1); {
  Do Something;
  provide Action To Eventually escape this loop (make dialogIndex = dialogFiles.Length - 1)
}

至于评论,您可以执行StartCoroutine(type());StartCoroutine("type");

答案 1 :(得分:0)

Coroutine中的任何地方都不会使用yield return。这将cause your coroutine to run all iterations in the same frame

来自Scripting documentation

// every 2 seconds perform the print()
private IEnumerator WaitAndPrint(float waitTime)
{
    while (true)
    {
        yield return new WaitForSeconds(waitTime);
        print("WaitAndPrint " + Time.time);
    }
}

或者您可以使用yield return null将协程传递到下一帧:

private IEnumerator YourDialogCoroutine()
{
    bool finished = false;

    while (!finished)
    {
        If(Input.GetKeyDown("Z"))
        {
            Debug.Log("Ending loop!");
            finished = true;
        }

        yield return null;
    }
}