├── Assets ├── Scene.meta ├── Scene │ ├── example.unity │ └── example.unity.meta ├── Scripts.meta └── Scripts │ ├── Example.meta │ ├── Svelto.meta │ ├── Svelto │ ├── Communication.meta │ └── Communication │ │ ├── SignalChain.meta │ │ └── SignalChain │ │ ├── IChainRoot.cs │ │ ├── IChainListener.cs │ │ ├── BubbleSignal.cs.meta │ │ ├── IChainRoot.cs.meta │ │ ├── SignalChain.cs.meta │ │ ├── IChainListener.cs.meta │ │ ├── BubbleSignal.cs │ │ └── SignalChain.cs │ └── Example │ ├── Cube.cs │ ├── Capsule.cs.meta │ ├── Cube.cs.meta │ ├── Sphere.cs.meta │ ├── BetterEvent.cs.meta │ ├── CubeAlone.cs.meta │ ├── Cylinder.cs.meta │ ├── BetterEvent.cs │ ├── Sphere.cs │ ├── Cylinder.cs │ ├── Capsule.cs │ └── CubeAlone.cs ├── README.md └── .gitignore /Assets/Scene.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0d3db1c2ad133be4eafd7417536bdad9 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scene/example.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebas77/Svelto.SignalChain.Example/HEAD/Assets/Scene/example.unity -------------------------------------------------------------------------------- /Assets/Scripts.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4476020fa944177499cb17632d02c56d 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts/Example.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c985aa9e41c00c84686fc926ba83fd40 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 60e081e99fe73054c906a474e603fc08 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scene/example.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 12ba43db10d82f34a808194b4978c4ec 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e06ed7972be9f4649a496dd8043df361 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Cube.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using Svelto.Communication.SignalChain; 3 | 4 | public class Cube : MonoBehaviour 5 | { 6 | } 7 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7b7e1d4b4a6fe234a890507e40a69943 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/IChainRoot.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Svelto.Communication.SignalChain 4 | { 5 | public interface IChainRoot 6 | {} 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Capsule.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1129d7590807b674aa7fc667f19f423b 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Cube.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b3be22c27c5f0c3448fcd292f95ebbc5 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Sphere.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 990990a8db7d728429803235ae2664b5 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/IChainListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Svelto.Communication.SignalChain 4 | { 5 | public interface IChainListener 6 | { 7 | void Listen(T message); 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/BetterEvent.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7ef56f8534daece42b00bd2d3de37039 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/CubeAlone.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ff02bd5cd44078b4c9ba910ba08b2a9f 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Cylinder.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fb8153d02f765a8498e46a52a9ea6069 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/BetterEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | public class BetterEvent 5 | { 6 | public Color color { get; private set; } 7 | 8 | public BetterEvent (Color value) 9 | { 10 | color = value; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/BubbleSignal.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cc23aeba6f3265c43a8e5fb25895409d 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/IChainRoot.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 576d5947f82b7434a925ae4a44d6386b 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/SignalChain.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a2603f5fa5a2acc4280152f3513363e5 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/IChainListener.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5f17be73145077c4488a11ac70985156 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SignalChain for Unity3D 2 | ======================== 3 | 4 | a better alternative to the Unity3D SendMessage and BroadcastMessage functions, more info can be found here: http://blog.sebaslab.com/whats-wrong-with-sendmessage-and-broadcastmessage-and-what-to-do-about-it/ 5 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Sphere.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using Svelto.Communication.SignalChain; 4 | 5 | public class Sphere : MonoBehaviour, IChainListener 6 | { 7 | public void Listen(T message) 8 | { 9 | if (message is BetterEvent) 10 | { 11 | this.transform.renderer.material.color = (message as BetterEvent).color; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Cylinder.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using Svelto.Communication.SignalChain; 4 | 5 | public class Cylinder : MonoBehaviour, IChainListener 6 | { 7 | public void Listen(T message) 8 | { 9 | if (message is string && (message as string) == "event") 10 | { 11 | this.transform.renderer.material.color = Color.red; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/Capsule.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using Svelto.Communication.SignalChain; 3 | 4 | public class Capsule : MonoBehaviour 5 | { 6 | BubbleSignal _bubbleSignal; 7 | 8 | void Awake() 9 | { 10 | _bubbleSignal = new BubbleSignal(this.transform); 11 | } 12 | 13 | void OnMouseDown() 14 | { 15 | _bubbleSignal.Dispatch(new BetterEvent(Color.blue)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Assets/Scripts/Example/CubeAlone.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using Svelto.Communication.SignalChain; 3 | 4 | public class CubeAlone : MonoBehaviour 5 | { 6 | public Transform root; 7 | SignalChain _chain; 8 | 9 | void Awake() 10 | { 11 | _chain = new SignalChain(root); 12 | } 13 | 14 | void OnMouseDown() 15 | { 16 | _chain.Broadcast("event"); 17 | 18 | _chain.Broadcast(new BetterEvent(Color.green)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /Assembly-CSharp-vs.csproj 2 | /Assembly-CSharp.csproj 3 | /Assembly-CSharp.pidb 4 | 5 | /Library/* 6 | 7 | /Temp/* 8 | /SignalChain-csharp.sln 9 | /SignalChain.sln 10 | /SignalChain.userprefs 11 | 12 | /ProjectSettings/AudioManager.asset 13 | /ProjectSettings/DynamicsManager.asset 14 | /ProjectSettings/EditorBuildSettings.asset 15 | /ProjectSettings/EditorSettings.asset 16 | /ProjectSettings/InputManager.asset 17 | /ProjectSettings/NavMeshLayers.asset 18 | /ProjectSettings/NetworkManager.asset 19 | /ProjectSettings/ProjectSettings.asset 20 | /ProjectSettings/QualitySettings.asset 21 | /ProjectSettings/TagManager.asset 22 | /ProjectSettings/TimeManager.asset 23 | 24 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/BubbleSignal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace Svelto.Communication.SignalChain 5 | { 6 | public class BubbleSignal 7 | { 8 | SignalChain _signalChain; 9 | 10 | public BubbleSignal (Transform node) 11 | { 12 | Transform root = node; 13 | 14 | while (root != null && RootIsFound(root) == false) 15 | root = root.parent; 16 | 17 | _signalChain = new SignalChain(root == null ? node : root); 18 | } 19 | 20 | // The notification of type M is broadcasted 21 | // to all the active children of type IChainListener 22 | 23 | public void Dispatch() 24 | { 25 | _signalChain.Broadcast(); 26 | } 27 | 28 | // The notification of type M is broadcasted 29 | // to all the active children of type IChainListener 30 | 31 | public void Dispatch(M notification) 32 | { 33 | _signalChain.Broadcast(notification); 34 | } 35 | 36 | // The notification of type M is broadcasted 37 | // to all the children of type IChainListener 38 | 39 | public void DeepDispatch() 40 | { 41 | _signalChain.DeepBroadcast(); 42 | } 43 | 44 | // The notification of type M is broadcasted 45 | // to all the children of type IChainListener 46 | 47 | public void DeepDispatch(M notification) 48 | { 49 | _signalChain.DeepBroadcast(notification); 50 | } 51 | 52 | // The notification of type T is sent 53 | // to the root components of type IChainListener 54 | 55 | public void TargetedDispatch() 56 | { 57 | _signalChain.Send(); 58 | } 59 | 60 | // The notification of type T is sent 61 | // to the root components of type IChainListener 62 | 63 | public void TargetedDispatch(M notification) 64 | { 65 | _signalChain.Send(notification); 66 | } 67 | 68 | private bool RootIsFound(Transform node) 69 | { 70 | return Array.FindIndex(node.GetComponents(), obj => { 71 | return obj is T; 72 | }) != -1; 73 | } 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /Assets/Scripts/Svelto/Communication/SignalChain/SignalChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | namespace Svelto.Communication.SignalChain 5 | { 6 | public class SignalChain 7 | { 8 | private Transform _root; 9 | 10 | public SignalChain (Transform root) 11 | { 12 | _root = root; 13 | } 14 | 15 | public void Send(T notification) 16 | { 17 | Send(typeof(T), notification); 18 | } 19 | 20 | public void Send() 21 | { 22 | Send(typeof(T)); 23 | } 24 | 25 | void Send(Type notification) 26 | { 27 | Send(notification, null); 28 | } 29 | 30 | public void Send(Type notificationType, object notification) 31 | { 32 | MonoBehaviour[] behaviours = _root.GetComponents(); 33 | 34 | foreach (MonoBehaviour behaviour in behaviours) 35 | if (behaviour is IChainListener) 36 | (behaviour as IChainListener).Listen(notification != null ? notification : notificationType); 37 | } 38 | 39 | public void Broadcast() 40 | { 41 | Broadcast(typeof(T)); 42 | } 43 | 44 | public void Broadcast(T notification) 45 | { 46 | Broadcast(typeof(T), notification, false); 47 | } 48 | 49 | void Broadcast(Type notification) 50 | { 51 | Broadcast(notification, null, false); 52 | } 53 | 54 | public void Broadcast(T notification, bool notifyDisabled) 55 | { 56 | Broadcast(typeof(T), notification, notifyDisabled); 57 | } 58 | 59 | public void Broadcast(Type notification, bool notifyDisabled) 60 | { 61 | Broadcast(notification, null, notifyDisabled); 62 | } 63 | 64 | private void Broadcast(Type notificationType, object notification, bool notifyDisabled) 65 | { 66 | foreach (MonoBehaviour behaviour in _root.GetComponentsInChildren(notifyDisabled)) 67 | if (behaviour is IChainListener) 68 | (behaviour as IChainListener).Listen(notification != null ? notification : notificationType); 69 | } 70 | 71 | public void DeepBroadcast() 72 | { 73 | DeepBroadcast(typeof(T)); 74 | } 75 | 76 | public void DeepBroadcast(T notification) 77 | { 78 | DeepBroadcast(typeof(T), notification); 79 | } 80 | 81 | void DeepBroadcast(Type notification) 82 | { 83 | DeepBroadcast(notification, null); 84 | } 85 | 86 | private void DeepBroadcast(Type notificationType, object notification) 87 | { 88 | Broadcast(notificationType, notification, true); 89 | } 90 | } 91 | } 92 | 93 | --------------------------------------------------------------------------------