using System.Collections; using System.Collections.Generic; using System.Reflection; using UnityEngine; using UnityEngine.UI; public class Conversation : MonoBehaviour { private string info; private Text txt; public float delay = 0.1f; [Header("题号")] public int QuesitionIndex = 1; public void Init() { txt = this.transform.Find("Text").GetComponent(); info = txt.text; txt.text = ""; int conversationindex = transform.GetSiblingIndex()+1; EventCenter.AddListener(EventType.ShowQuestion, (index) => { //conversationindex = QuesitionIndex; try { Show(index); Debug.Log(index); } catch { Show(); Debug.LogWarning("index丢失"); } }); EventCenter.AddListener(EventType.HideQuestion, Hide); if (gameObject.activeSelf) { Debug.Log("隐藏"); gameObject.SetActive(false); } } public void Show(int index=1) { if (index != this.QuesitionIndex) return; this.gameObject.SetActive(true); StartCoroutine(ShowText(index)); } public void Show() { this.gameObject.SetActive(true); StartCoroutine(ShowText(QuesitionIndex)); } public void Hide() { this.gameObject.SetActive(false); } IEnumerator ShowText(int index) { for (int i = 0; i < info.Length; i++)//遍历插入字符串的长度 { txt.text = info.Substring(0, i); yield return new WaitForSeconds(delay);//每次延迟的时间 数值越小 延迟越少 } if (QuesitionIndex == 3|| QuesitionIndex == 6) { //if (QuesitionIndex == 7) //{ // GameManager.Instance.obj1.SetActive(true); //} if (QuesitionIndex == 3 || QuesitionIndex == 6) { Invoke("yc", 2); } } else { Invoke("Delay", 2); } } /// /// 延迟2秒关闭上一段对话 以及打开我们输入框的那个 /// public void yc() { GameManager.Instance.conversationInputs[QuesitionIndex].gameObject.SetActive(true); GameManager.Instance.conversations[QuesitionIndex].gameObject.SetActive(false); } public void Delay() { GameManager.Instance.UpdateQuestionIndex(); } }