This commit is contained in:
commit
04c062bacb
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ee183f11afcda5644bd64299bd2f47f8
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,79 @@
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Net.Project.B.Dialogue
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 对话数据
|
||||||
|
/// </summary>
|
||||||
|
public interface IDialogueData
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 唯一对话ID
|
||||||
|
/// </summary>
|
||||||
|
public int Identity { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 通道ID,用于区分不同的对话
|
||||||
|
/// </summary>
|
||||||
|
public int Channel { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 演员ID,谁是说话的人
|
||||||
|
/// </summary>
|
||||||
|
public int ActorIdentity { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 文本
|
||||||
|
/// </summary>
|
||||||
|
public string Text { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 语音路径
|
||||||
|
/// </summary>
|
||||||
|
public string VoicePath { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 等待对话完成
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
TaskAwaiter GetAwaiter();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 对话选择
|
||||||
|
/// </summary>
|
||||||
|
public interface IDialogueChoice
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 索引
|
||||||
|
/// </summary>
|
||||||
|
public int Index { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 文本
|
||||||
|
/// </summary>
|
||||||
|
public string Text { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 是否可选
|
||||||
|
/// </summary>
|
||||||
|
public bool Enabled { get; }
|
||||||
|
/// <summary>
|
||||||
|
/// 备注,例如:选择这个选项会触发什么事件,不可选的原因等
|
||||||
|
/// </summary>
|
||||||
|
public string Remark { get; }
|
||||||
|
}
|
||||||
|
public struct DialogueData:IDialogueData
|
||||||
|
{
|
||||||
|
public int Identity { get; set; }
|
||||||
|
public int Channel { get; set; }
|
||||||
|
public int ActorIdentity { get; set; }
|
||||||
|
public string Text { get; set; }
|
||||||
|
public string VoicePath { get; set; }
|
||||||
|
public TaskAwaiter GetAwaiter()
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public struct DialogueChoice:IDialogueChoice
|
||||||
|
{
|
||||||
|
public int Index { get; set; }
|
||||||
|
public string Text { get; set; }
|
||||||
|
public bool Enabled { get; set; }
|
||||||
|
public string Remark { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ebb9eb40cce574a44b3419bd4d58411f
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,91 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using BITKit;
|
||||||
|
using Cysharp.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Net.Project.B.Dialogue
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 对话服务
|
||||||
|
/// </summary>
|
||||||
|
public interface IDialogueService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 所有对话
|
||||||
|
/// </summary>
|
||||||
|
IReadOnlyDictionary<int, IDialogueData> Dialogues { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 对话开始
|
||||||
|
/// </summary>
|
||||||
|
public event Func<IDialogueData, UniTask> OnDialogueStart;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 对话结束
|
||||||
|
/// </summary>
|
||||||
|
public event Action<IDialogueData> OnDialogueEnd;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 对话选择
|
||||||
|
/// </summary>
|
||||||
|
public event Func<IDialogueData, IReadOnlyCollection<IDialogueChoice>, UniTask<int>> OnDialogueChoose;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 对话选择回调
|
||||||
|
/// </summary>
|
||||||
|
public event Action<IDialogueData, IDialogueChoice> OnDialogueChose;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开启对话
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dialogueData">对话</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
UniTask CreateDialogue(IDialogueData dialogueData);
|
||||||
|
|
||||||
|
UniTask<int> CreateDialogue(IDialogueData dialogueData, IReadOnlyCollection<IDialogueChoice> dialogueChoices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DialogueService : IDialogueService
|
||||||
|
{
|
||||||
|
public IReadOnlyDictionary<int, IDialogueData> Dialogues => _dialogues;
|
||||||
|
|
||||||
|
public event Func<IDialogueData, UniTask> OnDialogueStart;
|
||||||
|
public event Action<IDialogueData> OnDialogueEnd;
|
||||||
|
public event Func<IDialogueData, IReadOnlyCollection<IDialogueChoice>, UniTask<int>> OnDialogueChoose;
|
||||||
|
public event Action<IDialogueData, IDialogueChoice> OnDialogueChose;
|
||||||
|
|
||||||
|
public async UniTask CreateDialogue(IDialogueData dialogueData)
|
||||||
|
{
|
||||||
|
if (_dialogues.TryAdd(dialogueData.Identity, dialogueData) is false)
|
||||||
|
{
|
||||||
|
dialogueData.Identity = _dialogues.Keys.Max() + 1;
|
||||||
|
_dialogues.TryAdd(dialogueData.Identity, dialogueData);
|
||||||
|
}
|
||||||
|
|
||||||
|
await OnDialogueStart.UniTaskFunc(dialogueData);
|
||||||
|
|
||||||
|
OnDialogueEnd?.Invoke(dialogueData);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async UniTask<int> CreateDialogue(IDialogueData dialogueData,
|
||||||
|
IReadOnlyCollection<IDialogueChoice> dialogueChoices)
|
||||||
|
{
|
||||||
|
if (OnDialogueChoose is null)
|
||||||
|
{
|
||||||
|
throw new NullReferenceException("OnDialogueChoose is null,unable to create dialogue");
|
||||||
|
}
|
||||||
|
|
||||||
|
var task = OnDialogueChoose.Invoke(dialogueData, dialogueChoices);
|
||||||
|
var index = await task;
|
||||||
|
|
||||||
|
OnDialogueChose?.Invoke(dialogueData, dialogueChoices.ElementAt(index));
|
||||||
|
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly ConcurrentDictionary<int, IDialogueData> _dialogues = new();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4b6700c7171a00048b0d5907bd7c0133
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "Net.Project.B.Dialogue",
|
||||||
|
"rootNamespace": "",
|
||||||
|
"references": [
|
||||||
|
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||||
|
"GUID:f51ebe6a0ceec4240a699833d6309b23"
|
||||||
|
],
|
||||||
|
"includePlatforms": [],
|
||||||
|
"excludePlatforms": [],
|
||||||
|
"allowUnsafeCode": false,
|
||||||
|
"overrideReferences": false,
|
||||||
|
"precompiledReferences": [],
|
||||||
|
"autoReferenced": true,
|
||||||
|
"defineConstraints": [],
|
||||||
|
"versionDefines": [],
|
||||||
|
"noEngineReferences": true
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: ba8323a5a01afc24188ded7f4b05db8a
|
||||||
|
AssemblyDefinitionImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"name": "net.project.b.dialogue",
|
||||||
|
"displayName": "Project B Dialogue",
|
||||||
|
"version": "2024.3.31",
|
||||||
|
"unity": "2022.3",
|
||||||
|
"description": "Project B 对话服务",
|
||||||
|
"keywords": [],
|
||||||
|
"dependencies": {}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e4f24e056b51e32478c042d2b2147084
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue