using Febucci.UI; using System; using TMPro; using UnityEngine; using UnityEngine.UI; using Yarn.Unity; using System.Threading.Tasks; namespace AibisDream { public class DialogUiViewer : Singleton { #region 组件名称 private const string DIALOG_BOX = "Dialog Box"; private const string ACTOR_NAME = "Actor Name"; private const string DIALOG_TEXT = "Dialog Text"; private const string NEXT_BUTTON = "Next Button"; private const string CHOICE_BUTTON = "Choice Button"; private const string QUICK_TALK = "Quick Talk"; #endregion #region 对话框UI private GameObject dialogBox; private TypewriterByCharacter dialogText; private TextMeshProUGUI actorName; private Button nextButton; private GameObject choiceBox; private GameObject choiceButtonPrefab; private GameObject quickTalkButton; #endregion protected override void Awake() { base.Awake(); // 获取预制体 choiceButtonPrefab = Resources.Load("Prefabs/UI/Choice Button"); // 获取子物体 dialogBox = transform.Find(DIALOG_BOX).gameObject; GetDialogComponentInBox(dialogBox); } private void OnEnable() { DialogController.OnDialogueComplete += HideDialog; } private void OnDisable() { DialogController.OnDialogueComplete -= HideDialog; } /// /// 显示文字 /// /// 对话内容 /// 名称 /// 下一步 public void ShowLine(string dialogLine, string actor, Action nextStep) { dialogBox.SetActive(true); dialogText.ShowText(""); dialogText.gameObject.SetActive(true); choiceBox.SetActive(false); actorName.text = actor; dialogText.ShowText(dialogLine); // 显示完成后处理 dialogText.onTextShowed.RemoveAllListeners(); dialogText.onTextShowed.AddListener(() => nextButton.gameObject.SetActive(true)); nextButton.onClick.RemoveAllListeners(); } /// /// 显示文字,显示完成自动跳过 /// /// /// /// public void ShowAutoLine(string dialogLine, string actor, Action nextStep) { dialogBox.SetActive(true); dialogText.ShowText(""); dialogText.gameObject.SetActive(true); choiceBox.SetActive(false); actorName.text = actor; nextButton.onClick.RemoveAllListeners(); dialogText.ShowText(dialogLine); // 对话显示完成后自动跳过 dialogText.onTextShowed.RemoveAllListeners(); dialogText.onTextShowed.AddListener(() => CommonUtil.Delay(200, nextStep)); } /// /// 显示选项 /// /// 选项 /// public void ShowOptions(DialogueOption[] dialogueOptions, Action onOptionSelected) { dialogBox.SetActive(true); choiceBox.SetActive(true); dialogText.gameObject.SetActive(false); dialogText.ShowText(""); actorName.text = ""; // 销毁原来的按钮 foreach (Transform child in choiceBox.transform) { Destroy(child.gameObject); } // 生成新按钮 foreach (DialogueOption option in dialogueOptions) { if (option.IsAvailable) { var choiceInstance = Instantiate(choiceButtonPrefab, Vector3.zero, Quaternion.identity); choiceInstance.transform.SetParent(choiceBox.transform); choiceInstance.GetComponentInChildren().text = option.Line.TextWithoutCharacterName.Text; choiceInstance.GetComponent