自动跳过和判断是否在对话的逻辑
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
---
|
||||
<<character_init Art/Character/Express_1>>
|
||||
我: 这是选项测试文件 #line:069537f
|
||||
我: 点击不同按钮可以执行不同命令 #line:00147c3
|
||||
我: 点击不同按钮可以执行不同命令 #line:00147c3 # auto_next
|
||||
<<load_scene OptionTestScene>>
|
||||
我: 加载测试场景 #line:0a67910
|
||||
我: 提供两个按钮进行点击 #line:0cb2011
|
||||
|
||||
@@ -42,6 +42,7 @@ namespace AibisDream
|
||||
return;
|
||||
}
|
||||
|
||||
DialogController.Instance.IsTalking = false;
|
||||
// 传输选项
|
||||
YarnOption[] options = YarnOption.GeneOptions(dialogueOptions, onOptionSelected);
|
||||
DialogController.Instance.InvokeOptionCommand(commandName, options);
|
||||
|
||||
@@ -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<string, Action<YarnOption[]>> 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<DialogueRunner>();
|
||||
_variableStorage = GetComponentInChildren<InMemoryVariableStorage>();
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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<int> onOptionSelected)
|
||||
{
|
||||
if (_isInCommand)
|
||||
@@ -37,6 +59,7 @@ namespace AibisDream
|
||||
return;
|
||||
}
|
||||
|
||||
DialogController.Instance.IsTalking = true;
|
||||
DialogUiViewer.Instance.ShowOptions(dialogueOptions, onOptionSelected);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示文字
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示文字,显示完成自动跳过
|
||||
/// </summary>
|
||||
/// <param name="dialogLine"></param>
|
||||
/// <param name="actor"></param>
|
||||
/// <param name="nextStep"></param>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示选项
|
||||
@@ -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<Button>().onClick.AddListener(SwitchQuickTalkMode);
|
||||
|
||||
dialogText.onTextShowed.AddListener(ShowNextButton);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream
|
||||
@@ -29,6 +32,17 @@ namespace AibisDream
|
||||
{
|
||||
return Mathf.Abs(value1 - value2) < Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 简单延时实现
|
||||
/// </summary>
|
||||
/// <param name="delayTime">延时</param>
|
||||
/// <param name="action">函数</param>
|
||||
public static async Task Delay(int delayTime, Action action)
|
||||
{
|
||||
await Task.Delay(delayTime);
|
||||
action?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,6 @@ namespace AibisDream
|
||||
|
||||
public void FadeOut(float duration)
|
||||
{
|
||||
Debug.Log("Fade out");
|
||||
_image.gameObject.SetActive(true);
|
||||
_image.DOBlendableColor(Color.clear, duration).onComplete += () =>
|
||||
{
|
||||
|
||||
@@ -1,28 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Yarn;
|
||||
using Yarn.Markup;
|
||||
using Yarn.Unity;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public static class YarnUtil
|
||||
{
|
||||
private const string COMMAND_ATTR = "command";
|
||||
private const string CommandAttr = "command";
|
||||
|
||||
private const string OptionAttr = "options";
|
||||
|
||||
private const string COMMAND_PROP = "name";
|
||||
private const string CommandProp = "name";
|
||||
|
||||
public static bool HasCommandAttr(this MarkupParseResult text)
|
||||
{
|
||||
return text.TryGetAttributeWithName(COMMAND_ATTR, out var attribute1) ||
|
||||
return text.TryGetAttributeWithName(CommandAttr, out var attribute1) ||
|
||||
text.TryGetAttributeWithName(OptionAttr, out var attribute2);
|
||||
}
|
||||
|
||||
public static bool TryGetCommandAttr(this MarkupParseResult text, out string commandName)
|
||||
{
|
||||
bool res = text.TryGetAttributeWithName(COMMAND_ATTR, out var attribute);
|
||||
commandName = res ? attribute.Properties[COMMAND_PROP].StringValue : null;
|
||||
bool res = text.TryGetAttributeWithName(CommandAttr, out var attribute);
|
||||
commandName = res ? attribute.Properties[CommandProp].StringValue : null;
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -30,5 +32,10 @@ namespace AibisDream
|
||||
{
|
||||
return text.TryGetAttributeWithName(OptionAttr, out var outStr);
|
||||
}
|
||||
|
||||
public static void AddCommandListener(this DialogueRunner runner, CommandHandler onCommand)
|
||||
{
|
||||
runner.Dialogue.CommandHandler += onCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user