Unity provides UnityEvents to make it easier for users to use C# events and delegates.
UnityEvents are a way of delivering arguments from different objects or passing on a message when certain requirements are met.
To use UnityEvent in ZEPETOScript, you need to declare it in the following format.
First, add an import command as shown below to use the UnityEvent class.
import { UnityEvent } from 'UnityEngine.Events';
See a sample coupling code for a UnityEvent.
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { UnityEvent } from 'UnityEngine.Events';
import { Button } from 'UnityEngine.UI';
export default class Event extends ZepetoScriptBehaviour {
public btn : Button;
private mEvent: UnityEvent;
Start() {
// Creates a new instance of UnityEvent and assigns it to mEvent.
this.mEvent = new UnityEvent();
// Adds a new listener to mEvent. This listener executes the Ping method when mEvent is called.
this.mEvent.AddListener(() => this.Ping());
// Checks if a button is clicked and mEvent is not null.
this.btn.onClick.AddListener(() => {
if (this.mEvent != null) {
// If the above condition is true, invokes mEvent.
this.mEvent.Invoke();
}
});
}
Ping() {
console.log('Ping');
}
}
- Script Description
- In the example above, a UnityEvent named ‘mEvent’ is invoked each time a button is pressed, and it executes the ‘Ping’ method when ‘mEvent’ is called.
- Therefore, each time the button is pressed, a message 'Ping' is printed to the console.
If the event that you are about to connect has parameters, you need to add a UnityEvent import command as shown below.
import { UnityEvent$1 } from 'UnityEngine.Events';
Tips
- UnityEvent$1 is the generic version of UnityEvent
- For instance, if you want to create an event that accepts an integer value, you can use
UnityEvent$1<int>
- Please note that the parameter type of UnityEvent$1 follows C#
A sample code when using UnityEvent$1 with parameters is as follows.
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { UnityEvent$1 } from 'UnityEngine.Events';
import { Button } from 'UnityEngine.UI';
export default class Event extends ZepetoScriptBehaviour {
public btn: Button;
// Stores an instance of UnityEvent with an int parameter type.
private mEventInt: UnityEvent$1<int>;
private countNum: number;
Start() {
// Creates a new instance of UnityEvent with an int parameter type.
this.mEventInt = new UnityEvent$1<int>();
// Adds a listener to mEventInt that calls the Count method with an int parameter when mEventInt is invoked.
this.mEventInt.AddListener(num => this.Count(num));
this.countNum = 0;
// Checks if a button is clicked and mEvent is not null.
this.btn.onClick.AddListener(() => {
// On button click, invokes the mEventInt event, passing countNum as the parameter.
if (this.mEventInt != null) {
this.mEventInt.Invoke(this.countNum);
++this.countNum;
if (this.countNum > 10) {
this.countNum = 0;
}
}
});
}
Count(num) {
console.log(`Count : ${num}`);
}
}
- Script Description
- In the example above, ‘mEventInt’ is invoked each time a button is pressed, passing ‘countNum’ as a parameter.
- When ‘mEventInt’ is called, it executes the ‘Count(num)’ method. It passes the current value of ‘countNum’ to the ‘Count’ method to be printed to the console, and then increments the value of ‘countNum’ by 1. If ‘countNum’ exceeds 10, it is reset back to 0.
You can learn more about UnityEvents by clicking the link below.
UnityEvents
https://docs.unity3d.com/kr/530/ScriptReference/Events.UnityEvent.html