-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventBase.cs
162 lines (137 loc) · 4.48 KB
/
EventBase.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Description: Base Scriptable Object that serves simultaneously the role of the Message and Message Center.
* Copyright: © 2017-2020 Kornel, MIT License. Please see 'README.md' and 'LICENSE' files for more information.
**/
using System;
using System.Collections.Generic;
using UnityEngine;
public class EventBase: ScriptableObject, IEvent
{
private List<IEventListener> listeners = new List<IEventListener>();
private List<Action> listenersA = new List<Action>();
public void Broadcast(GameObject gameObject)
{
EventLogging.Log
(
LogType.Broadcast,
ToString(),
gameObject != null ? gameObject.name : "N/A",
"N/A",
$"<b>{name}</b> is being broadcast to all subscribing Listeners.</b>",
gameObject
);
for(int i = listeners.Count - 1; i >= 0; i--)
{
listeners[i].OnEventRaised();
}
for(int i = listenersA.Count - 1; i >= 0; i--)
{
EventLogging.Log(LogType.Received, ToString(), listenersA[i].Method.Name, "N/A", $"Event received by <b>{listenersA[i].Method.Name}</b>.", null);
listenersA[i].Invoke();
}
}
public void Subscribe(IEventListener listener)
{
listeners.Add(listener);
}
public void Subscribe(Action listener)
{
listenersA.Add(listener);
}
public void UnSubscribe(IEventListener listener)
{
listeners.Remove(listener);
}
public void UnSubscribe(Action listener)
{
listenersA.Remove(listener);
}
}
public class EventBase<T>: ScriptableObject, IEvent<T>
{
private List<IEventListener<T>> listeners = new List<IEventListener<T>>();
private List<Action<T>> listenersA = new List<Action<T>>();
public void Broadcast(GameObject gameObject, T parameter)
{
EventLogging.Log
(
LogType.Broadcast,
ToString(),
gameObject != null ? gameObject.name : "N/A",
parameter.ToString(),
$"<b>{name}</b> is being broadcast to all subscribing Listeners. Value passed: <b>{parameter}</b>",
gameObject
);
for(int i = listeners.Count - 1; i >= 0; i--)
{
listeners[i].OnEventRaised(parameter);
}
for(int i = listenersA.Count - 1; i >= 0; i--)
{
EventLogging.Log(LogType.Received, ToString(), listenersA[i].Method.Name, parameter.ToString(), $"Event received by <b>{listenersA[i].Method.Name}</b>.", null);
listenersA[i].Invoke(parameter);
}
}
public void Subscribe(IEventListener<T> listener)
{
listeners.Add(listener);
}
public void Subscribe(Action<T> listener)
{
EventLogging.Log(LogType.Subscribed, name, "N/A", GetType().Name, $"{listener} subscribed directly to {name}.", null);
listenersA.Add(listener);
}
public void UnSubscribe(IEventListener<T> listener)
{
listeners.Remove(listener);
}
public void UnSubscribe(Action<T> listener)
{
EventLogging.Log(LogType.UnSubscribed, name, "N/A", GetType().Name, $"{listener} unsubscribed directly to {name}.", null);
listenersA.Remove(listener);
}
}
public class EventBase<T, U>: ScriptableObject, IEvent<T, U>
{
private List<IEventListener<T, U>> listeners = new List<IEventListener<T, U>>();
private List<Action<T, U>> listenersA = new List<Action<T, U>>();
public void Broadcast(GameObject gameObject, T parameter1, U parameter2)
{
EventLogging.Log
(
LogType.Broadcast,
ToString(),
gameObject != null ? gameObject.name : "N/A",
$"({parameter1}, {parameter2})",
$"<b>{name}</b> is being broadcast to all subscribing Listeners. Values passed: <b>{parameter1}</b>, <b>{parameter2}</b>",
gameObject
);
for(int i = listeners.Count - 1; i >= 0; i--)
{
listeners[i].OnEventRaised(parameter1, parameter2);
}
for(int i = listenersA.Count - 1; i >= 0; i--)
{
EventLogging.Log(LogType.Received, ToString(), listenersA[i].Method.Name, $"({parameter1}, {parameter2})", $"Event received by <b>{listenersA[i].Method.Name}</b>.", null);
listenersA[i].Invoke(parameter1, parameter2);
}
}
public void Subscribe(IEventListener<T, U> listener)
{
listeners.Add(listener);
}
public void Subscribe(Action<T, U> listener)
{
EventLogging.Log(LogType.Subscribed, name, "N/A", GetType().Name, $"{listener} subscribed directly to {name}.", null);
listenersA.Add(listener);
}
public void UnSubscribe(IEventListener<T, U> listener)
{
listeners.Remove(listener);
}
public void UnSubscribe(Action<T, U> listener)
{
EventLogging.Log(LogType.UnSubscribed, name, "N/A", GetType().Name, $"{listener} unsubscribed directly to {name}.", null);
listenersA.Remove(listener);
}
}