有延迟的 IEnumerator 运行 在 Android 上的顺序错误
IEnumerator with delays is running in the wrong order on Android
我的项目有字幕和画外音。我想延迟与声音同步的字符串上的所有字符。然后我写了这段代码,它在编辑器中运行良好:
public void Sync(GameObject textGameObject, string textCurrent, AudioClip clip)
{
audioDuration = clip.length;
textArea = textGameObject.GetComponent<TextMeshProUGUI>();
textArea.text = ""; //delete current text
subs = textCurrent.ToCharArray();
textDuration = clip.length / subs.Length;
audioSource.PlayOneShot(clip);
StartCoroutine(DelayTexts());
}
private IEnumerator DelayTexts()
{
yield return new WaitForSeconds(textDuration);
if (index < subs.Length)
{
textArea.text += subs[index];
index++;
StartCoroutine(DelayTexts());
}
else
{
StartCoroutine(WaitReadTime());
}
}
private IEnumerator WaitReadTime()
{
yield return new WaitForSeconds(2f);
textArea.text = "";
audioSource.Stop();
index = 0;
}
此代码在 Android 上的运行顺序错误,但我不知道 why.I 认为 IEnumerator
导致了问题。我可以在没有 IEnumerator
的情况下执行此操作或修复工作顺序吗?
可能会出现时间问题。 Check out this link and also 明白我的意思了。当 运行 在设备上时,您需要添加一些调试行来跟踪索引和 subs 值。
作为旁注,我建议重构您的 DelayTexts 方法。没有必要以这种方式递归调用 IEnumerator。您可以改为编写逻辑来循环。
像这样。我不确定你的条件如何运作,所以这可能不是你想要的,但这是一种更好的格式。
while (index < subs.Length)
{
yield return new WaitForSeconds(textDuration);
textArea.text += subs[index];
index++;
}
StartCoroutine(WaitReadTime());
我的项目有字幕和画外音。我想延迟与声音同步的字符串上的所有字符。然后我写了这段代码,它在编辑器中运行良好:
public void Sync(GameObject textGameObject, string textCurrent, AudioClip clip)
{
audioDuration = clip.length;
textArea = textGameObject.GetComponent<TextMeshProUGUI>();
textArea.text = ""; //delete current text
subs = textCurrent.ToCharArray();
textDuration = clip.length / subs.Length;
audioSource.PlayOneShot(clip);
StartCoroutine(DelayTexts());
}
private IEnumerator DelayTexts()
{
yield return new WaitForSeconds(textDuration);
if (index < subs.Length)
{
textArea.text += subs[index];
index++;
StartCoroutine(DelayTexts());
}
else
{
StartCoroutine(WaitReadTime());
}
}
private IEnumerator WaitReadTime()
{
yield return new WaitForSeconds(2f);
textArea.text = "";
audioSource.Stop();
index = 0;
}
此代码在 Android 上的运行顺序错误,但我不知道 why.I 认为 IEnumerator
导致了问题。我可以在没有 IEnumerator
的情况下执行此操作或修复工作顺序吗?
可能会出现时间问题。 Check out this link and also
作为旁注,我建议重构您的 DelayTexts 方法。没有必要以这种方式递归调用 IEnumerator。您可以改为编写逻辑来循环。
像这样。我不确定你的条件如何运作,所以这可能不是你想要的,但这是一种更好的格式。
while (index < subs.Length)
{
yield return new WaitForSeconds(textDuration);
textArea.text += subs[index];
index++;
}
StartCoroutine(WaitReadTime());