UnityEvents are a way of allowing user driven callback to be persisted from edit time to run time without the need for additional programming and script configuration.
UnityEvents are useful for a number of things:
Content driven callbacks
Decoupling systems
Persistent callbacks
Preconfigured call events
UnityEvent
s can be added to any MonoBehaviour
and are executed from code like a standard .net delegate. When a UnityEvent
is added to a MonoBehaviour
it appears in the InspectorA Unity window that displays information about the currently selected GameObject, Asset or Project Settings, alowing you to inspect and edit the values. More info
See in Glossary and persistent callbacks can be added.
UnityEvent
s have similar limitations to standard delegates. That is, they hold references to the element that is the target and this stops the target being garbage collected. If you have a UnityEngine.Object as the target and the native representation disappears the callback will not be invoked.
To configure a callback in the editor there are a few steps to take:
Make sure your script imports/uses UnityEngine.Events
.
Select the + icon to add a slot for a callback
Select the UnityEngine.Object you wish to receive the callback (You can use the object selector for this)
Select the function you wish to be called
You can add more then one callback for the event
When configuring a UnityEvent
in the Inspector there are two types of function calls that are supported:
By default a UnityEvent
in a Monobehaviour
binds dynamically to a void function. This does not have to be the case as dynamic invocation of UnityEvents supports binding to functions with up to 4 arguments. To do this you need to define a custom UnityEvent
class that supports multiple arguments. This is quite easy to do:
[Serializable]
public class StringEvent : UnityEvent <string> {}
By adding an instance of this to your class instead of the base UnityEvent
it will allow the callback to bind dynamically to string functions.
This can then be invoked by calling the Invoke()
function with a string
as argument.
UnityEvents can be defined with up to 4 arguments in their generic definition.
Did you find this page useful? Please give it a rating: