Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.