Basically I’m trying to build a visual novel-like dialog system. So far I’ve done it using a two-dimensional array to provide the name of the character speaking and his/her line of dialog. Now I’m trying to animate it, which I was able to accomplish through a Coroutine with a foreach loop.
The problem is that if you click while a sentence is still being typed out, the whole thing breaks. If someone clicks while a sentence is still being typed out, I need it to appear in its entirety without trying to move on to the next sentence.
I’ve tried using a bool to denote whether or not the click occured during the foreach loop, but it didn’t work out. Any help/ideas would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Dialog : MonoBehaviour {
public float letterPaused = 0.03f;
public Text charName;
public Text charLine;
int i = 0;
string[,] Intro = new string [,] {{“Lina”, “Hello”},
{“Fred”, “How’s it going?”},
{“Bruce”, “What’s everyone up to?”}};
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
charName.text = Intro[i, 0];
charLine.text = " ";
StartCoroutine(TypeText());
// charLine.text = Intro[i, 1];
i++;
}
}
IEnumerator TypeText()
{
foreach(char letter in Intro[i,1].ToCharArray())
{
charLine.text += letter;
yield return 0;
yield return new WaitForSeconds(letterPaused);
}
}
}