From f4f0b4cf3d684b100cee37ed22577638a2a72a53 Mon Sep 17 00:00:00 2001 From: Ding Yuntian <1491671119@qq.com> Date: Tue, 14 Jan 2025 22:51:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=87=E9=92=88=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Game Loop/EventEnums.cs | 11 ++- Assets/Scripts/UI/Cursor/CursorManager.cs | 93 ++++++++++++++++--- Assets/Scripts/UI/DialogUI/DialogBoxBase.cs | 11 ++- .../Scripts/UI/DialogUI/DialogBubbleViewer.cs | 4 +- 4 files changed, 98 insertions(+), 21 deletions(-) diff --git a/Assets/Scripts/Game Loop/EventEnums.cs b/Assets/Scripts/Game Loop/EventEnums.cs index 6978843dc..7afd3390e 100644 --- a/Assets/Scripts/Game Loop/EventEnums.cs +++ b/Assets/Scripts/Game Loop/EventEnums.cs @@ -7,7 +7,6 @@ namespace AibisDream public enum EventEnum { NextYarn, - DialogOption, PlugIn, PlugOut } @@ -15,13 +14,19 @@ namespace AibisDream public enum DialogEventEnum { LineStart, - OptionShow, - OptionSelected, LineShowed, LineEnd, + OptionShow, + OptionSelected, Command } + public enum InteractionEventEnum + { + Start, + End + } + public struct DialogLine { public bool isOption; diff --git a/Assets/Scripts/UI/Cursor/CursorManager.cs b/Assets/Scripts/UI/Cursor/CursorManager.cs index 0671e38ea..85159b9a3 100644 --- a/Assets/Scripts/UI/Cursor/CursorManager.cs +++ b/Assets/Scripts/UI/Cursor/CursorManager.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; using AibisDream.Framework; using UnityEngine; using UnityEngine.UI; @@ -7,14 +8,24 @@ namespace AibisDream { public class CursorManager : MonoBehaviour { + private const float DelayTime = 0.2f; + [Header("指针图片")] public Sprite defaultCursor; public Sprite nextTalk, talking, option, interactive, holding, disable, eye; private Image _cursor; private RectTransform _cursorCanvas; + #region 状态相关 + + private Coroutine _switchCoroutine; + private CursorState _curState = CursorState.Default; + + #endregion + + // 卸载时注销事件 - private List _eventUnRegisters = new(); + private List _unRegisters = new(); public void Awake() { @@ -24,7 +35,7 @@ namespace AibisDream private void InitCursor() { - SwitchCursorImage(defaultCursor); + SwitchCursor(defaultCursor); } private void OnEnable() @@ -35,12 +46,12 @@ namespace AibisDream private void OnDisable() { // 注销所有事件 - foreach (var eventUnRegister in _eventUnRegisters) + foreach (var eventUnRegister in _unRegisters) { eventUnRegister.UnRegister(); } - _eventUnRegisters.Clear(); + _unRegisters.Clear(); } private void InitRefs() @@ -53,35 +64,87 @@ namespace AibisDream { // 此处监听所有可能对鼠标状态产生影响的事件 // 对话相关 - EnumEventSystem.Global.Register(DialogEventEnum.LineStart, OnDialogLineStart); - EnumEventSystem.Global.Register(DialogEventEnum.OptionShow, () => SwitchCursorImage(option)); + _unRegisters.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineStart, + _ => SwitchCursor(talking))); + _unRegisters.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineShowed, () => SwitchCursor(nextTalk))); + _unRegisters.Add(EnumEventSystem.Global.Register(DialogEventEnum.LineEnd, () => SwitchCursorDelay(defaultCursor))); + _unRegisters.Add(EnumEventSystem.Global.Register(DialogEventEnum.OptionShow, () => SwitchCursor(option))); + _unRegisters.Add(EnumEventSystem.Global.Register(DialogEventEnum.OptionSelected, () => SwitchCursorDelay(defaultCursor))); + // 交互相关 + _unRegisters.Add(EnumEventSystem.Global.Register(InteractionEventEnum.Start, OnInteractiveStart)); + _unRegisters.Add(EnumEventSystem.Global.Register(InteractionEventEnum.End, OnInteractiveEnd)); } private void LateUpdate() { - // 获取鼠标指针在UI Camera上的位置 + // 将虚拟指针与鼠标位置同步 RectTransformUtility.ScreenPointToLocalPointInRectangle(_cursorCanvas, Input.mousePosition, CameraKit.Instance.uiCamera, out var worldPos); _cursor.rectTransform.localPosition = worldPos; + + // 在输入状态,要单独处理 } - private void SwitchCursorImage(Sprite sprite) + #region 交互状态相关 + + private void OnInteractiveStart() { + _curState = CursorState.Interactive; + } + + private void OnInteractiveEnd() + { + _curState = CursorState.Interactive; + } + + #endregion + + #region 指针切换 + + /// + /// 直接切指针 + /// + /// 指针图片 + private void SwitchCursor(Sprite sprite) + { + StopDelayCursor(); _cursor.sprite = sprite; } - private void OnDialogLineStart(DialogLine line) + /// + /// 延迟切换指针。如果后面有其他切换状态,就直接打断 + /// + /// 指针图片 + private void SwitchCursorDelay(Sprite sprite) { - // 对话指针 - if (!line.isOption) SwitchCursorImage(talking); + StopDelayCursor(); + _switchCoroutine = StartCoroutine(SwitchCursorAsync(sprite)); } - private void OnDialogLineEnd() + private IEnumerator SwitchCursorAsync(Sprite sprite) { - // 下一句指针 - SwitchCursorImage(nextTalk); + yield return new WaitForSeconds(DelayTime); + _cursor.sprite = sprite; } + + private void StopDelayCursor() + { + if (_switchCoroutine != null) + { + StopCoroutine(_switchCoroutine); + _switchCoroutine = null; + } + } + + #endregion + } + + public enum CursorState + { + Default, + Interactive, + Eye } } \ No newline at end of file diff --git a/Assets/Scripts/UI/DialogUI/DialogBoxBase.cs b/Assets/Scripts/UI/DialogUI/DialogBoxBase.cs index 981d18ad4..face63efe 100644 --- a/Assets/Scripts/UI/DialogUI/DialogBoxBase.cs +++ b/Assets/Scripts/UI/DialogUI/DialogBoxBase.cs @@ -30,6 +30,7 @@ namespace AibisDream #endregion private bool _skipLineDelay; + private Action _nextStep; [Header("跳过阻塞延迟时间/ms")] [SerializeField] private int delayTime = 300; @@ -110,17 +111,23 @@ namespace AibisDream dialogText.onTextShowed.AddListener(() => nextArray.gameObject.SetActive(true)); dialogText.onTextShowed.AddListener(() => onTextShowed?.Invoke()); dialogText.onTextShowed.AddListener(() => EnumEventSystem.Global.Send(DialogEventEnum.LineShowed)); - + + _nextStep = nextStep; + // 执行自动跳过结局 if (isAutoSkip) { - dialogText.onTextShowed.AddListener(() => _ = CommonUtil.Delay(200, nextStep)); + dialogText.onTextShowed.AddListener(() => _ = CommonUtil.Delay(200, NextStep)); } } public virtual void NextStep() { + if (_nextStep == null) return; + EnumEventSystem.Global.Send(DialogEventEnum.LineEnd); + _nextStep.Invoke(); + _nextStep = null; } /// diff --git a/Assets/Scripts/UI/DialogUI/DialogBubbleViewer.cs b/Assets/Scripts/UI/DialogUI/DialogBubbleViewer.cs index a5567d2dc..62b2f0e2a 100644 --- a/Assets/Scripts/UI/DialogUI/DialogBubbleViewer.cs +++ b/Assets/Scripts/UI/DialogUI/DialogBubbleViewer.cs @@ -21,6 +21,8 @@ namespace AibisDream private IDialogView _currentView; + private Action _nextStep; + private void Awake() { Init(); @@ -74,7 +76,7 @@ namespace AibisDream public void NextStep() { - throw new NotImplementedException(); + _currentView.NextStep(); } private IDialogView SelectBubbleByCharacter(CharacterVo characterVo)