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 { /// /// 对话组件,基于NodeCanvas.DialogueTrees /// public class UXDialogue : MonoBehaviour { [SerializeField] private UXBuilder dialogueBuilder; private readonly ValidHandle allowBackground = new(); private readonly List _dialogueIds = new(); private void Start() { allowBackground.AddListener(dialogueBuilder.VisualElement.SetActive); allowBackground.Invoke(); destroyCancellationToken.Register(() => { DialogueTree.OnSubtitlesRequest -= OnSubtitlesRequest; }); } private async void OnSubtitlesRequest(SubtitlesRequestInfo obj) { var id = obj.GetHashCode(); _dialogueIds.Add(id); 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), destroyCancellationToken); } else { await Task.Delay(5000, destroyCancellationToken); } obj.Continue.Invoke(); await Task.Delay(5000, destroyCancellationToken); container.visualElement.AddToClassList("dispose"); await Task.Delay(1000, destroyCancellationToken); container.visualElement.RemoveFromHierarchy(); } catch (OperationCanceledException) { obj.Continue.Invoke(); } _dialogueIds.Remove(id); } private void Update() { allowBackground.SetElements(this, _dialogueIds.Count is not 0); } } }