- Home /
Need to filter UnityEvents based on attributes.
I have made a custom UnityEvent and I would like to invoke the listeners that have a specific attribute(Depending on how many fingers are touching the screen I need or I don't need to invoke that event). I found "UnityEventBase.GetValidMethodInfo" in order to get the method for the current listener but I cannot invoke the method because I do not have the parameters from the editor(eg: if the function receives a string and there is a value set in the editor I cannot access it). Is there any way to do this?
This is a tryout but it doesn't work:
InteractionTouches touchesMask = GetMask (evData.Count);
for (int i = 0; i < this.GetPersistentEventCount (); i++) {
object target = this.GetPersistentTarget (i);
MethodInfo method = this.FindMethod_Impl (this.GetPersistentMethodName (i), target);
object[] attributes = method.GetCustomAttributes (typeof(InteractionTouchAttribute), true);
if (attributes.Length == 0)
method.Invoke (target, new object[]{ evData });
else if ((((InteractionTouchAttribute)attributes [0]).AllowedInteraction & touchesMask) == touchesMask)
method.Invoke (target, new object[]{ evData });
}
In this case "this.FindMethod_Impl" only returns the method that has the header with the parameter that is generic in the UnityEvent. I replaced "this.FindMethod_Impl" with "UnityEventBase.GetValidMethodInfo" but as I mentioned before I don't have the arguments so it's completely useless. I just need to take control of whether a listener is being called or not. Nothing more.
Cheers, Gamar
Your answer
Follow this Question
Related Questions
Regarding Update() Performance 0 Answers
Casting methods, events, and/or delegates from strings / enum 0 Answers
Reflection on android platform 0 Answers
how to make things be invisible but cast information to reflection probe? 0 Answers
What is the best practice for using multiple reflection probes 0 Answers