using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine;using UnityEngine.UI;public class Dialoguemanager : MonoBehaviour{public static Dialoguemanager instance;public GameObject dialogueBox;//显示或隐藏对话窗口public Text dialogueText, nameText;//对话窗口的输出文字和对话者的名字public Image characterImage;public AnimationCurve ShowCurve;public AnimationCurve HideCurve;public float animationSpeed;[TextArea(1, 3)]//保证输入框文字不止一行public string[] dialogueLines;//多条对话的数组[SerializeField] private int currentLine;//追踪对话窗口在输出哪一行的文字private bool isScrolling;[SerializeField] private float textSpeed; private RectTransform dialogueBoxRectTransform;// 获取对话框的 RectTransform 组件IEnumerator ShowPanel(GameObject gameObjec){float timer = 0;while(timer < 1){dialogueBoxRectTransform.localScale = Vector3.one * ShowCurve.Evaluate(timer);timer += Time.deltaTime * animationSpeed;yield return null;} }IEnumerator HidePanel(GameObject gameObjec){float timer = 0;while (timer < 1){dialogueBoxRectTransform.localScale = Vector3.one * HideCurve.Evaluate(timer);timer += Time.deltaTime * animationSpeed;yield return null;} }private void Awake()//单例{if(instance == null){instance = this;}else{if(instance != this){Destroy(gameObject);}}DontDestroyOnLoad(gameObject);dialogueBoxRectTransform = dialogueBox.GetComponent();} // Start is called before the first frame updatevoid Start(){dialogueText.text = dialogueLines[currentLine];}// Update is called once per framevoid Update(){if (dialogueBox.activeInHierarchy){if (Input.GetMouseButtonUp(0))//按下并且松开鼠标左键后{if (isScrolling == false) { currentLine++;//跳转到下一行if (currentLine < dialogueLines.Length){CheckName();CheckImage();//dialogueText.text = dialogueLines[currentLine];StartCoroutine(ScrollingText());FindObjectOfType().canMove = false;}else{dialogueBox.SetActive(false);FindObjectOfType().canMove = true;}}//如果对话进行完毕则关闭窗口}} }//插入人物名称public void CheckName() {if (dialogueLines[currentLine].StartsWith("n-")){nameText.text = dialogueLines[currentLine].Replace("n-", "");currentLine++;FindObjectOfType().canMove = false;}}//插入人物立绘public void CheckImage(){if (dialogueLines[currentLine].StartsWith("ch")){characterImage.sprite = Resources.Load(dialogueLines[currentLine]);currentLine++;FindObjectOfType().canMove = false;}}private IEnumerator ScrollingText(){isScrolling = true;dialogueText.text = "";foreach(char letter in dialogueLines[currentLine].ToCharArray()){dialogueText.text += letter;yield return new WaitForSeconds(textSpeed);}isScrolling = false;}public void ShowDialogue(string[] _newLines)//用来调用的方法{dialogueLines = _newLines;currentLine = 0;CheckName();CheckImage();//dialogueText.text = dialogueLines[currentLine];StartCoroutine(ScrollingText());dialogueBox.SetActive(true);dialogueBoxRectTransform.localScale = Vector3.zero;StartCoroutine(ShowPanel(dialogueBox));FindObjectOfType().canMove = false;}} }