创造你的世界
经济
产品 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()); 服务器然后处理货币的增加或减少,并将最终余额信息发送给客户端。客户端接收此信息并更新用户界面。 产品 api 使用示例 docid\ r9obhku jurf6khvcqjha 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\ r9obhku jurf6khvcqjha this multiplay roomjoined += (room room) => { room addmessagehandler("syncproductinfo", (message productmessage) => { this startcoroutine(this refreshproductui()); }); }; 通常,世界产品需要花费货币进行购买。 在这种情况下,您可以轻松地通过一个 产品购买按钮 进行销售,参考指南 将你的世界货币化!设置产品和货币 docid\ uw6k8dr1whl1wgqblq64g 服务器脚本 以下是管理世界货币和产品的完整服务器代码,以及如何实现它。 完整服务器脚本 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 错误 ${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 错误 货币不足"); } } catch (e) { console error(`debitcredit 错误 ${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 错误 ${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 错误 ${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 错误 ${e}`); } } onjoin(client sandboxplayer) { // 加入逻辑在这里 } ontick(deltatime number) void { // tick 逻辑在这里 } 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值进行条件筛选,以仅获取该货币的余额。 将最终余额值作为房间消息传递给客户端。