-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathEventSystem.cs
More file actions
75 lines (62 loc) · 2.15 KB
/
EventSystem.cs
File metadata and controls
75 lines (62 loc) · 2.15 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EventCallbacks
{
public class EventSystem : MonoBehaviour
{
// Use this for initialization
void OnEnable()
{
__Current = this;
}
static private EventSystem __Current;
static public EventSystem Current
{
get
{
if(__Current == null)
{
__Current = GameObject.FindObjectOfType<EventSystem>();
}
return __Current;
}
}
Dictionary<System.Type, dynamic> eventListeners;
public void RegisterListener<T>(System.Action<T> listener) where T : EventInfo
{
System.Type eventType = typeof(T);
if (eventListeners == null)
{
eventListeners = new Dictionary<System.Type, dynamic>();
}
if(eventListeners.ContainsKey(eventType) == false || eventListeners[eventType] == null)
{
eventListeners[eventType] = new List<System.Action<T>>();
}
// Wrap a type converstion around the event listener
// I'm betting someone better at C# generic syntax
// can find a way around this.
//EventListener wrapper = (ei) => { listener((T)ei); };
//eventListeners[eventType].Add(wrapper);
eventListeners[eventType].Add(listener);
}
public void UnregisterListener<T>(System.Action<T> listener) where T : EventInfo
{
// TODO
}
public void FireEvent<T>(T eventInfo) where T : EventInfo
{
System.Type trueEventInfoClass = typeof(T);
if (eventListeners == null || eventListeners[trueEventInfoClass] == null)
{
// No one is listening, we are done.
return;
}
foreach(System.Action<T> el in eventListeners[trueEventInfoClass])
{
el( eventInfo );
}
}
}
}