Studio GuideWorld SDK Guide
Log In

UnityEvent

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 { 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