あなたの世界を作りなさい
経済
製品 API 使用例
18分
以下は、製品apiを使用して世界通貨と世界製品を管理する例です。 この例では、製品apiの基本的な理解を得て、サーバーとクライアント間でメッセージを送受信する実装を行うことができるため、世界通貨と製品の付与および控除を簡単にテストできます。 この例には、世界通貨と世界製品のクライアントスクリプトの説明が含まれており、その後、すべてのサーバーサイド処理を処理する統合サーバースクリプトが続き、以下の機能が含まれています: 世界通貨の付与 世界通貨の控除 通貨控除なしでの世界製品の付与 世界製品の控除 世界通貨の付与と控除 以下は、世界通貨の付与と控除を管理するための完全なクライアントコードと、その実装方法です。 通貨クライアントスクリプト currencymanagersample import { button, text } from 'unityengine ui'; import { zepetoscriptbehaviour } from 'zepeto script' import { localplayer, zepetoplayers } from 'zepeto character controller'; import { room, roomdata } from 'zepeto multiplay'; import { zepetoworldmultiplay } from 'zepeto world'; export default class currencymanagersample extends zepetoscriptbehaviour { public currencyid string; public increasequantity number; public decreasequantity number; public increasecurrencybtn button; public decreasecurrencybtn button; public balancetext text; public multiplay zepetoworldmultiplay; private localplayer localplayer; start() { zepetoplayers instance onaddedlocalplayer addlistener(() => { this localplayer = zepetoplayers instance localplayer; this loadcurrencybalance(this currencyid); }); // 通貨uiの更新のためのルーム参加イベントを処理します this multiplay roomjoined += (room room) => { room addmessagehandler("synccurrencyinfo", (message currencymessage) => { const currentcurrencyid = message currencyid; const currentquantity = message quantity; this balanceuiupdate(currentcurrencyid, currentquantity); }); }; // 通貨を増やすためのボタンイベントリスナー クレジットリクエスト this increasecurrencybtn onclick addlistener(() => { const data = new roomdata(); data add("currencyid", this currencyid); data add("quantity", this increasequantity); this multiplay room? send("creditcurrency", data getobject()); }) // 通貨を減らすためのボタンイベントリスナー デビットリクエスト this decreasecurrencybtn onclick addlistener(() => { const data = new roomdata(); data add("currencyid", this currencyid); data add("quantity", this decreasequantity); this multiplay room? send("debitcurrency", data getobject()); }) } // 指定された通貨の現在の量をリクエストします private loadcurrencybalance(currentcurrencyid string) { this multiplay room? send("loadbalance", currentcurrencyid); } // 通貨の現在の量を反映するようにuiを更新します private balanceuiupdate(currentcurrencyid string, currentquantity number) { this balancetext text = currentquantity tostring(); } } // 通貨同期メッセージのインターフェース interface currencymessage { currencyid string, quantity number, } 通貨クライアントスクリプトの説明 キャラクターがロードされると、サーバーにルームメッセージが送信され、既存の通貨の残高がロードされます。その後、サーバーから受信した残高情報に基づいてuiが更新されます。 this multiplay room? send("loadbalance", currentcurrencyid); メッセージ交換を促進するために、 currencymessage インターフェースが定義されています。 interface currencymessage { currencyid string, quantity number, } ユーザーが通貨を増減させると、彼らはサーバーにルームメッセージとしてリクエストを送信します。 それには、どの通貨を増減させるか、そしてどのくらいの量を増減させるかのデータが含まれています。 this multiplay room? send("creditcurrency", data getobject()); this multiplay room? send("debitcurrency", data getobject()); サーバーは通貨の増減を処理し、最終的な残高情報をクライアントに送信します。クライアントはこの情報を受け取り、uiを更新します。 製品 api 使用例 docid\ t4tujd mngihomx0dpyeq this multiplay roomjoined += (room room) => { room addmessagehandler("synccurrencyinfo", (message currencymessage) => { const currentcurrencyid = message currencyid; const currentquantity = message quantity; this updatebalanceui(currentcurrencyid, currentquantity); }); }; 世界の製品の付与と控除 以下は、世界の製品の付与と控除を管理するための完全なクライアントコードと、その実装方法です。 製品クライアントスクリプト productmanagersample import { button, text } from 'unityengine ui'; import { zepetoscriptbehaviour } from 'zepeto script' import { localplayer, zepetoplayers } from 'zepeto character controller'; import { room, roomdata } from 'zepeto multiplay'; import { zepetoworldmultiplay } from 'zepeto world'; import { inventoryrecord, inventoryservice } from 'zepeto inventory'; import { waituntil } from 'unityengine'; export default class productmanagersample extends zepetoscriptbehaviour { public productid string; public productaddquantity number; public acquireitembtn button; public useitembtn button; public itemcounttext text; public multiplay zepetoworldmultiplay; private localplayer localplayer; start() { zepetoplayers instance onaddedlocalplayer addlistener(() => { this localplayer = zepetoplayers instance localplayer; this startcoroutine(this refreshproductui()); }); // 製品uiの更新のためのルーム参加イベントを処理します this multiplay roomjoined += (room room) => { room addmessagehandler("syncproductinfo", (message productmessage) => { this startcoroutine(this refreshproductui()); }); }; // アイテム取得ボタンのイベントリスナー 製品を追加 this acquireitembtn onclick addlistener(() => { const data = new roomdata(); data add("productid", this productid); data add("quantity", this productaddquantity); this multiplay room? send("addproduct", data getobject()); }) // アイテム使用ボタンのイベントリスナー 製品を使用 this useitembtn onclick addlistener(() => { const data = new roomdata(); data add("productid", this productid); data add("quantity", 1); this multiplay room? send("useproduct", data getobject()); }) } private refreshproductui() { const request = inventoryservice getasync(this productid); yield new waituntil(() => request keepwaiting == false); // リクエストが成功した場合、製品uiを更新します if (request responsedata issuccess) { this updateitemcounttext(request responsedata product); } else { console log("製品アイテムの読み込みに失敗しました。"); } } private updateitemcounttext(item inventoryrecord) { if (item != null) { this itemcounttext text = item quantity tostring(); } else { this itemcounttext text = "0"; } } } // 製品メッセージのインターフェース。 interface productmessage { productid string, productaction productaction, } // 製品アクションタイプを定義するための列挙型。 export enum productaction { use, add, } 製品クライアントスクリプトの説明 キャラクターがロードされるときは、 inventoryservice を使用して、 zepeto inventory https //developer zepeto me/docs/product/namespaces/zepeto inventory/ を使用して、製品の初期インベントリをロードします。その後、uiを更新します。 private refreshproductui() { const request = inventoryservice getasync(this productid); yield new waituntil(() => request keepwaiting == false); if (request responsedata issuccess) { this updateitemcounttext(request responsedata product); } else { console log("製品アイテムのロードに失敗しました。"); } } メッセージを送受信するには、 productmessage インターフェースを定義します。 productaction 列挙型を定義して、例の実装を行います。 interface productmessage { productid string, productaction productaction, } export enum productaction { 使用, 追加, } 製品を増減させるとき、そのデータをサーバーにルームメッセージとして送信します。 どの製品をどれだけ増減させるかに関するデータが含まれています。 this multiplay room? send("addproduct", data getobject()); this multiplay room? send("useproduct", data getobject()); 製品の変更を処理した後、サーバーは最終的な在庫情報をクライアントに送信し、uiをそれに応じて更新します。 製品 api 使用例 docid\ t4tujd mngihomx0dpyeq this multiplay roomjoined += (room room) => { room addmessagehandler("syncproductinfo", (message productmessage) => { this startcoroutine(this refreshproductui()); }); }; 一般的に、ワールド製品は購入するために通貨が必要です。 この場合、ガイドを参照して、 製品購入ボタン を使って簡単に販売できます。 世界を収益化しよう!製品と通貨の設定 docid\ lqww5dfmwkdvkcznz07bs サーバースクリプト 以下は、世界の通貨と製品を管理する完全なサーバーコードと、その実装方法です。 完全なサーバースクリプト import { sandbox, sandboxoptions, sandboxplayer } from "zepeto multiplay"; import { loadcurrency } from "zepeto multiplay currency"; import { loadinventory } from "zepeto multiplay inventory"; export default class extends sandbox { async oncreate(options sandboxoptions) { // 通貨 //通貨の読み込みをリクエスト this onmessage("loadbalance", (client, message string) => { this loadbalance(client, message); }); //クレジットの追加をリクエスト this onmessage("creditcurrency", (client, message currencymessage ) => { const currencyid = message currencyid; const quantity = message quantity; this addcredit(client, currencyid, quantity); }); //クレジットの引き落としをリクエスト this onmessage("debitcurrency", (client, message currencymessage ) => { const currencyid = message currencyid; const quantity = message quantity; this ondebit(client, currencyid, quantity); }); // 商品 //アイテムの追加をリクエスト this onmessage("addproduct", (client, message any) => { const productid = message productid; const quantity = message quantity; this addproduct(client, productid, quantity); }); //アイテムの使用をリクエスト this onmessage("useproduct", (client, message any) => { const productid = message productid; const quantity = message quantity; this useproduct(client, productid, quantity); }); } async addcredit(client sandboxplayer, currencyid string, quantity number) { try { const currency = await loadcurrency(client userid); await currency credit(currencyid, quantity); this loadbalance(client, currencyid); } catch (e) { console error(`addcredit error ${e}`); } } async ondebit(client sandboxplayer, currencyid string, quantity number) { try { const currency = await loadcurrency(client userid); if (await currency debit(currencyid, quantity) === true) { this loadbalance(client, currencyid); } else { console error("debitcredit error 通貨が不足しています"); } } catch (e) { console error(`debitcredit error ${e}`); } } async loadbalance(client sandboxplayer, currencyid string) { try { const currency = await loadcurrency(client userid); let balancesobject = await currency getbalances(); let balancesmap map\<string, number> = new map(object entries(balancesobject)); const specificcurrencyid = currencyid; const specificbalance = balancesmap get(specificcurrencyid) ?? 0; const currencysync currencymessage = { currencyid specificcurrencyid, quantity specificbalance } client send("synccurrencyinfo", currencysync); } catch (e) { console error(`loadbalance error ${e}`); } } async addproduct(client sandboxplayer, productid string, quantity number) { try { const inventory = await loadinventory(client userid); await inventory add(productid, quantity); const productmessage productmessage = { productid productid, productaction productaction add } client send("syncproductinfo", productmessage); } catch (e) { console error(`addproduct error ${e}`); } } async useproduct(client sandboxplayer, productid string, quantity number) { try { const inventory = await loadinventory(client userid); await inventory use(productid, quantity); const productmessage productmessage = { productid productid, productaction productaction use } client send("syncproductinfo", productmessage); } catch (e) { console error(`useproduct error ${e}`); } } onjoin(client sandboxplayer) { // 参加時のロジック } ontick(deltatime number) void { // タイックロジック } onleave(client sandboxplayer, consented? boolean) { // 退室時のロジック } } // 通貨メッセージのインターフェース interface currencymessage { currencyid string, quantity number, } // 商品メッセージのインターフェース。 interface productmessage { productid string, productaction productaction, } // 商品アクションタイプを定義するための列挙型。 export enum productaction { use, add, } サーバースクリプトの説明 世界の通貨を管理するには、 zepeto multiplay currency https //developer zepeto me/docs/multiplay server/namespaces/zepeto multiplay currency/ 。 を使用して、 currency credit() と currency debit() を使用して、希望する通貨の残高を増減させます。その後、 currency getbalances() を呼び出して、各通貨の現在の残高を取得します。 currency credit(currencyid, quantity); currency debit(currencyid, quantity); currency getbalances(); 世界の製品を管理するには、 zepeto multiplay inventory https //developer zepeto me/docs/multiplay server/namespaces/zepeto multiplay inventory 。 を使用して、 inventory add() と inventory use() を使用して、希望する通貨の残高を増減させます。 inventory add(productid, quantity); inventory use(productid, quantity); クライアントからの通貨や商品の増減に関するリクエストを処理します。 使用する loadbalance() を使用して、zepeto studioに登録された通貨情報を取得します。 複数の通貨がある場合、特定の通貨idの値に基づいて条件を設定し、その通貨の残高のみを取得できます。 最終的な残高値をクライアントにルームメッセージとして渡します。