From 7735b716401f630697d5b02910d50dbf8f3416af Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Sat, 30 May 2026 19:35:16 +0800 Subject: 正在编写动态场景加载 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/SoulCoreGameLoop/UI/AreaHint.cs | 26 ++++++------- .../SoulCoreGameLoop/UI/AreaHintGenerator.cs | 39 ++++++++++++++++++++ .../SoulCoreGameLoop/UI/AreaHintGenerator.cs.meta | 11 ++++++ .../Scripts/SoulCoreGameLoop/UI/UIEventListener.cs | 43 ++++++++++------------ 4 files changed, 83 insertions(+), 36 deletions(-) create mode 100644 Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs create mode 100644 Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs.meta (limited to 'Assets/Scripts/SoulCoreGameLoop/UI') diff --git a/Assets/Scripts/SoulCoreGameLoop/UI/AreaHint.cs b/Assets/Scripts/SoulCoreGameLoop/UI/AreaHint.cs index ab03647..6b676fe 100644 --- a/Assets/Scripts/SoulCoreGameLoop/UI/AreaHint.cs +++ b/Assets/Scripts/SoulCoreGameLoop/UI/AreaHint.cs @@ -1,31 +1,31 @@ using System; -using TMPro; -using UnityEditor; using UnityEngine; +using TMPro; namespace SoulCoreGameLoop.UI { [RequireComponent(typeof(Animator))] public class AreaHint : MonoBehaviour, IUIEventSender { - private const String TriggerPlay = "Play"; + private static readonly int TriggerPlay = Animator.StringToHash("Play"); public TMP_Text areaName; private Animator _animator; - [InitializeOnLoadMethod] - public static void RegisterListener () - => UIEventListener.JoinListener(); - - public void OnReceive(AreaHintMessage evt) + private void Awake() { - areaName.text = evt.AreaName; - _animator.Play(TriggerPlay); + _animator = GetComponent(); + UIEventListener.Register(this); } + + private void OnDestroy() + => UIEventListener.Unregister(); + - private void Start() + public void OnReceive(AreaHintMessage evt) { - _animator = GetComponent(); + areaName.text = evt.AreaName; + _animator.SetTrigger(TriggerPlay); } } @@ -38,4 +38,4 @@ namespace SoulCoreGameLoop.UI AreaName = areaName; } } -} +} \ No newline at end of file diff --git a/Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs b/Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs new file mode 100644 index 0000000..efc5e5f --- /dev/null +++ b/Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs @@ -0,0 +1,39 @@ +using UnityEngine; + +namespace SoulCoreGameLoop.UI +{ + [RequireComponent(typeof(BoxCollider))] + public class AreaHintGenerator : MonoBehaviour + { + [Header("名字")] + public new string name = "土豆祭祀场"; + + private BoxCollider _collider; + + private void Awake() + { + _collider = GetComponent(); + _collider.isTrigger = true; + } + + private void OnTriggerEnter(Collider other) + { + Debug.Log("AreaHintGenerator OnTriggerEnter"); + // 向监听器发送消息 + UIEventListener.Send(new AreaHintMessage(name)); + } + + private void OnDrawGizmosSelected() + { + if (_collider == null) return; + + var cs = _collider.size; + var ts = transform.lossyScale; + var size = new Vector3(cs.x * ts.x, cs.y * ts.y, cs.z * ts.z); + + Gizmos.color = new Color(1f, 1f, 1f, 0.5f); + Gizmos.matrix = transform.localToWorldMatrix; + Gizmos.DrawCube(_collider.center, size); + } + } +} diff --git a/Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs.meta b/Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs.meta new file mode 100644 index 0000000..1282763 --- /dev/null +++ b/Assets/Scripts/SoulCoreGameLoop/UI/AreaHintGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 33b85f0adc67b1a43b0e1033f466d85e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SoulCoreGameLoop/UI/UIEventListener.cs b/Assets/Scripts/SoulCoreGameLoop/UI/UIEventListener.cs index 2d33a9b..0b17b6f 100644 --- a/Assets/Scripts/SoulCoreGameLoop/UI/UIEventListener.cs +++ b/Assets/Scripts/SoulCoreGameLoop/UI/UIEventListener.cs @@ -1,52 +1,49 @@ using System; using System.Collections.Generic; -using Object = System.Object; namespace SoulCoreGameLoop.UI { /// - /// UI 事件监听器 + /// UI 事件总线(消息类型 → 处理器) /// public static class UIEventListener { - private static readonly Dictionary TypeSets = new (); + private static readonly Dictionary Handlers = new(); /// - /// 向 UI 事件监听器添加发送器实例 + /// 注册某个消息类型的处理器(同一类型只能有一个处理器) /// - /// 发送器绑定类型 - public static void JoinListener() where T : new() + public static void Register(IUIEventSender handler) { - var typeCode = Type.GetTypeCode(typeof(T)); - TypeSets.Add(typeCode, new T()); + Handlers[typeof(T)] = handler; } /// - /// 向 UI 事件监听器发送消息 + /// 移除某个消息类型的处理器 + /// + public static void Unregister() + { + Handlers.Remove(typeof(T)); + } + + /// + /// 发送消息 /// - /// 发送器 - /// 消息类型 public static void Send(T message) { - var typeCode = Type.GetTypeCode(typeof(T)); - if (TypeSets.TryGetValue(typeCode, out var set)) + Type type = typeof(T); + if (Handlers.TryGetValue(type, out object handlerObj)) { - var sender = (IUIEventSender) set; - sender.OnReceive(message); + ((IUIEventSender)handlerObj).OnReceive(message); } } } /// - /// UI 事件发送器 + /// 消息处理器接口 /// - /// public interface IUIEventSender { - /// - /// 当接收到 UI 事件时的处理 - /// - /// - public void OnReceive(TEvent evt); + void OnReceive(TEvent evt); } -} +} \ No newline at end of file -- cgit