简体   繁体   中英

Game slows down when printing text

I have implemented the dialog system from this Brackeys video in my project. Everything works perfectly, but when I have done the build for android I have seen that printing long texts slows the game down.

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. 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.

 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.

And you can call it at Start instead of FixedUpdate.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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