62 lines
1.7 KiB
C#
62 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BaseGames.Core.Events
|
|
{
|
|
/// <summary>
|
|
/// 泛型 SO 事件频道基类。T 为负载类型。
|
|
/// </summary>
|
|
public abstract class BaseEventChannelSO<T> : ScriptableObject
|
|
{
|
|
[Multiline] public string description;
|
|
|
|
public event Action<T> OnEventRaised;
|
|
|
|
public void Raise(T value)
|
|
{
|
|
#if UNITY_EDITOR
|
|
EventBusMonitor.Record(name, value?.ToString() ?? "null",
|
|
OnEventRaised?.GetInvocationList().Length ?? 0);
|
|
#endif
|
|
OnEventRaised?.Invoke(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 订阅并返回可 Dispose 的订阅句柄,配合 CompositeDisposable 使用。
|
|
/// </summary>
|
|
public EventSubscription Subscribe(Action<T> callback)
|
|
{
|
|
OnEventRaised += callback;
|
|
return new EventSubscription(() => OnEventRaised -= callback);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 无负载事件频道基类。
|
|
/// </summary>
|
|
public abstract class VoidBaseEventChannelSO : ScriptableObject
|
|
{
|
|
[Multiline] public string description;
|
|
|
|
public event Action OnEventRaised;
|
|
|
|
public void Raise()
|
|
{
|
|
#if UNITY_EDITOR
|
|
EventBusMonitor.Record(name, "<void>",
|
|
OnEventRaised?.GetInvocationList().Length ?? 0);
|
|
#endif
|
|
OnEventRaised?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 订阅并返回可 Dispose 的订阅句柄。
|
|
/// </summary>
|
|
public EventSubscription Subscribe(Action callback)
|
|
{
|
|
OnEventRaised += callback;
|
|
return new EventSubscription(() => OnEventRaised -= callback);
|
|
}
|
|
}
|
|
}
|