1. Zepeto script 참조 방식

내가 구현하고 싶었던 것은, OnResult()로 받은 myRank.score 넘버를 UI의 text에 출력하는 기능이었다.

하지만, OnResult() 안에 프로퍼티가 참조가 되지 않아 고민을 하던 중, OnResult() 안에 text를 출력할 스크립트를 참조시켜 호출하게 하는 방식을 생각하게 되었다.

※ 만약, 다른 객체의 스크립트를 참조하여 그 안에 있는 함수를 호출하고 싶다면, 아래와 같이 작성하면 해결할 수 있다.

(1) 참조하고 싶은 스크립트를 import 한다.

import UI from './UI';

(2) 참조하고 싶은 스크립트 타입으로 객체를 생성한다.

let ui: UI;

(3) 다른 객체를 찾아 UI 스크립트 컴퍼넌트를 참조한다.

let ui: UI = GameObject.Find("UI").GetComponent<UI>();

(4) 그리고 생성한 객체로 함수를 콜한다.

ui.showScore(result.rankInfo.myRank.score);

2. 구현한 코드

[UI.ts]

import { ZepetoScriptBehaviour } from 'ZEPETO.Script'
import { Text } from 'UnityEngine.UI';

export default class UI extends ZepetoScriptBehaviour {

    public txt: Text;
    score: number;

    showScore(score: number) {
        this.txt.text = score.toString();
    }
}

[tressureHit.ts]

import { Collider, GameObject } from 'UnityEngine';
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { LeaderboardAPI, ResetRule } from 'ZEPETO.Script.Leaderboard';
import { GetRangeRankResponse } from 'ZEPETO.Script.Leaderboard';

// UI 스크립트의 함수를 실행하기 위해 클래스를 불러옴
import UI from './UI';

export default class tressureHit extends ZepetoScriptBehaviour {

    public leaderboardId: string;
    public startRank: number;
    public endRank: number;
    public resetRule: ResetRule;
    public score: number;

    Start() {

    }

    OnTriggerEnter(coll: Collider) {
        LeaderboardAPI.SetScore(this.leaderboardId, this.score, this.OnResult, this.OnError);
    }
    
    OnTriggerExit(coll: Collider) {
        LeaderboardAPI.GetRangeRank(this.leaderboardId, this.startRank, this.endRank, this.resetRule, false, this.OnResult, this.OnError);
        
    }

    OnResult(result: GetRangeRankResponse) {
        if (result.rankInfo.myRank) {
            console.log(`member: ${result.rankInfo.myRank.member}, rank: 
            ${result.rankInfo.myRank.rank}, score: ${result.rankInfo.myRank.score}, name: 
            ${result.rankInfo.myRank.name}`);
        }

        // UI 스크립트의 showScore 함수를 호출
        let ui: UI = GameObject.Find("UI").GetComponent<UI>();
        ui.showScore(result.rankInfo.myRank.score);
    }

    OnError(error: string) {
        console.log(error);
    }
}

3. 실행 결과

image-20220804200521729

태그:

카테고리:

업데이트:

댓글남기기