176 lines
4.7 KiB
C#
176 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogCanvasManager : Singleton<DialogCanvasManager>
|
|
{
|
|
private const string BubbleView = "Dialog Bubble View";
|
|
private const string OldBoxView = "Dialog Box";
|
|
|
|
private const string QuickTalk = "Quick Talk";
|
|
|
|
#region 引用
|
|
|
|
private IDialogView _bubbleView;
|
|
private IDialogView _oldBoxView;
|
|
|
|
private IDialogView _curView;
|
|
|
|
private GameObject _quickTalkButton;
|
|
|
|
#endregion
|
|
|
|
private List<IUnRegister> _rmvList = new();
|
|
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
InitDialogViews();
|
|
InitQuickTalkButton();
|
|
InitEvent();
|
|
// 默认为旧Box
|
|
_curView = _bubbleView;
|
|
}
|
|
|
|
private void InitEvent()
|
|
{
|
|
_rmvList.Add(EnumEventSystem.Global.Register<DialogEventEnum, DialogLine>(DialogEventEnum.LineStart, AddDialogueLine));
|
|
_rmvList.Add(EnumEventSystem.Global.Register<DialogEventEnum, DialogLine>(DialogEventEnum.OptionSelected, AddDialogueLine));
|
|
_rmvList.Add(EnumEventSystem.Global.Register(EventEnum.NextYarn, CleanDialogHistory));
|
|
}
|
|
|
|
public override void OnSingletonDestroy()
|
|
{
|
|
// 销毁前处理事件
|
|
_rmvList.ForEach(item => item.UnRegister());
|
|
}
|
|
|
|
#region 对话结束后隐藏
|
|
|
|
private void OnEnable()
|
|
{
|
|
DialogController.OnDialogueComplete += HideDialog;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
DialogController.OnDialogueComplete -= HideDialog;
|
|
}
|
|
|
|
private void HideDialog()
|
|
{
|
|
_curView.HideDialog();
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Q) && Input.GetKey(KeyCode.P))
|
|
{
|
|
_quickTalkButton.SetActive(!_quickTalkButton.activeSelf);
|
|
}
|
|
}
|
|
|
|
#region 对话框相关处理
|
|
|
|
private void InitDialogViews()
|
|
{
|
|
_bubbleView = transform.Find(BubbleView).GetComponent<IDialogView>();
|
|
_oldBoxView = transform.Find(OldBoxView).GetComponent<IDialogView>();
|
|
|
|
_bubbleView.HideDialog();
|
|
_oldBoxView.HideDialog();
|
|
}
|
|
|
|
private void InitQuickTalkButton()
|
|
{
|
|
_quickTalkButton = transform.Find(QuickTalk).gameObject;
|
|
_quickTalkButton.GetComponent<Button>().onClick.AddListener(SwitchQuickTalkMode);
|
|
}
|
|
|
|
private void SwitchQuickTalkMode()
|
|
{
|
|
DialogController.Instance.quickRunMode = !DialogController.Instance.quickRunMode;
|
|
_quickTalkButton.GetComponent<Image>().color =
|
|
DialogController.Instance.quickRunMode ? Color.gray : Color.white;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换对话UI
|
|
/// </summary>
|
|
/// <param name="viewType">UI类型</param>
|
|
/// <returns></returns>
|
|
public IDialogView SwitchDialogView(DialogViewType viewType)
|
|
{
|
|
var newDlg = viewType switch
|
|
{
|
|
DialogViewType.Bubble => _bubbleView,
|
|
DialogViewType.OldBox => _oldBoxView,
|
|
_ => _oldBoxView
|
|
};
|
|
|
|
if (_curView != newDlg)
|
|
{
|
|
_curView.HideDialog();
|
|
_curView = newDlg;
|
|
}
|
|
|
|
return _curView;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前正在使用的Dialog View
|
|
/// </summary>
|
|
/// <returns>当前正在使用的Dialog View</returns>
|
|
/// <exception cref="Exception">未找到Canvas</exception>
|
|
public static IDialogView GetCurView()
|
|
{
|
|
return Instance._curView;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 对话回顾
|
|
|
|
[Header("对话回顾组件")] [SerializeField] private TMP_Text logText;
|
|
|
|
/// <summary>
|
|
/// 记录对话
|
|
/// </summary>
|
|
/// <param name="line">对话</param>
|
|
private void AddDialogueLine(DialogLine line)
|
|
{
|
|
logText.text += line.GetDialogText();
|
|
}
|
|
|
|
private void CleanDialogHistory()
|
|
{
|
|
logText.text = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换对话回顾展示状态
|
|
/// </summary>
|
|
public void ControlDialogHistory()
|
|
{
|
|
var logObj = transform.Find("Dialogue Log").gameObject;
|
|
logObj.SetActive(!logObj.activeSelf);
|
|
EnumEventSystem.Global.Send(EventEnum.Menu, logObj.activeSelf);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public enum DialogViewType
|
|
{
|
|
Bubble,
|
|
OldBox
|
|
}
|
|
} |