Game Development Reference
In-Depth Information
The
UnityEvent
is an extension of the base delegate pattern that is found
in most programming languages, obviously now tuned to work better
from within Unity's own engine and language interpreter.
For more detail about delegates, see:
Delegate tutorial
Starting simple, we'll create a script that will output to the console to show the event
has occurred. Create a new
c#
script called
SimpleEvent.cs
and replace its contents
with the following:
For standards stake, you should add scripts into a folder called
Scripts
and scenes into a folder called
Scenes
. It's up to you; this isn't mandatory
or anything.
using UnityEngine;
using UnityEngine.Events;
public class SimpleEvent : MonoBehaviour {
//My UnityEvent Manager
public UnityEvent myUnityEvent = new UnityEvent();
void Start () {
//Subscribe my delegate to my UnityEvent Manager
myUnityEvent.AddListener(MyAwesomeDelegate);
//Execute all registered delegates
myUnityEvent.Invoke();
}
//My Delegate function
private void MyAwesomeDelegate()
{
Debug.Log("My Awesome UnityEvent lives");
}
}