using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class DialogueScript : MonoBehaviour { public Text textComponent; public string[] lines; public float textSpeed; private int index; public List images; private GameObject image; private GameObject textImage; public List imageAndTexts; private int indexList = 0; private void OnEnable() { textComponent.text = string.Empty; StartDialogue(); } // Update is called once per frame void Update() { if(images.Contains(index)) { SetImageStep(); } if (Input.GetMouseButtonDown(0)) { if(images.Contains(index - 1)){ DisableImageStep(); } if (textComponent.text == lines[index]) { NextLine(); } else { StopAllCoroutines(); textComponent.text = lines[index]; } } } public void StartDialogue() { index = 0; StartCoroutine(TypeLine()); } IEnumerator TypeLine() { foreach (char c in lines[index].ToCharArray()) { textComponent.text += c; yield return new WaitForSeconds(textSpeed); } } public void NextLine() { if (index < lines.Length - 1) { index++; textComponent.text = string.Empty; StartCoroutine(TypeLine()); } else { gameObject.SetActive(false); } } private void SetImageStep(){ textImage = imageAndTexts[indexList]; indexList ++; image = imageAndTexts[indexList]; indexList ++; image.SetActive(true); textImage.SetActive(true); index ++; } private void DisableImageStep(){ image.SetActive(false); textImage.SetActive(false); } }