70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BITKit;
|
|
using Cysharp.Threading.Tasks;
|
|
using NodeCanvas.DialogueTrees;
|
|
using UnityEngine;
|
|
|
|
namespace BITFALL.UX
|
|
{
|
|
/// <summary>
|
|
/// 对话组件,基于NodeCanvas.DialogueTrees
|
|
/// </summary>
|
|
public class UXDialogue : MonoBehaviour
|
|
{
|
|
[SerializeField] private UXBuilder dialogueBuilder;
|
|
private CancellationToken _cancellationToken;
|
|
private void Awake()
|
|
{
|
|
DialogueTree.OnSubtitlesRequest += OnSubtitlesRequest;
|
|
_cancellationToken = this.GetCancellationTokenOnDestroy();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
dialogueBuilder.VisualElement.SetActive(false);
|
|
}
|
|
|
|
private async void OnSubtitlesRequest(SubtitlesRequestInfo obj)
|
|
{
|
|
dialogueBuilder.VisualElement.SetActive(true);
|
|
var container = dialogueBuilder.BuildAsContainer();
|
|
container.contextLabel.text = $"{obj.actor.name}:{obj.statement}";
|
|
try
|
|
{
|
|
if (double.TryParse( obj.statement.meta,out var duration))
|
|
{
|
|
await Task.Delay(TimeSpan.FromSeconds(duration), _cancellationToken);
|
|
}
|
|
else
|
|
{
|
|
await Task.Delay(5000, _cancellationToken);
|
|
}
|
|
obj.Continue.Invoke();
|
|
await Task.Delay(5000, _cancellationToken);
|
|
container.visualElement.AddToClassList("dispose");
|
|
await Task.Delay(1000, _cancellationToken);
|
|
container.visualElement.RemoveFromHierarchy();
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
obj.Continue.Invoke();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
dialogueBuilder.VisualElement.SetActive(dialogueBuilder.VisualElement.childCount > 0);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
DialogueTree.OnSubtitlesRequest -= OnSubtitlesRequest;
|
|
}
|
|
}
|
|
|
|
}
|