Studio GuideWorld SDK Guide
Log In

Event Functions

Event Functions are special functions automatically called during the Life Cycle of a GameObject or component in the Unity engine, Therefore, they are also referred to as Lifecycle Event Functions.

Event Functions are automatically called by the Unity engine at specific timings.

Event Functions are special functions automatically called during the Life Cycle of a GameObject or component in the Unity engine, hence they are also referred to as Lifecycle Event Functions.

ZEPETO Script Lifecycle Flowchart

ZEPETO Script Lifecycle Flowchart


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.

Mouse Events

  • In ZEPETOScript, events that respond to mouse interactions are supported.
  • However, mouse events require a Collider or Collider2D to be present on the relevant GameObject, and they are only triggered when that Collider is clicked with the mouse.
  • OnMouseDown() is called when the user clicks the mouse button.
  • OnMouseUp() is called when the user releases the mouse button.
  • OnMouseOver() is called every frame while the mouse cursor is over the Collider or Collider2D of the GameObject.

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.

📘

Unity Event Functions

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


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.

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

📘

Order of Execution for Event Functions

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