70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
![]() |
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using NodeCanvas.DialogueTrees;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace Net.Project.B.Dialogue
|
||
|
{
|
||
|
public class DialogueMiddlewareNodeCanvasDialogueTree : IDisposable
|
||
|
|
||
|
{
|
||
|
private readonly IDialogueService _dialogueService;
|
||
|
|
||
|
public DialogueMiddlewareNodeCanvasDialogueTree(IDialogueService dialogueService)
|
||
|
{
|
||
|
_dialogueService = dialogueService;
|
||
|
DialogueTree.OnSubtitlesRequest += OnSubtitlesRequest;
|
||
|
_dialogueService.OnDialogueStart += OnDialogueStart;
|
||
|
DialogueTree.OnMultipleChoiceRequest += OnMultipleChoiceRequest;
|
||
|
}
|
||
|
|
||
|
private UniTask OnDialogueStart(IDialogueData arg)
|
||
|
{
|
||
|
if (string.IsNullOrEmpty(arg.VoicePath))
|
||
|
{
|
||
|
return UniTask.Delay(5000);
|
||
|
}
|
||
|
|
||
|
return UniTask.CompletedTask;
|
||
|
}
|
||
|
|
||
|
private async void OnMultipleChoiceRequest(MultipleChoiceRequestInfo obj)
|
||
|
{
|
||
|
var chooses = obj.options.Select(pair => new DialogueChoice()
|
||
|
{
|
||
|
Enabled = true,
|
||
|
Index = pair.Value,
|
||
|
Text = pair.Key.text,
|
||
|
Remark = pair.Key.meta
|
||
|
}).OfType<IDialogueChoice>().ToArray();
|
||
|
var index = await _dialogueService.CreateDialogue(null, chooses);
|
||
|
|
||
|
if (DialogueTree.currentDialogue)
|
||
|
obj.SelectOption.Invoke(index);
|
||
|
}
|
||
|
|
||
|
private async void OnSubtitlesRequest(SubtitlesRequestInfo obj)
|
||
|
{
|
||
|
await _dialogueService.CreateDialogue(new DialogueData()
|
||
|
{
|
||
|
Text = obj.statement.text.Replace("\r",string.Empty),
|
||
|
ActorIdentity = obj.actor.transform.gameObject.GetInstanceID(),
|
||
|
VoicePath = obj.statement.audio?.name
|
||
|
});
|
||
|
|
||
|
if (DialogueTree.currentDialogue)
|
||
|
obj.Continue.Invoke();
|
||
|
}
|
||
|
|
||
|
public void Dispose()
|
||
|
{
|
||
|
DialogueTree.OnSubtitlesRequest -= OnSubtitlesRequest;
|
||
|
DialogueTree.OnMultipleChoiceRequest -= OnMultipleChoiceRequest;
|
||
|
_dialogueService.OnDialogueStart -= OnDialogueStart;
|
||
|
}
|
||
|
}
|
||
|
}
|