-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignalChain.cs
103 lines (89 loc) · 3.44 KB
/
SignalChain.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
#if UNITY_5 || UNITY_5_3_OR_NEWER
using System;
using UnityEngine;
namespace Svelto.UI.Comms.SignalChain
{
public class SignalChain
{
public SignalChain(Transform root)
{
_root = root;
_behaviours = _root.GetComponents<MonoBehaviour>();
_hierarchicalBehaviours = _root.GetComponentsInChildren<MonoBehaviour>(true);
}
public void Send<T>(T notification)
{
Send(typeof(T), notification);
}
public void Send<T>()
{
Send(typeof(T), null);
}
/// <summary>
/// Send an event to all the components of the root. If a notification object
/// is passed, events must expect a value object.
/// </summary>
/// <param name="notificationType">Event is described by a type</param>
/// <param name="notification">Event could be a value object</param>
public void Send(Type notificationType, object notification)
{
for (var i = 0; i < _behaviours.Length; i++)
{
var behaviour = _behaviours[i];
if (behaviour is IChainListener)
(behaviour as IChainListener).Listen(notification ?? notificationType);
}
}
public void Broadcast<T>()
{
Broadcast(typeof(T), null);
}
public void Broadcast<T>(T notification)
{
Broadcast(typeof(T), notification, false);
}
/// <summary>
/// Broadcast an event to all the components of the root and root children.
/// If a notification object is passed, events must expect a value object.
/// </summary>
/// <param name="notificationType">Event is described by a type</param>
/// <param name="notification">Event could be a value object</param>
public void Broadcast(Type notificationType, object notification)
{
Broadcast(notificationType, notification, false);
}
public void DeepBroadcast<T>()
{
DeepBroadcast(typeof(T), null);
}
public void DeepBroadcast<T>(T notification)
{
DeepBroadcast(typeof(T), notification);
}
/// <summary>
/// Broadcast an event to all the components of the root and root children,
/// even if the children are disabled.
/// If a notification object is passed, events must expect a value object.
/// </summary>
/// <param name="notificationType">Event is described by a type</param>
/// <param name="notification">Event could be a value object</param>
public void DeepBroadcast(Type notificationType, object notification)
{
Broadcast(notificationType, notification, true);
}
void Broadcast(Type notificationType, object notification, bool notifyDisabled)
{
for (var i = 0; i < _hierarchicalBehaviours.Length; i++)
{
var behaviour = _hierarchicalBehaviours[i];
if (behaviour is IChainListener && (notifyDisabled == true ||
(notifyDisabled == false && behaviour.gameObject.activeInHierarchy == true)))
(behaviour as IChainListener).Listen(notification ?? notificationType);
}
}
Transform _root;
MonoBehaviour[] _behaviours;
MonoBehaviour[] _hierarchicalBehaviours;
}
}
#endif