diff --git a/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn b/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn index 3108624bf..e1a204dfb 100644 --- a/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn +++ b/Assets/Resources/Yarn/OptionTest/OptionTest1.yarn @@ -2,7 +2,7 @@ --- <> 我: 这是选项测试文件 #line:069537f -我: 点击不同按钮可以执行不同命令 #line:00147c3 +我: 点击不同按钮可以执行不同命令 #line:00147c3 # auto_next <> 我: 加载测试场景 #line:0a67910 我: 提供两个按钮进行点击 #line:0cb2011 diff --git a/Assets/Scripts/Dialog System/CommandOptionView.cs b/Assets/Scripts/Dialog System/CommandOptionView.cs index e76f82def..a2ecb0814 100644 --- a/Assets/Scripts/Dialog System/CommandOptionView.cs +++ b/Assets/Scripts/Dialog System/CommandOptionView.cs @@ -42,6 +42,7 @@ namespace AibisDream return; } + DialogController.Instance.IsTalking = false; // 传输选项 YarnOption[] options = YarnOption.GeneOptions(dialogueOptions, onOptionSelected); DialogController.Instance.InvokeOptionCommand(commandName, options); diff --git a/Assets/Scripts/Dialog System/DialogController.cs b/Assets/Scripts/Dialog System/DialogController.cs index fac71ba49..75e9b15da 100644 --- a/Assets/Scripts/Dialog System/DialogController.cs +++ b/Assets/Scripts/Dialog System/DialogController.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Events; using Yarn.Unity; namespace AibisDream @@ -14,8 +15,28 @@ namespace AibisDream private Dictionary> commandMap = new(); - public static event Action OnDialogueStart; - public static event Action OnDialogueComplete; + private bool _isTalking; + + public bool IsTalking + { + set + { + if (_isTalking == value) return; + + _isTalking = value; + if (value) + { + OnDialogueStart?.Invoke(); + } + else + { + OnDialogueComplete?.Invoke(); + } + } + } + + public static UnityAction OnDialogueStart; + public static UnityAction OnDialogueComplete; public bool quickRunMode; @@ -23,8 +44,10 @@ namespace AibisDream { dialogueRunner = GetComponentInChildren(); _variableStorage = GetComponentInChildren(); - dialogueRunner.onDialogueStart.AddListener(() => OnDialogueStart?.Invoke()); - dialogueRunner.onDialogueComplete.AddListener(() => OnDialogueComplete?.Invoke()); + + dialogueRunner.onDialogueStart.AddListener(() => { OnDialogueStart?.Invoke(); }); + dialogueRunner.onDialogueComplete.AddListener(() => { OnDialogueComplete?.Invoke(); }); + dialogueRunner.AddCommandListener(_ => { IsTalking = false; }); } #region 变量操作 @@ -38,12 +61,12 @@ namespace AibisDream { _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); @@ -95,8 +118,7 @@ namespace AibisDream { Debug.Log($"命令 {commandName} 已经存在,将被替换"); } - - Debug.Log(commandMap == null); + commandMap[commandName] = commandAction; } @@ -137,7 +159,6 @@ namespace AibisDream [YarnCommand("character_init")] public static void CharacterInit(string picName) { - Debug.Log(picName); MainSceneController.Instance.Guest.SwitchImage(picName); MainSceneController.Instance.Guest.Show(); } diff --git a/Assets/Scripts/Dialog System/LineView.cs b/Assets/Scripts/Dialog System/LineView.cs index cec44ca91..a11224e96 100644 --- a/Assets/Scripts/Dialog System/LineView.cs +++ b/Assets/Scripts/Dialog System/LineView.cs @@ -1,10 +1,13 @@ using System; +using System.Linq; using Yarn.Unity; namespace AibisDream { public class LineView : DialogueViewBase { + private const string AutoNext = "auto_next"; + // 进入指令选项模式 private bool _isInCommand; @@ -18,9 +21,8 @@ namespace AibisDream base.RunLine(dialogueLine, onDialogueLineFinished); return; } - - DialogUiViewer.Instance.ShowLine(dialogueLine.TextWithoutCharacterName.Text, - dialogueLine.CharacterName, onDialogueLineFinished); + + HandleLineOutput(dialogueLine, onDialogueLineFinished); if (DialogController.Instance.quickRunMode) { @@ -28,6 +30,26 @@ namespace AibisDream } } + private void HandleLineOutput(LocalizedLine dialogueLine, Action onDialogueLineFinished) + { + // 对话系统正在交谈状态 + DialogController.Instance.IsTalking = true; + + // 解析LocalizedLine + if (dialogueLine.Metadata != null && dialogueLine.Metadata.Contains(AutoNext)) + { + // 自动跳过 + DialogUiViewer.Instance.ShowAutoLine(dialogueLine.TextWithoutCharacterName.Text, + dialogueLine.CharacterName, onDialogueLineFinished); + } + else + { + // 需要玩家点击 + DialogUiViewer.Instance.ShowLine(dialogueLine.TextWithoutCharacterName.Text, + dialogueLine.CharacterName, onDialogueLineFinished); + } + } + public override void RunOptions(DialogueOption[] dialogueOptions, Action onOptionSelected) { if (_isInCommand) @@ -37,6 +59,7 @@ namespace AibisDream return; } + DialogController.Instance.IsTalking = true; DialogUiViewer.Instance.ShowOptions(dialogueOptions, onOptionSelected); } diff --git a/Assets/Scripts/UI/DialogUiViewer.cs b/Assets/Scripts/UI/DialogUiViewer.cs index c7e72baaa..4ae1876f2 100644 --- a/Assets/Scripts/UI/DialogUiViewer.cs +++ b/Assets/Scripts/UI/DialogUiViewer.cs @@ -4,6 +4,7 @@ using TMPro; using UnityEngine; using UnityEngine.UI; using Yarn.Unity; +using System.Threading.Tasks; namespace AibisDream { @@ -42,6 +43,16 @@ namespace AibisDream GetDialogComponentInBox(dialogBox); } + private void OnEnable() + { + DialogController.OnDialogueComplete += HideDialog; + } + + private void OnDisable() + { + DialogController.OnDialogueComplete -= HideDialog; + } + /// /// 显示文字 /// @@ -56,6 +67,11 @@ namespace AibisDream actorName.text = actor; dialogText.ShowText(dialogLine); + + // 显示完成后处理 + dialogText.onTextShowed.RemoveAllListeners(); + dialogText.onTextShowed.AddListener(() => nextButton.gameObject.SetActive(true)); + nextButton.onClick.RemoveAllListeners(); nextButton.onClick.AddListener(() => { @@ -63,6 +79,27 @@ namespace AibisDream nextButton.gameObject.SetActive(false); }); } + + /// + /// 显示文字,显示完成自动跳过 + /// + /// + /// + /// + public void ShowAutoLine(string dialogLine, string actor, Action nextStep) + { + dialogBox.SetActive(true); + dialogText.gameObject.SetActive(true); + choiceBox.SetActive(false); + + actorName.text = actor; + nextButton.onClick.RemoveAllListeners(); + dialogText.ShowText(dialogLine); + + // 对话显示完成后自动跳过 + dialogText.onTextShowed.RemoveAllListeners(); + dialogText.onTextShowed.AddListener(() => CommonUtil.Delay(200, nextStep)); + } /// /// 显示选项 @@ -96,11 +133,6 @@ namespace AibisDream } } - public void ShowNextButton() - { - nextButton.gameObject.SetActive(true); - } - public void HideDialog() { dialogBox?.SetActive(false); @@ -140,8 +172,6 @@ namespace AibisDream quickTalkButton = box.transform.Find(QUICK_TALK).gameObject; quickTalkButton.GetComponent