I noticed that the reflection for generating methods in the OnClick handler for UIButton only has the flags BindingFlags.Instance | BindingFlags.Public in them. I think I understand why this was done; in order to make things easier for the user. However, I usually find myself making my events private, to properly hide away such things from other classes. Sure, it's not a big deal, but I take my encapsulation in Unity very seriously, going as far as to using SerializeField all the time so I can hide away all those inspector fields from other classes.
So what I came up with was a simple empty attribute. For those who don't care, they can continue making them public. For those who care, they can use the NGUIEvent I created;
[System.AttributeUsageAttribute(System.AttributeTargets.Method)]
public class NGUIEvent : System.Attribute
{
// Used to allow private methods in the Methods dropdown for OnClicks in the inspector
}
I added the BindingFlags.NonPublic flag, then I added this code on line 40 or something;
if (!mi.IsPublic) {
System.Object[] nguiEventMethods
= mi
.GetCustomAttributes(typeof(NGUIEvent
),
false); bool hasAttribute = false;
if (nguiEventMethods.Length > 0) {
foreach (var nguiEventMethod in nguiEventMethods) {
if (nguiEventMethod
.GetType() == typeof(NGUIEvent
)) { hasAttribute = true;
}
}
}
if (!hasAttribute) continue;
}
That would probably look prettier with some link which can be processed on the original mi collection. What do you guys think?
I especially like this because it makes the code even more legible, because now [NGUIEvent] will clearly tell you what a method is being used for, generally.