Display fixed number of words in textbox at a time Unity C#

Viewed 68

Hi I have been trying to figure this out with no luck. I am receiving text from an API and sometimes the text is received with over 10 words and sometimes only one word. I would like to write a script that will only allow up to 3 words to be displayed at a time in a text box on screen.

Then after 2 seconds it will move to the next three words. If there is less than three words it will only display the remaining words. once it is finished I would like to call the API again to get more text. Can anyone help with this please?

I have been trying to create a lot of if statements but I am not sure how I would get the logic right? I think I might be overcomplicating this. Please help if there is an easier way. I have included some code that I have started with. Thank you!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using JacobGames.SuperInvoke;

public class TesxtMeshAi : MonoBehaviour
    {
    int count = 0;
        public Text Ai;
        public string AiMesh;
    string CombText;
    private string[] TextBreak;
        public int timer;
    public GameObject Call; // the api to call
   
    public TextMeshProUGUI textDisplay;
    
        void Start()
        {
            AiMesh = Ai.text;
        textDisplay.text = AiMesh;
       TextChange();
   
    }
    public void TextChange()
    {
        AiMesh = Ai.text;
        TextBreak = AiMesh.Split(char.Parse("_"));
        //  textDisplay.text = AiMesh;
        count = 0;
       SuperInvoke.RunRepeat(3, 1, TextBreak.Length, RunText); // this is a third party asset asset that repeats a method for the number of items in the array. 
   
    void RunText()
    {


        if (count + 2 > TextBreak.Length)
        {
            CombText = TextBreak[count] + " " + TextBreak[count + 1];
             count = 0;
            Call.GetComponent<TestChatEasy>().Call();
         }
        if (count + 1 > TextBreak.Length)
        {
            Call.GetComponent<TestChatEasy>().Call();
        }

        if (count > TextBreak.Length) //was old one
        {
            count = 0;
            Call.GetComponent<TestChatEasy>().Call();
         
        }
        if (count >= 0)
        {
            if (TextBreak.Length < 2)
            {
                CombText = TextBreak[count] + " " + TextBreak[count + 1];
                textDisplay.text = CombText;
                count = 0;
                Call.GetComponent<TestChatEasy>().Call();
               //   TextChange();
            }
            if (TextBreak.Length < 1)
            {
                CombText = TextBreak[count];
                textDisplay.text = CombText;
                count = 0;
                Call.GetComponent<TestChatEasy>().Call();
              //   TextChange();
            }
            if (TextBreak.Length > 3)
            {
                count = count + 1;
                CombText = TextBreak[count] + " " + TextBreak[count + 1] + " " + TextBreak[count + 2];
                textDisplay.text = CombText;
            }

        }
    }
1 Answers

Here's how I would do

I am receiving text from an API
only allow up to 3 words to be displayed at a time
after 2 seconds it will move to the next three words
once it is finished I would like to call the API again to get more text

  • Get text from api
  • Display it 3 words at the time, for 2 seconds of each 3-word-group
  • Repeat it forever
IEnumerator Start() {
  while (true) {
    var textFromApi = GetSomeTextFromSomewhere();
    yield return StartCoroutine(DisplayText(textFromApi));
  }

}


IEnumerator DisplayText(string text) {
  var start = 0;
  var take = 3;

  var arrayOfWords = text.split(' ').ToList();

  for (int i = 0; i < arrayOfWords.Count; i += take) {
    var displayList = arrayOfWords.Skip(i).Take(take);
    var displayText = String.Join(' ', displayList);
    
    textDisplay.text = displayText;

    yield return new WaitForSeconds(2);
  }
}
Related