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