Created
March 18, 2016 00:49
-
-
Save Delamare2112/a505140f0308aeb28e95 to your computer and use it in GitHub Desktop.
Like SendMessage but with as many aparams as you want!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Reflection; | |
using System.Linq; | |
public static class Extensions | |
{ | |
public static void BetterSendMessage(this Component obj, string function, params object[] args) | |
{ | |
MessageComponenets(obj.GetComponents<Component>(), function, args); | |
} | |
public static void BetterBroadcastMessage(this Component obj, string function, params object[] args) | |
{ | |
MessageComponenets(obj.GetComponentsInChildren<Component>(), function, args); | |
} | |
public static void BetterSendMessageUpwards(this Component obj, string function, params object[] args) | |
{ | |
MessageComponenets(obj.GetComponentsInParent<Component>(), function, args); | |
} | |
private static void MessageComponenets(Component[] components, string function, params object[] args) | |
{ | |
foreach (Component c in components) | |
{ | |
MethodInfo methodInfo = c.GetType().GetMethods().SingleOrDefault<MethodInfo>(x => x.Name == function && x.GetParameters().Length == args.Length); | |
if (methodInfo != null) | |
methodInfo.Invoke(c, args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment