public void Broadcast(MessageBase message, Type receiver) { Component[] behaviours = GetComponentsInChildren(receiver); foreach (BasicMessageDispatcher behaviour in behaviours) { behaviour.DispatchMessage(message.GetName(), message); } }
public void Broadcast(MessageBase message) { BasicMessageDispatcher[] behaviours = GetComponentsInChildren <BasicMessageDispatcher>(); foreach (BasicMessageDispatcher behaviour in behaviours) { behaviour.DispatchMessage(message.GetName(), message); } }
public void BroadcastWithTag(MessageBase message, string tag) { BasicMessageDispatcher[] behaviours = GetComponentsInChildren <BasicMessageDispatcher>(); foreach (BasicMessageDispatcher behaviour in behaviours) { if (behaviour.tag == tag) { behaviour.DispatchMessage(message.GetName(), message); } } }
/// <summary> /// Registers this page to listen for the provided message types. /// </summary> /// <param name="messageTypes">The types to listen for.</param> protected void BootstrapMessageSubscriptions(params Type[] messageTypes) { foreach (Type t in messageTypes) { string name = MessageBase.GetName(t); MethodInfo method = GetType().GetTypeInfo().GetDeclaredMethod($"Handle{name}"); DebugHelper.Assert(method != null, $"Handler for message {name} should be declared"); ParameterInfo[] parameters = method.GetParameters(); DebugHelper.Assert(parameters.Length == 1, "Message handlers must take one parameter"); DebugHelper.Assert(parameters[0].ParameterType == t, "Message handler parameter type must match expected message type"); DebugHelper.Assert(method.ReturnType.Equals(typeof(Task)), "Message handles must return a task"); DebugHelper.Assert(!this.messageSubscriptions.ContainsKey(name)); this.messageSubscriptions[name] = method; MessageBus.Subscribe(name, this); } }