/// <summary> /// Subscribe all GameObject's components that annotate with MessageHandlerAttribute to the messages /// </summary> /// <param name="messagesHub">Hub on which subscription will be occur. If null then defaul hub (MessagesUtils.Hub) will be used. </param> public void Subscribe(MessagesHub messagesHub = null) { Unsubscribe(); #if !NETFX_CORE var engineAssembly = typeof(UnityEngine.Application).Assembly; #else var engineAssembly = typeof(UnityEngine.Application).GetTypeInfo().Assembly; #endif m_subscriptions = new List <SubscriptionEntry>(); foreach (Component component in GetComponents <Component>()) { if (component == null) { Debug.LogWarningFormat("There is null component while attempt to auto subscribe. GameObject = [{0}]", gameObject.name); continue; } var componentType = component.GetType(); if (componentType == typeof(MessageAutoSubscriber) || #if !NETFX_CORE component.GetType().Assembly == engineAssembly) #else componentType.GetTypeInfo().Assembly == engineAssembly #endif { continue; } var subscriptions = MessagesUtils.AutoSubscribe(component, messagesHub); if (subscriptions != null && subscriptions.Count > 0) { var entry = new SubscriptionEntry() { ComponentName = componentType.Name, MethodSubscriptions = subscriptions }; m_subscriptions.Add(entry); } } }
/// <summary> /// Return <paramref name="hub"/> hub or MessagesHub.RuntimeInstance if null is specified. /// </summary> /// <param name="hub"></param> /// <returns></returns> public static MessagesHub HubOrRuntimeDefault(MessagesHub hub) { return(hub ?? MessagesHub.RuntimeInstance); }
/// <summary> /// Iterate over all methods of the obj and wrap message-handlers methods as subscribers. /// </summary> /// <param name="obj"></param> /// <param name="hostHub"></param> /// <returns>Collections of the subscriptions</returns> /// <exception cref="MessageHandlerSignatureException">If method's signature does not correspond to the set of the message broadcasting parameters.</exception> public static IList <MethodSubscription> AutoSubscribe(object obj, MessagesHub hostHub = null) { if (obj == null) { throw new ArgumentNullException("obj"); } var hub = HubOrRuntimeDefault(hostHub); Type t = obj.GetType(); #if !NETFX_CORE var inspectedMethods = ReflectionUtils.GetAllMethods(t, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); #else var inspectedMethods = from m in t.GetRuntimeMethods() where !m.IsStatic select m ; #endif var subscribedMethods = new List <MethodSubscription>(); foreach (MethodInfo mi in inspectedMethods) { var method = mi; var attribs = ReflectionUtils.GetCustomAttributes <MessageHandlerAttribute>(mi); foreach (var attrib in attribs) { var descriptor = CheckHandlerAndGetDescriptor(obj, attrib.MessageType, mi, attrib); var act = new Action <object[]>(parameters => { var expectedParametersCount = method.GetParameters().Length; if (expectedParametersCount == parameters.Length) { method.Invoke(obj, parameters); } else { var rangeOfParameters = new object[expectedParametersCount]; for (var i = 0; i < expectedParametersCount; ++i) { rangeOfParameters[i] = parameters[i]; } method.Invoke(obj, rangeOfParameters); } }); var subscription = hub.AddSubscription(descriptor, act, obj); subscribedMethods.Add(new MethodSubscription(method.Name, subscription)); } } return(subscribedMethods); }
/// <summary> /// Broadcast parameterless message. /// </summary> /// <param name="hub"></param> public static void Broadcast(MessagesHub hub = null) { MessagesUtils.HubOrRuntimeDefault(hub).Broadcast(Instance()); }
public static void Register(MessagesHub hub) { s_hubs.Add(new WeakReference(hub)); }