diff --git a/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn b/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn index e1eeda74d..3108624bf 100644 --- a/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn +++ b/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn @@ -2,19 +2,45 @@ --- <> 我: 这是选项测试文件 #line:069537f -<> -<> 我: 点击不同按钮可以执行不同命令 #line:00147c3 -<> <> 我: 加载测试场景 #line:0a67910 我: 提供两个按钮进行点击 #line:0cb2011 我: 代码部分在Asset->Scripts->Test->OptionTest #line:0344182 + +<> +<> + [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 === diff --git a/Assets/Scripts/Dialog System/CommandOptionView.cs b/Assets/Scripts/Dialog System/CommandOptionView.cs index fcaeb9813..f4f118f9e 100644 --- a/Assets/Scripts/Dialog System/CommandOptionView.cs +++ b/Assets/Scripts/Dialog System/CommandOptionView.cs @@ -11,12 +11,19 @@ namespace AibisDream public override void RunLine(LocalizedLine dialogueLine, Action onDialogueLineFinished) { + // 判断选项选项行 + if (dialogueLine.Text.IsOptionAttrLine()) + { + isInCommand = true; + base.RunLine(dialogueLine, onDialogueLineFinished); + return; + } // 判断是否进入Command状态 - isInCommand = dialogueLine.Text.TryGetCommandAttr(out var name); + isInCommand = dialogueLine.Text.TryGetCommandAttr(out var curCommandName); if (isInCommand) { // 保存指令 - commandName = name; + commandName = curCommandName; // 进入下一步 onDialogueLineFinished.Invoke(); } diff --git a/Assets/Scripts/Dialog System/DialogController.cs b/Assets/Scripts/Dialog System/DialogController.cs index fa19df2c6..03f0ace99 100644 --- a/Assets/Scripts/Dialog System/DialogController.cs +++ b/Assets/Scripts/Dialog System/DialogController.cs @@ -10,6 +10,8 @@ namespace AibisDream { private DialogueRunner dialogueRunner; + private VariableStorageBehaviour _variableStorage; + private Dictionary> commandMap = new(); public bool quickRunMode; @@ -17,8 +19,33 @@ namespace AibisDream private void Start() { dialogueRunner = GetComponentInChildren(); + _variableStorage = GetComponentInChildren(); } + #region 变量操作 + + public bool TryGetVariable(string variableName, out T result) + { + return _variableStorage.TryGetValue(variableName, out result); + } + + public void SetVariable(string variableName, string value) + { + _variableStorage.SetValue(variableName, value); + } + + public void SetVariable(string variableName, bool value) + { + _variableStorage.SetValue(variableName, value); + } + + public void SetVariable(string variableName, float value) + { + _variableStorage.SetValue(variableName, value); + } + + #endregion + /// /// 开启对话 /// @@ -158,7 +185,6 @@ namespace AibisDream [YarnCommand("fade_in")] public static IEnumerator FadeIn(float duration = 1) { - Debug.Log("fade_in"); return MainUIController.Instance.FadeInSync(duration); } diff --git a/Assets/Scripts/Dialog System/YarnOption.cs b/Assets/Scripts/Dialog System/YarnOption.cs index 5991e92b7..997dab65a 100644 --- a/Assets/Scripts/Dialog System/YarnOption.cs +++ b/Assets/Scripts/Dialog System/YarnOption.cs @@ -8,25 +8,26 @@ namespace AibisDream { public class YarnOption { - private DialogueOption dialogueOption; - - private Action onOptionSelected; - - public YarnOption(DialogueOption dialogueOption, Action onOptionSelected) - { - this.dialogueOption = dialogueOption; - this.onOptionSelected = onOptionSelected; - } + private int _idx; + + private readonly DialogueOption _dialogueOption; + private Action _onOptionSelected; + /// /// 选项中的文字 /// - public string Text + public string Text => _dialogueOption.Line.TextWithoutCharacterName.Text; + + public bool IsAvailable => _dialogueOption.IsAvailable; + + public int Index => _idx; + + public YarnOption(int idx, DialogueOption dialogueOption, Action onOptionSelected) { - get - { - return dialogueOption.Line.TextWithoutCharacterName.Text; - } + _dialogueOption = dialogueOption; + _onOptionSelected = onOptionSelected; + _idx = idx; } /// @@ -34,15 +35,21 @@ namespace AibisDream /// public void SelectOption() { - if (onOptionSelected == null) + if (!IsAvailable) + { + Debug.LogWarning($"选项 {Text} 不可用"); + return; + } + + if (_onOptionSelected == null) { Debug.LogWarning($"选项 {Text} 已经执行过了"); + return; } - else - { - onOptionSelected.Invoke(dialogueOption.DialogueOptionID); - onOptionSelected = null; - } + + // 执行选项 + _onOptionSelected.Invoke(_dialogueOption.DialogueOptionID); + _onOptionSelected = null; } /// @@ -53,10 +60,10 @@ namespace AibisDream /// 包装选项 public static YarnOption[] GeneOptions(DialogueOption[] options, Action onOptionSelected) { - YarnOption[] yarnOptions = new YarnOption[options.Length]; - for (int i = 0; i < options.Length; i++) + var yarnOptions = new YarnOption[options.Length]; + for (var i = 0; i < options.Length; i++) { - yarnOptions[i] = new YarnOption(options[i], onOptionSelected); + yarnOptions[i] = new YarnOption(i, options[i], onOptionSelected); } return yarnOptions; diff --git a/Assets/Scripts/Test/OptionTest.cs b/Assets/Scripts/Test/OptionTest.cs index bbe7ef20e..8ee45b6d3 100644 --- a/Assets/Scripts/Test/OptionTest.cs +++ b/Assets/Scripts/Test/OptionTest.cs @@ -1,8 +1,10 @@ using System.Collections; using System.Collections.Generic; using TMPro; +using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; +using Yarn.Unity; namespace AibisDream.Test { @@ -10,6 +12,8 @@ namespace AibisDream.Test { private GameObject panel; private GameObject prefab; + private GameObject[] buttonGroup; + private YarnOption[] _options; void Start() { @@ -29,18 +33,32 @@ namespace AibisDream.Test /// 触发时提供的选择项 public void ShowButtons(YarnOption[] options) { - foreach (YarnOption option in options) - { - var instance = Instantiate(prefab, panel.transform, true); - instance.GetComponentInChildren().text = option.Text; - instance.GetComponent