CREATE YOUR WORLD
Scripting

UnityEvent

4min
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 typescript import { unityevent } from 'unityengine events'; see a sample coupling code for a unityevent event 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 typescript 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 event 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 https //docs unity3d com/kr/530/scriptreference/events unityevent html