using System.Collections.Generic; using UnityEngine; namespace BaseGames.Core.Events { /// /// 运行时事件频道注册表。 /// 在 Persistent 场景 Awake 时由 EventChannelRegistrar 注册所有频道 SO, /// 供不能持有 [SerializeField] 的动态对象(CharmEffect SO 等)按类型名查找频道。 /// public class EventChannelRegistry : MonoBehaviour, IEventChannelRegistry { private readonly Dictionary _channels = new(); private void Awake() { if (BaseGames.Core.ServiceLocator.GetOrDefault() != null) { Destroy(gameObject); return; } BaseGames.Core.ServiceLocator.Register(this); } /// 由 EventChannelRegistrar 在场景初始化时批量注册频道 SO。 public void Register(string key, ScriptableObject channel) => _channels[key] = channel; /// /// 按 key 查找频道。key = SO 资产文件名(不含扩展名),如 "EVT_HitConfirmed"。 /// public T Get(string key) where T : ScriptableObject { if (_channels.TryGetValue(key, out var ch) && ch is T typed) return typed; Debug.LogError($"[EventChannelRegistry] Key '{key}' not found or wrong type ({typeof(T).Name})."); return null; } /// 尝试获取频道,不报错。 public bool TryGet(string key, out T channel) where T : ScriptableObject { if (_channels.TryGetValue(key, out var ch) && ch is T typed) { channel = typed; return true; } channel = null; return false; } } }