简体   繁体   English

打印文本时游戏变慢

[英]Game slows down when printing text

I have implemented the dialog system from this Brackeys video in my project.我已经在我的项目中实现了这个 Brackeys 视频中的对话系统。 Everything works perfectly, but when I have done the build for android I have seen that printing long texts slows the game down.一切都很完美,但是当我完成 android 的构建时,我发现打印长文本会减慢游戏速度。

The code divides the sentence to be displayed in the UI into an array of characters and then prints each character one by one, with a small delay.代码将要在 UI 中显示的句子分成一个字符数组,然后一个一个地打印每个字符,延迟很小。 I have been doing several tests and first I thought that the problem was the coroutine from where the characters were printed.我一直在做几次测试,首先我认为问题出在打印字符的协同程序上。 But then I have removed the code from the coroutine and I have seen that the more characters it prints, the more the game slows down.但是后来我从协程中删除了代码,我发现它打印的字符越多,游戏速度就越慢。

void FixedUpdate()
 {
     if(typeSentence)
     {
         if(t <= 0)
         {
             TypeChar();

             t = charDelay;
         }

         t -= Time.fixedDeltaTime;
     }
 }

 private void TypeChar()
 {
     GUIs[dialogue.UIIndex].dialogueText.text += charSentence[sentenceIndex];

     sentenceIndex++;

     if (sentenceIndex >= charSentence.Length)
     {
         typeSentence = false;
         sentenceIndex = 0;
         GUIs[dialogue.UIIndex].continueButton.SetActive(true);
     }
 }

I don't know if there is a more efficient way to do it, or if someone can explain to me what is happening and why it slows down so much.我不知道是否有更有效的方法来做到这一点,或者是否有人可以向我解释发生了什么以及为什么它会减慢这么多。

Instead of triggering TypeChar() method in fixed update, you can convert TypeChar() to a Coroutine that can handle time more performing way.您可以将 TypeChar() 转换为可以处理时间的协程,而不是在固定更新中触发 TypeChar() 方法。

 private IEnumerator TypeChar()
 {
     while(sentenceIndex >= charSentence.Length) 
     {
        if(typeSentence)
        {
            GUIs[dialogue.UIIndex].dialogueText.text += charSentence[sentenceIndex];
            sentenceIndex++;
            yield return new WaitForSeconds(charDelay);
        }
     }
     typeSentence = false;
     sentenceIndex = 0;
     GUIs[dialogue.UIIndex].continueButton.SetActive(true); 
 }

And you can delete typeSentence variable completely if you do not change it out of scope.如果不将 typeSentence 变量从 scope 中更改出来,则可以完全删除它。

And you can call it at Start instead of FixedUpdate.您可以在 Start 而不是 FixedUpdate 时调用它。

private void Start()
{
    StartCoroutine(nameof(TypeChar));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM