IndieGame/client/Assets/Scripts/Frame/Dialogue/DialogueFrame.cs

83 lines
2.6 KiB
C#

using NodeCanvas.DialogueTrees;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace Ether
{
[UIElement(FrameTier.Top)]
public class DialogueFrame : DialogueFrameBase
{
List<ButtonEx> choiceBtnList = new List<ButtonEx>();
private SubtitlesRequestInfo curSubTitleInfo;
protected override void OnShow()
{
_GoTempChoiceBtn.SetActive(false);
_BtnExBgNext.OnClick.AddListener(() =>
{
curSubTitleInfo.Continue();
});
}
protected override void OnSubscribe()
{
EventCenter.AddListener<SubtitlesRequestInfo>("OnSubtitlesRequest", OnSubtitlesRequest);
EventCenter.AddListener<MultipleChoiceRequestInfo>("OnMultipleChoiceRequest", OnMultipleChoiceRequest);
}
private void OnSubtitlesRequest(SubtitlesRequestInfo info)
{
curSubTitleInfo = info;
foreach (ButtonEx btnEx in choiceBtnList)
{
btnEx.OnClick.RemoveAllListeners();
btnEx.gameObject.SetActive(false);
}
_TextActorName.text = info.actor.name;
_TextContent.text = info.statement.text;
//Debug.LogError($"说话人:{info.actor.name},内容:{info.statement.text},声音:{info.statement.audio}");
}
private void OnMultipleChoiceRequest(MultipleChoiceRequestInfo info)
{
int index = 0;
foreach (var pair in info.options)
{
ButtonEx choiceBtn;
if (index < choiceBtnList.Count)
{
choiceBtn = choiceBtnList[index];
}
else
{
choiceBtn = GameObject.Instantiate(_GoTempChoiceBtn, _TransChoiceBtnList).GetComponent<ButtonEx>();
choiceBtnList.Add(choiceBtn);
}
choiceBtn.gameObject.SetActive(true);
TextMeshProUGUI name = choiceBtn.transform.Find("Name").GetComponent<TextMeshProUGUI>();
name.text = pair.Key.text;
choiceBtn.OnClick.AddListener(() =>
{
info.SelectOption(pair.Value);
});
index++;
}
}
protected override void OnUnSubscribe()
{
EventCenter.RemoveListener<SubtitlesRequestInfo>("OnSubtitlesRequest", OnSubtitlesRequest);
EventCenter.RemoveListener<MultipleChoiceRequestInfo>("OnMultipleChoiceRequest", OnMultipleChoiceRequest);
}
}
}