147 lines
4.8 KiB
C#
147 lines
4.8 KiB
C#
using Febucci.UI;
|
|
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogUiViewer : Singleton<DialogUiViewer>
|
|
{
|
|
#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<GameObject>("Prefabs/UI/Choice Button");
|
|
// 获取子物体
|
|
dialogBox = transform.Find(DIALOG_BOX).gameObject;
|
|
GetDialogComponentInBox(dialogBox);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示文字
|
|
/// </summary>
|
|
/// <param name="dialogLine">对话内容</param>
|
|
/// <param name="actor">名称</param>
|
|
/// <param name="nextStep">下一步</param>
|
|
public void ShowLine(string dialogLine, string actor, Action nextStep)
|
|
{
|
|
dialogBox.SetActive(true);
|
|
dialogText.gameObject.SetActive(true);
|
|
choiceBox.SetActive(false);
|
|
|
|
actorName.text = actor;
|
|
dialogText.ShowText(dialogLine);
|
|
nextButton.onClick.RemoveAllListeners();
|
|
nextButton.onClick.AddListener(() =>
|
|
{
|
|
nextStep.Invoke();
|
|
nextButton.gameObject.SetActive(false);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示选项
|
|
/// </summary>
|
|
/// <param name="dialogueOptions">选项</param>
|
|
/// <param name="onOptionSelected"></param>
|
|
public void ShowOptions(DialogueOption[] dialogueOptions, Action<int> onOptionSelected)
|
|
{
|
|
dialogBox.SetActive(true);
|
|
choiceBox.SetActive(true);
|
|
dialogText.gameObject.SetActive(false);
|
|
|
|
// 销毁原来的按钮
|
|
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<TextMeshProUGUI>().text =
|
|
option.Line.TextWithoutCharacterName.Text;
|
|
choiceInstance.GetComponent<Button>().onClick
|
|
.AddListener(() => onOptionSelected.Invoke(option.DialogueOptionID));
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ShowNextButton()
|
|
{
|
|
nextButton.gameObject.SetActive(true);
|
|
}
|
|
|
|
public void HideDialog()
|
|
{
|
|
dialogBox?.SetActive(false);
|
|
}
|
|
|
|
public void SwitchQuickTalkMode()
|
|
{
|
|
DialogController.Instance.quickRunMode = !DialogController.Instance.quickRunMode;
|
|
quickTalkButton.GetComponent<Image>().color =
|
|
DialogController.Instance.quickRunMode ? Color.gray : Color.white;
|
|
}
|
|
|
|
public bool TrySkipLine()
|
|
{
|
|
if (dialogText.isShowingText)
|
|
{
|
|
dialogText.SkipTypewriter();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void AfterEndLine()
|
|
{
|
|
nextButton.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void GetDialogComponentInBox(GameObject box)
|
|
{
|
|
actorName = box.transform.Find(ACTOR_NAME).gameObject.GetComponent<TextMeshProUGUI>();
|
|
dialogText = box.transform.Find(DIALOG_TEXT).gameObject.GetComponent<TypewriterByCharacter>();
|
|
nextButton = box.transform.Find(NEXT_BUTTON).GetComponent<Button>();
|
|
choiceBox = box.transform.Find(CHOICE_BUTTON).gameObject;
|
|
quickTalkButton = box.transform.Find(QUICK_TALK).gameObject;
|
|
|
|
quickTalkButton.GetComponent<Button>().onClick.AddListener(SwitchQuickTalkMode);
|
|
|
|
dialogText.onTextShowed.AddListener(ShowNextButton);
|
|
}
|
|
}
|
|
} |