Skip to content

Instantly share code, notes, and snippets.

@Delamare2112
Created March 18, 2016 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Delamare2112/a505140f0308aeb28e95 to your computer and use it in GitHub Desktop.
Save Delamare2112/a505140f0308aeb28e95 to your computer and use it in GitHub Desktop.
Like SendMessage but with as many aparams as you want!
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