forked from NullTale/UnityEventBus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventBusBase.cs
More file actions
59 lines (50 loc) · 1.55 KB
/
EventBusBase.cs
File metadata and controls
59 lines (50 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using UnityEngine;
namespace UnityEventBus
{
/// <summary>
/// EventBusImpl MonoBehaviour wrapper
/// </summary>
public class EventBusBase : MonoBehaviour, IEventBus
{
private IEventBusImpl m_Impl = new EventBusImpl();
// =======================================================================
protected virtual void Awake()
{
foreach (var sub in this.ExtractWrappers())
m_Impl.Subscribe(sub);
}
public void Send<TEvent, TInvoker>(in TEvent e, in TInvoker invoker) where TInvoker : IEventInvoker
{
m_Impl.Send(in e, in invoker);
}
public void Subscribe(ISubscriber sub)
{
// add listeners to the bus
m_Impl.Subscribe(sub);
}
public void UnSubscribe(ISubscriber sub)
{
m_Impl.UnSubscribe(sub);
}
public void Subscribe(IEventBus bus)
{
m_Impl.Subscribe(bus);
}
public void UnSubscribe(IEventBus bus)
{
m_Impl.UnSubscribe(bus);
}
protected virtual void OnDestroy()
{
m_Impl.Dispose();
m_Impl = null;
}
// =======================================================================
[ContextMenu("Log subscribers", false, 0)]
public void LogSubscribers()
{
foreach (var subscriber in m_Impl.GetSubscribers())
Debug.Log(subscriber.ToString(), subscriber.Target as MonoBehaviour);
}
}
}