Studio GuideWorld SDK Guide
Log In

ScriptableObject

ZepetoScriptableObject supports Unity ScriptableObject. ZepetoScriptableObject is a data container that saves large amounts of data, such as world data settings and character options.

To create a new ZepetoScriptableObject file, click the [+] icon in the top left corner of the Project panel, or click Assets → Create → ZEPETO → ScriptableObject.


The newly created ZepetoScriptableObject needs to be connected by creating scripts with a defined data class.

585

Make a new ZEPETOScript file, and declare the data you will use as the member variables.

Here are some sample codes that can be used as data.

import { Vector3, Quaternion } from 'UnityEngine';

export default class PlayerScriptObjectDef {  

    public name: string;
    public hp: number;      
    public position: Vector3;
    public rotation: Vector3;
 }

When you link the ZEPETOScript with the defined data class to ZepetoScriptableObjects, you will see those data in the Inspector window, as shown below.


Create a ZEPETOScript file to call data from the ZepetoScriptableObject.

Import the ZepetoScriptableObject from the ZEPETOScript and import the data type you have declared as well.

import { ZepetoScriptableObject } from 'ZEPETO.Script';
import PlayerScriptObjectDef from './playerDefine/PlayerScriptObjectDef';

247

Refer to the script file location


Declare the ZepetoScriptableObject’s as properties in the ZEPETOScript.

public player: ZepetoScriptableObject<PlayerScriptObjectDef>;
public enemy: ZepetoScriptableObject<PlayerScriptObjectDef>;

Open the Inspector window in ZEPETOScript assign values for the ZepetoScriptableObject properties, as shown below.

729

This sample shows how you can use the ZepetoScriptableObject and access it’s values.

// Direct access by name
console.log(`play name : ${this.player["name"]}`);
console.log(`play hp : ${this.player["hp"]}`);      
console.log(`play position : (${this.player["position"].x}, ${this.player["position"].y}, ${this.player["position"].z})`);
console.log(`play rotation : (${this.player["rotation"].x}, ${this.player["rotation"].y}, ${this.player["rotation"].z})`);
 
// Generic data call
let monsterState = this.enemy.targetObject;
console.log(`enemy name : ${monsterState.name}`);
console.log(`enemy hp : ${monsterState.hp}`);
console.log(`enemy position : (${monsterState.position.x}, ${monsterState.position.y}, ${monsterState.position.z})`);
console.log(`enemy rotation : (${monsterState.rotation.x}, ${monsterState.rotation.y}, ${monsterState.rotation.z})`);

To access a primitive type of member variable, declare it as a generic type or covert it using type casting, like the example shown below.

// Type casting
let playerPosition = this.player["position"] as Vector3;
console.log(`player position : (x:${playerPosition.x}, y:${playerPosition.y}, z:${playerPosition.z})`);
  
// Generic type can access class member
console.log(`enemy position : (x:${monsterState.position.x}, y:${monsterState.position.y}, z:${monsterState.position.z})`);

Check the full code example for ScriptableObject.

import { Vector3 } from 'UnityEngine';
import { ZepetoScriptBehaviour, ZepetoScriptableObject } from 'ZEPETO.Script';
// Import scriptable object type definition
import PlayerScriptObjectDef from './playerDefine/PlayerScriptObjectDef'; 

export default class ScriptableObjectSample extends ZepetoScriptBehaviour {

    public player: ZepetoScriptableObject;
    // Generic type call
    public enemy: ZepetoScriptableObject<PlayerScriptObjectDef>; 

    Awake() {
        // Direct access by name
        console.log(`play name : ${this.player["name"]}`);
        console.log(`play hp : ${this.player["hp"]}`);
        console.log(`play position : (${this.player["position"].x}, ${this.player["position"].y}, ${this.player["position"].z})`);
        console.log(`play rotation : (${this.player["rotation"].x}, ${this.player["rotation"].y}, ${this.player["rotation"].z})`);

        // Generic data call
        let monsterState = this.enemy.targetObject;
        console.log(`enemy name : ${monsterState.name}`);
        console.log(`enemy hp : ${monsterState.hp}`);
        console.log(`enemy position : (${monsterState.position.x}, ${monsterState.position.y}, ${monsterState.position.z})`);
        console.log(`enemy rotation : (${monsterState.rotation.x}, ${monsterState.rotation.y}, ${monsterState.rotation.z})`);

        // Type casting
        let playerPosition = this.player["position"] as Vector3;
        console.log(`player position : (${playerPosition.x}, ${playerPosition.y}, ${playerPosition.z})`);

        // Generic type can access class member
        console.log(`enemy position : (${monsterState.position.x}, ${monsterState.position.y}, ${monsterState.position.z})`);
    }
}