Studio GuideWorld SDK Guide
Log In

Event Functions

The Unity game engine offers regular update events that you can add logic to the lifecycle (creation, updates in every frame, termination) or the events (UI events, collision detection) of an object (or GameObject) within the script.

Initialization events

You can use an initialization event to call the initialization code before an update.

The Start function is called before any physics update and before the first frame, while the Awake function is called for each object in the scene when calling up a scene.

Start and Awake functions for various objects are called in random order, but all Awake calls are finished before Start is called.

This means that the Start function can use other initializations previously carried out in the Awake function.


Regular update events

Regular updates refers to all updates called every frame.

They change the position, state, and behavior of objects before each frame is rendered.

ZEPETOScript provides a guideline for processing the computation in the Update function.

Physics events

Physics events call event functions through a script added in the GameObject.

The OnCollisionEnter, OnCollisionStay, and OnCollisionExit functions are called as contact is made, held, and broken.

The OnTriggerEnter, OnTriggerStay and OnTriggerExit functions are called when an object collider is configured as a trigger (i.e. a collider that does not react physically to collisions but simply detects them instead).

Such functions may be called several times in succession if multiple contacts are detected during the physics update, and thus parameters are sent to the function that provides details of the collision (position, identity of the object).

GUI events

ZEPETOScript supports events that render GUI controls and reacts to clicks from the elements. They work differently from normal frame updates, so UI related logic should be placed in the OnGUI function.

Termination Events

Termination events are called in all activated objects in a scene.

The OnDisable function is called when a given object is disabled or is inactive, and OnApplicationQuit is called on all GameObjects before the application closes.

All the event functions inherently supported by ZEPETOScript are listed in the sample code below.

import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { Collider, Collider2D, Collision, Collision2D } from 'UnityEngine';

export default class SampleMonoBehaviourLifeCycle extends ZepetoScriptBehaviour {

    Awake() {
        console.log('Awake.');
    }
    OnEnable() {
        console.log('OnEnable.');
    }
    Start() {
        console.log('Start.');
    }

    Update() {
        console.log('OnUpdate.');
    }
    FixedUpdate() {
        console.log('FixedUpdate.');
    }
    LateUpdate() {
        console.log('LateUpdate.');
    }

    OnDisable() {
        console.log('OnDisable.');
    }
    OnDestroy() {
        console.log('OnDestroy.');
    }

    OnTriggerEnter(coll: Collider) {
        console.log(`OnTriggerEnter ${coll.gameObject.name}.`);
    }
    OnTriggerExit(coll: Collider) {
        console.log(`OnTriggerExit ${coll.gameObject.name}.`);
    }
    OnTriggerStay(coll: Collider) {
        console.log(`OnTriggerStay ${coll.gameObject.name}.`);
    }

    OnTriggerEnter2D(coll: Collider2D) {
        console.log(`OnTriggerEnter2D ${coll.gameObject.name}.`);
    }
    OnTriggerExit2D(coll: Collider2D) {
        console.log(`OnTriggerExit2D ${coll.gameObject.name}.`);
    }
    OnTriggerStay2D(coll: Collider2D) {
        console.log(`OnTriggerStay2D ${coll.gameObject.name}.`);
    }

    OnCollisionEnter(coll: Collision) {
        console.log(`OnCollisionEnter ${coll.gameObject.name}.`);
    }
    OnCollisionExit(coll: Collision) {
        console.log(`OnCollisionExit ${coll.gameObject.name}.`);
    }
    OnCollisionStay(coll: Collision) {
        console.log(`OnCollisionStay ${coll.gameObject.name}.`);
    }

    OnCollisionEnter2D(coll: Collision2D) {
        console.log(`OnCollisionEnter2D ${coll.gameObject.name}.`);
    }
    OnCollisionExit2D(coll: Collision2D) {
        console.log(`OnCollisionExit2D ${coll.gameObject.name}.`);
    }
    OnCollisionStay2D(coll: Collision2D) {
        console.log(`OnCollisionStay2D ${coll.gameObject.name}.`);
    }

    OnGUI() {
        console.log('OnGUI.');
    }
    OnMouseDown() {
        console.log('OnMouseDown.');
    }
    OnMouseDrag() {
        console.log('OnMouseDrag.');
    }
    OnMouseUp() {
        console.log('OnMouseUp.');
    }
    OnMouseEnter() {
        console.log('OnMouseEnter.');
    }
    OnMouseExit() {
        console.log('OnMouseExit.');
    }
    OnMouseOver() {
        console.log('OnMouseOver.');
    }
    OnMouseUpAsButton() {
        console.log('OnMouseUpAsButton.');
    }

    OnAnimatorIK(layerIndex: number) {
        console.log(`OnAnimatorIK ${layerIndex}.`);
    }

    OnApplicationFocus() {
        console.log('OnApplicationFocus.');
    }
    OnApplicationPause() {
        console.log('OnApplicationPause.');
    }
    OnApplicationQuit() {
        console.log('OnApplicationQuit.');
    }

}

Check out the test code output screen below.

1177

You can learn more about the Unity event functions that ZEPETOScript offers by clicking the links below.

📘

Unity Event Functions

https://docs.unity3d.com/kr/current/Manual/EventFunctions.html

📘

Order of Execution for Event Functions

https://docs.unity3d.com/kr/current/Manual/ExecutionOrder.html