-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventListenerBase.cs
96 lines (79 loc) · 3.34 KB
/
EventListenerBase.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
/**
* Description: Base Listens for Messages and routes them via UnityEvents.
* Copyright: © 2017-2020 Kornel, MIT License. Please see 'README.md' and 'LICENSE' files for more information.
**/
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.Events;
public class EventListenerBase<U, M>: MonoBehaviour, IEventListener where U : UnityEvent where M : class, IEvent
{
[SerializeField] private M gameEvent = null;
[SerializeField] private U response = null;
private void Start()
{
Assert.IsNotNull(gameEvent);
}
private void OnEnable()
{
EventLogging.Log(LogType.Subscribed, gameEvent.ToString(), gameObject.name, "N/A", $"Listener on {name} subscribed.", gameObject);
gameEvent.Subscribe(this);
}
private void OnDisable()
{
EventLogging.Log(LogType.UnSubscribed, gameEvent.ToString(), gameObject.name, "N/A", $"Listener on {name} unsubscribed.", gameObject);
gameEvent.UnSubscribe(this);
}
public void OnEventRaised()
{
EventLogging.Log(LogType.Received, gameEvent.ToString(), gameObject.name, "N/A", $"Event received by listener on <b>{name}</b> GameObject. UnityEvents attached: <b>{response.GetPersistentEventCount()}</b>", gameObject);
response.Invoke();
}
}
public class EventListenerBase<U, M, T> : MonoBehaviour, IEventListener<T> where U: UnityEvent<T> where M: class, IEvent<T>
{
[SerializeField] private M gameEvent = null;
[SerializeField] private U response = null;
private void Start( )
{
Assert.IsNotNull( gameEvent );
}
private void OnEnable( )
{
EventLogging.Log( LogType.Subscribed, gameEvent.ToString(), gameObject.name, GetType( ).Name, $"Listener on {name} subscribed.", gameObject);
gameEvent.Subscribe( this );
}
private void OnDisable( )
{
EventLogging.Log( LogType.UnSubscribed, gameEvent.ToString(), gameObject.name, GetType( ).Name, $"Listener on {name} unsubscribed.", gameObject);
gameEvent.UnSubscribe( this );
}
public void OnEventRaised( T parameter )
{
EventLogging.Log( LogType.Received, gameEvent.ToString(), gameObject.name, parameter.ToString(), $"Event received by listener on <b>{name}</b> GameObject. Value passed: <b>{parameter}</b>. UnityEvents attached: <b>{response.GetPersistentEventCount( )}</b>", gameObject);
response.Invoke( parameter );
}
}
public class EventListenerBase<U, M, T, V>: MonoBehaviour, IEventListener<T,V> where U : UnityEvent<T,V> where M : class, IEvent<T,V>
{
[SerializeField] private M gameEvent = null;
[SerializeField] private U response = null;
private void Start()
{
Assert.IsNotNull(gameEvent);
}
private void OnEnable()
{
EventLogging.Log(LogType.Subscribed, gameEvent.ToString(), gameObject.name, $"({typeof(T)}, {typeof(V)})", $"Listener on {name} subscribed.", gameObject);
gameEvent.Subscribe(this);
}
private void OnDisable()
{
EventLogging.Log(LogType.UnSubscribed, gameEvent.ToString(), gameObject.name, $"({typeof(T)}, {typeof(V)})", $"Listener on {name} unsubscribed.", gameObject);
gameEvent.UnSubscribe(this);
}
public void OnEventRaised(T parameter1, V parameter2)
{
EventLogging.Log(LogType.Received, gameEvent.ToString(), gameObject.name, $"({parameter1}, {parameter2})", $"Event received by listener on <b>{name}</b> GameObject. Values passed: <b>{parameter1}</b>, <b>{parameter2}</b>. UnityEvents attached: <b>{response.GetPersistentEventCount()}</b>", gameObject);
response.Invoke(parameter1, parameter2);
}
}