-

UnityEvents - Differentiating Between Multiple Component Instances

16 Jan, 2016Ariel Schoch

UnityEvents offer a convenient way of adding persistent callbacks at design time through the inspector and help keep your scripts modular.
One of the biggest drawbacks however, is that there is no support for picking a specific target instance when a game object has multiple components of the same type.

After searching for solutions and checking the Asset Store for alternatives I decided to write my own implementation.
The fancy visuals of the standard UnityEvent are done using the undocumented ReorderableList class and using a GenericMenu for the component selection.
Simply numbering duplicate components in the dropdown solves the original problem.

To avoid using reflection at runtime I created a QuickEvent class that implements the ISerializationCallbackReceiver, using the OnAfterDeserialize method to take the callback info and create a delegate from it.

A nice article comparing the performance of delegates versus MethodInfo.Invoke () can be found here.

Someone posted this issue over on the Unity Feedback page where you can upvote their suggestion.
My own implementation of the UnityEvent class can be found on the
Asset Store or over on Bitbucket.