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 { Input } from 'UnityEngine';
export default class Event extends ZepetoScriptBehaviour {
private mEvent: UnityEvent;
Start() {
this.mEvent = new UnityEvent();
this.mEvent.AddListener(() => this.Ping());
}
Update() {
if ((Input.anyKeyDown) && (this.mEvent != null)) {
this.mEvent.Invoke();
}
}
Ping() {
console.log('Ping');
}
}
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';
A sample code when using UnityEvent$1 with parameters is as follows.
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { UnityEvent$1 } from 'UnityEngine.Events';
import { Input } from 'UnityEngine';
export default class Event extends ZepetoScriptBehaviour {
private mEventInt: UnityEvent$1<int>;
private countNum: number;
Start() {
this.mEventInt = new UnityEvent$1<int>();
this.mEventInt.AddListener(num => this.Count(num));
this.countNum = 0;
}
Update() {
if ((Input.anyKeyDown) && (this.mEventInt != null)) {
this.mEventInt.Invoke(this.countNum);
++this.countNum;
if (this.countNum > 100) {
this.countNum = 0;
}
}
}
Count(num) {
console.log(`Count : ${num}`);
}
}
You can learn more about UnityEvents by clicking the link below.
UnityEvents
https://docs.unity3d.com/kr/530/ScriptReference/Events.UnityEvent.html