106 lines
2.6 KiB
C#
106 lines
2.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BaseGames.Core.Events
|
|
{
|
|
/// <summary>
|
|
/// 泛型 SO 事件频道基类。T 为负载类型。
|
|
/// </summary>
|
|
public abstract class BaseEventChannelSO<T> : ScriptableObject
|
|
{
|
|
[Multiline] public string description;
|
|
|
|
private event Action<T> _onEventRaisedBacking;
|
|
#if UNITY_EDITOR
|
|
private int _subscriberCount;
|
|
#endif
|
|
|
|
public event Action<T> OnEventRaised
|
|
{
|
|
add
|
|
{
|
|
_onEventRaisedBacking += value;
|
|
#if UNITY_EDITOR
|
|
_subscriberCount++;
|
|
#endif
|
|
}
|
|
remove
|
|
{
|
|
_onEventRaisedBacking -= value;
|
|
#if UNITY_EDITOR
|
|
_subscriberCount--;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
public void Raise(T value)
|
|
{
|
|
#if UNITY_EDITOR
|
|
EventBusMonitor.Record(name, value?.ToString() ?? "null",
|
|
_subscriberCount,
|
|
Time.frameCount);
|
|
#endif
|
|
_onEventRaisedBacking?.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;
|
|
|
|
private event Action _onEventRaisedBacking;
|
|
#if UNITY_EDITOR
|
|
private int _subscriberCount;
|
|
#endif
|
|
|
|
public event Action OnEventRaised
|
|
{
|
|
add
|
|
{
|
|
_onEventRaisedBacking += value;
|
|
#if UNITY_EDITOR
|
|
_subscriberCount++;
|
|
#endif
|
|
}
|
|
remove
|
|
{
|
|
_onEventRaisedBacking -= value;
|
|
#if UNITY_EDITOR
|
|
_subscriberCount--;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
public void Raise()
|
|
{
|
|
#if UNITY_EDITOR
|
|
EventBusMonitor.Record(name, "<void>",
|
|
_subscriberCount,
|
|
Time.frameCount);
|
|
#endif
|
|
_onEventRaisedBacking?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 订阅并返回可 Dispose 的订阅句柄。
|
|
/// </summary>
|
|
public EventSubscription Subscribe(Action callback)
|
|
{
|
|
OnEventRaised += callback;
|
|
return new EventSubscription(() => OnEventRaised -= callback);
|
|
}
|
|
}
|
|
}
|