diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-30 19:35:16 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-30 19:35:16 +0800 |
| commit | 7735b716401f630697d5b02910d50dbf8f3416af (patch) | |
| tree | 52d23e7e0434a569bbafdfca6ad196e3ef24af3f /Assets/Scripts/SoulCoreGameLoop | |
| parent | 4408cc53e044a9c649572991a764f46d247c67bf (diff) | |
正在编写动态场景加载
Diffstat (limited to 'Assets/Scripts/SoulCoreGameLoop')
4 files changed, 83 insertions, 36 deletions
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<AreaHintMessage> { - 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<AreaHintMessage>(); - - public void OnReceive(AreaHintMessage evt) + private void Awake() { - areaName.text = evt.AreaName; - _animator.Play(TriggerPlay); + _animator = GetComponent<Animator>(); + UIEventListener.Register(this); } + + private void OnDestroy() + => UIEventListener.Unregister<AreaHintMessage>(); + - private void Start() + public void OnReceive(AreaHintMessage evt) { - _animator = GetComponent<Animator>(); + 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<BoxCollider>(); + _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 { /// <summary> - /// UI 事件监听器 + /// UI 事件总线(消息类型 → 处理器) /// </summary> public static class UIEventListener { - private static readonly Dictionary<TypeCode, Object> TypeSets = new (); + private static readonly Dictionary<Type, object> Handlers = new(); /// <summary> - /// 向 UI 事件监听器添加发送器实例 + /// 注册某个消息类型的处理器(同一类型只能有一个处理器) /// </summary> - /// <typeparam name="T"> 发送器绑定类型 </typeparam> - public static void JoinListener<T>() where T : new() + public static void Register<T>(IUIEventSender<T> handler) { - var typeCode = Type.GetTypeCode(typeof(T)); - TypeSets.Add(typeCode, new T()); + Handlers[typeof(T)] = handler; } /// <summary> - /// 向 UI 事件监听器发送消息 + /// 移除某个消息类型的处理器 + /// </summary> + public static void Unregister<T>() + { + Handlers.Remove(typeof(T)); + } + + /// <summary> + /// 发送消息 /// </summary> - /// <param name="message"> 发送器 </param> - /// <typeparam name="T"> 消息类型 </typeparam> public static void Send<T>(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<T>) set; - sender.OnReceive(message); + ((IUIEventSender<T>)handlerObj).OnReceive(message); } } } /// <summary> - /// UI 事件发送器 + /// 消息处理器接口 /// </summary> - /// <typeparam name="TEvent"></typeparam> public interface IUIEventSender<in TEvent> { - /// <summary> - /// 当接收到 UI 事件时的处理 - /// </summary> - /// <param name="evt"></param> - public void OnReceive(TEvent evt); + void OnReceive(TEvent evt); } -} +}
\ No newline at end of file |
