diff --git a/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn b/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn index 6db56c081..ba1589cce 100644 --- a/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn +++ b/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn @@ -1,4 +1,4 @@ -title: Start +title: Start --- <> me: 这是选项测试文件 #line:069537f @@ -21,36 +21,7 @@ peipei: 做一点测试 <> <> -[command name=show_buttons /] #line:006eeaa -<> -=== - -title: OptionButton -position: -42,237 ---- -[options /] // 如果选项需要跳转回来重复选择,需要加这行,不然会出问题 - -> 按钮1 #line:0db0f08 - <> - 我: 按下了按钮1 #line:0d84460 - <> - <> // 这里可以添加其他功能,比如按钮打开 - <> - 我: 已经按过按钮1了 #line:0f6f363 - <> - <> - -> 按钮2 #line:0556913 - <> - 我: 按下了按钮2 #line:0b552f4 - <> - <> // 这里可以添加其他功能,比如按钮打开 - <> - 我: 已经按过按钮2了 #line:09081bc - <> - <> - -> 点击进入深度1 #line:0b33abc // 在代码里判断选项是否可用 - 我: 已进入深度1 #line:00ffa7f - -> 点击进入深度2 #line:02d1695 - 我: 已进入深度2 #line:072a4b9 - -我: 测试完成 #line:011c95f +[pause name="show_buttons" /] +我: 继续对话 +我: 结束 === diff --git a/Assets/Scripts/Dialog System/CommandOptionView.cs b/Assets/Scripts/Dialog System/CommandOptionView.cs index b013d5b1b..aba19bdd5 100644 --- a/Assets/Scripts/Dialog System/CommandOptionView.cs +++ b/Assets/Scripts/Dialog System/CommandOptionView.cs @@ -9,10 +9,6 @@ namespace AibisDream // 进入指令选项模式 private bool _waitCommandOptions; private string _commandName; - - // 进入暂停后 - private bool _isInPause; - private Action _pauseNextStep; public override void RunLine(LocalizedLine dialogueLine, Action onDialogueLineFinished) { @@ -58,13 +54,17 @@ namespace AibisDream switch (commandLineType) { case CommandLineType.Pause: - DialogController.Instance.PauseTalk(onDialogueLineFinished); + // 获取选项名称 + dialogueLine.Text.TryGetPauseCommandAttr(out var pauseCommandName); + // 传递暂停指令 + var step = YarnContinueStep.GeneContinueStep(onDialogueLineFinished); + DialogController.Instance.InvokePauseCommand(pauseCommandName, step); break; case CommandLineType.OptionCommand: // 选项指令 _waitCommandOptions = true; - dialogueLine.Text.TryGetCommandAttr(out var curCommandName); + dialogueLine.Text.TryGetOptionCommandAttr(out var curCommandName); // 保存指令 _commandName = curCommandName; // 进入下一步 @@ -73,6 +73,7 @@ namespace AibisDream case CommandLineType.NonCommand: default: // 非Command直接跳过 + _waitCommandOptions = false; base.RunLine(dialogueLine, onDialogueLineFinished); break; } diff --git a/Assets/Scripts/Dialog System/DialogController.cs b/Assets/Scripts/Dialog System/DialogController.cs index 21a88b2a0..7d15699ec 100644 --- a/Assets/Scripts/Dialog System/DialogController.cs +++ b/Assets/Scripts/Dialog System/DialogController.cs @@ -18,6 +18,7 @@ namespace AibisDream private VariableStorageBehaviour _variableStorage; private readonly Dictionary> commandMap = new(); + private readonly Dictionary> _pauseCommandMap = new(); # region controller状态标识 @@ -47,9 +48,8 @@ namespace AibisDream public bool quickRunMode; // 暂停对话 - private bool _isInPause; - private Action _pauseNextStep; - + public bool isInPause; + # endregion public static UnityAction OnDialogueStart; @@ -112,29 +112,7 @@ namespace AibisDream dialogueRunner.StartDialogue("Start"); } - /// - /// 继续对话 - /// - public bool TryContinueTalk() - { - // 如果对话系统在暂停状态 - if (_isInPause) - { - _pauseNextStep.Invoke(); - _isInPause = false; - _pauseNextStep = null; - return true; - } - - // 如果对话系统不在暂停状态 - return false; - } - - public void PauseTalk(Action pauseNextStep) - { - _isInPause = true; - _pauseNextStep = pauseNextStep; - } + #region 选项指令 /// /// 调用命令 @@ -190,6 +168,67 @@ namespace AibisDream } } + #endregion + + #region 暂停选项指令 + + /// + /// 执行暂停命令 + /// + /// 命令名 + /// 参数 + public void InvokePauseCommand(string commandName, YarnContinueStep pauseCommand) + { + if (string.IsNullOrEmpty(commandName)) + { + Debug.Log("命令名为空"); + return; + } + + if (_pauseCommandMap.TryGetValue(commandName, out var action)) + { + action(pauseCommand); + } + else + { + Debug.LogWarning($"命令 {commandName} 不存在"); + } + } + + /// + /// 注册暂停命令 + /// + /// 命令名 + /// 注册命令函数 + public void RegisterPauseCommand(string commandName, Action commandAction) + { + if (_pauseCommandMap.ContainsKey(commandName)) + { + Debug.Log($"命令 {commandName} 已经存在,将被替换"); + } + + _pauseCommandMap[commandName] = commandAction; + } + + /// + /// 移除命令 + /// + /// 命令名称 + public void RemovePauseCommand(string commandName) + { + if (_pauseCommandMap.ContainsKey(commandName)) + { + _pauseCommandMap.Remove(commandName); + } + else + { + Debug.Log($"命令 {commandName} 不存在"); + } + } + + #endregion + + #region 命令名 [YarnCommand("switch_tv")] diff --git a/Assets/Scripts/Dialog System/YarnOption.cs b/Assets/Scripts/Dialog System/YarnOption.cs index 997dab65a..d3bb4044c 100644 --- a/Assets/Scripts/Dialog System/YarnOption.cs +++ b/Assets/Scripts/Dialog System/YarnOption.cs @@ -1,6 +1,4 @@ using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine; using Yarn.Unity; @@ -69,5 +67,43 @@ namespace AibisDream return yarnOptions; } } + + public class YarnContinueStep + { + private Action _nextStep; + + public YarnContinueStep(Action nextStep) + { + _nextStep = nextStep; + } + + /// + /// 继续对话 + /// + public void ContinueTalk() + { + if (_nextStep != null) + { + _nextStep.Invoke(); + DialogController.Instance.isInPause = false; + _nextStep = null; + } + else + { + Debug.LogWarning("对话已经继续,请勿重复触发"); + } + } + + /// + /// 生成继续命令 + /// + /// 下一句命令 + /// 命令 + public static YarnContinueStep GeneContinueStep(Action nextStep) + { + DialogController.Instance.isInPause = true; + return new YarnContinueStep(nextStep); + } + } } diff --git a/Assets/Scripts/Test/OptionTest.cs b/Assets/Scripts/Test/OptionTest.cs index 93aef3842..d88031752 100644 --- a/Assets/Scripts/Test/OptionTest.cs +++ b/Assets/Scripts/Test/OptionTest.cs @@ -20,72 +20,24 @@ namespace AibisDream.Test // 初始化时注册Command // 第一个参数为[command name=xxx]的参数 // 第二个参数是注册的函数,在Yarn走到选择项时触发 - DialogController.Instance.RegisterOptionCommand("show_buttons", ShowButtons); + DialogController.Instance.RegisterPauseCommand("show_buttons", ShowButtons); // 不重要 panel = transform.GetChild(0).gameObject; prefab = Resources.Load("Prefabs/UI/Choice Button"); } - /// - /// 注册的命令 - /// - /// 触发时提供的选择项 - public void ShowButtons(YarnOption[] options) + public void ShowButtons(YarnContinueStep step) { - // 将前两个选项生成清单 - buttonGroup = InstantiateButtons(options); - - _options = options; - } - - private GameObject[] InstantiateButtons(YarnOption[] options) - { - // 这里默认是前两个选项生成按钮,你也可以利用Text或Index来判断哪些是需要生成按钮的选项 - GameObject[] res = new GameObject[2]; - for (int i = 0; i < 2; i++) - { - var option = options[i]; - var instance = Instantiate(prefab, panel.transform, true); - var textMesh = instance.GetComponentInChildren(); - textMesh.text = option.Text; + var option = step; + var instance = Instantiate(prefab, panel.transform, true); + var textMesh = instance.GetComponentInChildren(); + textMesh.text = "继续对话"; - instance.GetComponent