สร้างโลกของคุณเอง
เศรษฐกิจ
ตัวอย่างการใช้งาน Product API
18 นาที
ต่อไปนี้คือตัวอย่างการใช้ product api เพื่อจัดการสกุลเงินโลกและผลิตภัณฑ์โลก ตัวอย่างนี้ช่วยให้คุณเข้าใจพื้นฐานของ product 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); }); // handles room joined events for currency ui updates this multiplay roomjoined += (room room) => { room addmessagehandler("synccurrencyinfo", (message currencymessage) => { const currentcurrencyid = message currencyid; const currentquantity = message quantity; this balanceuiupdate(currentcurrencyid, currentquantity); }); }; // button event listener for increasing currency request credit 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()); }) // button event listener for decreasing currency request debit 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()); }) } // requests the current amount of specified currency private loadcurrencybalance(currentcurrencyid string) { this multiplay room? send("loadbalance", currentcurrencyid); } // updates the ui to reflect the current quantity of the currency private balanceuiupdate(currentcurrencyid string, currentquantity number) { this balancetext text = currentquantity tostring(); } } // interface for currency sync messages 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 ตัวอย่างการใช้งาน product api docid\ dgsveluxyfp drtwacokt 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, } // enum สำหรับการกำหนดประเภทการกระทำผลิตภัณฑ์ export enum productaction { ใช้, เพิ่ม, } คำอธิบายสคริปต์ลูกค้าผลิตภัณฑ์ เมื่อมีการโหลดตัวละคร ให้ใช้ 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 enum สำหรับการใช้งานตัวอย่าง interface productmessage { productid string, productaction productaction, } export enum productaction { ใช้, เพิ่ม, } เมื่อคุณเพิ่มหรือลดผลิตภัณฑ์ คุณจะส่งข้อมูลนั้นไปยังเซิร์ฟเวอร์ในฐานะข้อความห้อง มันรวมข้อมูลเกี่ยวกับผลิตภัณฑ์ที่ต้องการเพิ่มหรือลดและจำนวนที่ต้องการเพิ่มหรือลดด้วย this multiplay room? send("addproduct", data getobject()); this multiplay room? send("useproduct", data getobject()); หลังจากประมวลผลการเปลี่ยนแปลงผลิตภัณฑ์ เซิร์ฟเวอร์จะส่งข้อมูลสินค้าคงคลังสุดท้ายไปยังไคลเอนต์ ซึ่งจะอัปเดต ui ตามนั้น ตัวอย่างการใช้งาน product api docid\ dgsveluxyfp drtwacokt this multiplay roomjoined += (room room) => { room addmessagehandler("syncproductinfo", (message productmessage) => { this startcoroutine(this refreshproductui()); }); }; โดยทั่วไป ผลิตภัณฑ์ในโลกควรมีค่าใช้จ่ายในการซื้อ ในกรณีนี้ คุณสามารถขายได้ง่ายๆ ด้วย ปุ่มซื้อผลิตภัณฑ์ โดยอ้างอิงจากคู่มือ สร้างรายได้จากโลกของคุณ! การตั้งค่าสินค้าและสกุลเงิน docid 7wb5zcznhytu 6qzn63cv สคริปต์เซิร์ฟเวอร์ ด้านล่างนี้คือรหัสเซิร์ฟเวอร์ทั้งหมดที่จัดการสกุลเงินโลกและผลิตภัณฑ์ และวิธีการนำไปใช้ สคริปต์เซิร์ฟเวอร์ทั้งหมด 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 สกุลเงินเฉพาะเพื่อดึงยอดคงเหลือของสกุลเงินนั้นเท่านั้น ส่งค่าบัญชีสุดท้ายไปยังลูกค้าเป็นข้อความในห้อง