สร้างโลกของคุณเอง
ผู้เล่นและตัวละคร: ขั้นสูง
การสร้างและการควบคุม NPC
42นาที
ศึกษาวิธีสร้าง npc ตัวละคร zepeto นำสคริปต์ตัวอย่างแต่ละฟังก์ชันที่มีให้มาใช้ในการเพิ่ม npc ที่ต้องการลงในเวิลด์ สร้าง npc ใช้ zepeto id ในการสร้าง npc ❗️ คำเตือน คำเตือน รูปลักษณ์ของ npc และไอเทมที่ใส่จะตรงกับ zepeto id ที่ป้อน แนะนำให้ตั้งค่าตัวละคร zepeto ที่มีรูปลักษณ์พิเศษก่อน แล้วค่อยสร้าง npc ตั้งค่าวัตถุในตำแหน่งที่จะสร้าง npc สร้างโค้ดสำหรับสร้างตัวละคร zepeto แบบพื้นฐานใน scene 📘 กรุณาอ้างอิงจากคู่มือต่อไปนี้ \[ การสร้างตัวละคร zepeto docid\ mrnk9t5xnrdqcvmsgfgpk ] เลือก hierarchy > create empty object เพื่อสร้างวัตถุ แล้วเปลี่ยนชื่อเป็น npc วัตถุสำหรับบันทึกตำแหน่งที่จะสร้าง npc ตั้งค่า position และ rotation เขียนสคริปต์สร้าง npc เลือก project > create > zepeto > typescript เพื่อสร้างสคริปต์ แล้วเปลี่ยนชื่อเป็น npccreator เขียนสคริปต์ตัวอย่างดังด้านล่าง import { zepetoscriptbehaviour } from 'zepeto script'; import { spawninfo, zepetocharacter, zepetocharactercreator } from 'zepeto character controller'; export default class npccreator extends zepetoscriptbehaviour { // zepeto id of the npc public zepetoid string; // npc character object private npc zepetocharacter; start() { // create a new instance of spawninfo and set its position and rotation based on the object's transform const spawninfo = new spawninfo(); spawninfo position = this transform position; spawninfo rotation = this transform rotation; // use zepetocharactercreator to create a new character by zepeto id and assign it to npc variable zepetocharactercreator createbyzepetoid(this zepetoid, spawninfo, (character zepetocharacter) => { this npc = character; }) } } การทำงานของสคริปต์จะเป็นไปดังนี้ start() ใช้ฟังก์ชัน zepetocharactercreator createbyzepetoid() สร้าง npc ในตำแหน่งวัตถุ npc จากนั้นบันทึกลงใน npc หลังจากเขียนสคริปต์เสร็จเรียบร้อยแล้ว เพิ่มสคริปต์ลงในวัตถุ npc กำหนด zepeto id ใน inspector zepeto id zepeto id ของ npc เมื่อกดปุ่ม play เพื่อรัน npc ที่มีป้ายชื่อจะถูกสร้างขึ้น สามารถสร้าง npc ต่างๆ ได้อย่างง่ายๆ โดยการเพิ่มวัตถุในตำแหน่ง npc ด้วยวิธีเดียวกัน และเพิ่มสคริปต์ npc ติดป้ายชื่อให้ npc สามารถแยกผู้เล่นและ npc ได้โดยการติดป้ายชื่อให้ npc สร้าง prefab ของ canvas ป้ายชื่อ เลือก hierachy > ui > canvas และสร้าง จากนั้นเปลี่ยนชื่อเป็น prefnametagcanvas ตั้งค่า render mode เป็น world space เลือก hierachy > ui > text แล้วสร้างให้เป็น child ของ prefnametagcanvas จากนั้นเปลี่ยนชื่อเป็น nametagtext ข้อความที่จะปรากฏเป็นชื่อ เพิ่ม component "content size fitter" เพื่อทำให้ข้อความมีขนาดเหมาะสม เมื่อตั้งค่าเสร็จเรียบร้อยแล้ว ลากไปยังหน้าต่าง project เพื่อสร้างเป็น prefab จากนั้นลบ prefnametagcanvas ที่ยังเหลืออยู่ใน hierachy เขียนสคริปต์ป้ายชื่อ npc เลือก project > create > zepeto > typescript และสร้าง จากนั้นเปลี่ยนชื่อเป็น npccreatorwithnametag เขียนสคริปต์ตัวอย่างตามด้านล่างนี้ import { zepetoscriptbehaviour } from 'zepeto script'; import { knowsockets, spawninfo, zepetocharacter, zepetocharactercreator } from 'zepeto character controller'; import { canvas, camera, vector3, object, gameobject } from 'unityengine'; import { text } from 'unityengine ui'; export default class npccreatorwithnametag extends zepetoscriptbehaviour { // zepeto id of the npc public zepetoid string; // name to be displayed in the name tag public nametag string; // prefab of the name tag canvas game object public nametagprefab gameobject; // y axis offset value of the name tag canvas game object public nametagyoffset number; // npc character object private npc zepetocharacter; // name tag canvas game object private npcnametagobject gameobject; // text inside the name tag canvas game object private npcnametagtext text; // name tag canvas private canvas canvas; // world camera private cachedworldcamera camera; start() { // create a new instance of spawninfo and set its position and rotation based on the object's transform const spawninfo = new spawninfo(); spawninfo position = this transform position; spawninfo rotation = this transform rotation; // use zepetocharactercreator to create a new character by zepeto id and assign it to npc variable zepetocharactercreator createbyzepetoid(this zepetoid, spawninfo, (character zepetocharacter) => { this npc = character; // set the name tag this setnametag(); }) } // set the name tag setnametag() { // dynamically create the name tag canvas game object this npcnametagobject = object instantiate(this nametagprefab) as gameobject; // set the parent of the name tag canvas game object transform to be the npc transform this npcnametagobject transform setparent(this npc transform); // set the position of the name tag canvas game object above the npc's head this npcnametagobject transform position = vector3 op addition(this npc getsocket(knowsockets head upper) position, new vector3(0, this nametagyoffset,0)); // set the text inside the name tag this npcnametagtext = this npcnametagobject getcomponentinchildren\<text>(); this npcnametagtext text = this nametag; this canvas = this npcnametagobject getcomponent\<canvas>(); this cachedworldcamera = object findobjectoftype\<camera>(); this canvas worldcamera = this cachedworldcamera; } private update() { if (this canvas != null) { this updatecanvasrotation(); } } // update the rotation of the name tag canvas to face the camera private updatecanvasrotation() { this canvas transform lookat(this cachedworldcamera transform); this canvas transform rotate(0, 180, 0); } } การทำงานของสคริปต์จะเป็นไปดังนี้ start() เรียกฟังก์ชันคัสตอม setnametag() setnametag() สร้างป้ายชื่อ npc และปรับตำแหน่งป้ายชื่อที่สร้างให้อยู่บนหัว npc ตั้งค่าข้อความในป้ายชื่อ update() เรียกใช้ฟังก์ชันคัสตอม updatecanvasrotation() เพื่อหมุน canvas ให้ตรงกับกล้อง หลังจากเขียนสคริปต์เสร็จแล้ว เพิ่มสคริปต์ลงในวัตถุ npc กำหนด zepeto id, name tag, name tag prefab และ name tag y offset ใน inspector name tag ชื่อที่จะแสดงในป้ายชื่อ npc name tag prefab prefab แคนวาสป้ายชื่อ name tag y offset ตัวแปรบันทึกค่า offset แกน y ของวัตถุแคนวาสป้ายชื่อ ซึ่งสามารถปรับระยะห่างระหว่างตัวละครและป้ายชื่อเมื่อให้ป้ายชื่อปรากฏบนหัวตัวละครได้ เมื่อกดปุ่ม play เพื่อรัน npc ที่มีป้ายชื่อจะถูกสร้างขึ้น ควบคุมการเคลื่อนไหวของ npc สามารถควบคุมการเคลื่อนไหวของ npc ได้ กระโดด ใช้ zepeto character api เพื่อทำให้ npc กระโดด สามารถใช้ zepeto character api สร้างการเคลื่อนไหวให้หลากหลายมากขึ้นได้ 📘 กรุณาอ้างอิงจากคู่มือต่อไปนี้ \[ zepeto character docid 1sjftquomtcxfkg3tjh g ] เขียนสคริปต์ npc กระโดด เลือก project > create > zepeto > typescript แล้วสร้างสคริปต์ จากนั้นเปลี่ยนชื่อเป็น npcjump เขียนสคริปต์ตัวอย่างตามด้านล่างนี้ import { zepetoscriptbehaviour } from 'zepeto script'; import { spawninfo, zepetocharacter, zepetocharactercreator } from 'zepeto character controller'; import { waitforseconds } from 'unityengine'; export default class npcjump extends zepetoscriptbehaviour { // zepeto id of the npc public zepetoid string; // npc character object private npc zepetocharacter; start() { // create a new instance of spawninfo and set its position and rotation based on the object's transform const spawninfo = new spawninfo(); spawninfo position = this transform position; spawninfo rotation = this transform rotation; // use zepetocharactercreator to create a new character by zepeto id and assign it to npc variable zepetocharactercreator createbyzepetoid(this zepetoid, spawninfo, (character zepetocharacter) => { this npc = character; this startcoroutine(this jumpcoroutine()); }); } jumpcoroutine() { // infinite loop to continuously while (true) { // call the jump() method of the zepetocharacter to make it jump this npc jump(); // wait for 5 seconds yield new waitforseconds(5); } } } การทำงานของสคริปต์จะเป็นไปดังนี้ start() เรียกใช้ coroutine "jumpcoroutine()" jumpcoroutine() เรียกใช้ method "jump()" เพื่อให้ npc กระโดดทุก 5 วินาที หลังจากเขียนสคริปต์เสร็จเรียบร้อยแล้ว เพิ่มสคริปต์ลงในวัตถุ npc กดปุ่ม play เพื่อให้ npc กระโดด ท่าทาง ใช้ animator controller เพื่อทำให้ npc ทำท่าทาง สามารถใช้ animator controller สร้างท่าทางให้หลากหลายมากขึ้นได้ 📘 unity animator controller https //docs unity3d com/manual/class animatorcontroller html https //docs unity3d com/manual/class animatorcontroller html เตรียมคลิปอนิเมชัน เตรียมคลิปอนิเมชันท่าทางที่จะให้ npc ทำโดยอ้างอิงจากคู่มือต่อไปนี้ 📘 please refer to the following guide \[ whether to use zepeto character animation docid\ ahh0r8pikezml5up q4dc ] \[ how to apply custom animation docid\ fgnaprjtltzzp5pj6sn4x ] สร้างอนิเมเตอร์ เลือก project > create > animator controller แล้วสร้าง จากนั้นเปลี่ยนชื่อเป็น npcanimatorcontroller เปิดแท็บ animator จากนั้นเลือก create state > empty และสร้าง เปลี่ยนชื่อให้เหมาะสมใน inspector และกำหนดคลิปอนิเมชันใน motion เขียนสคริปต์ท่าทาง npc เลือก project > create > zepeto > typescript แล้วสร้างสคริปต์ จากนั้นเปลี่ยนชื่อเป็น npcgesture เขียนสคริปต์ตัวอย่างตามด้านล่างนี้ import { zepetoscriptbehaviour } from 'zepeto script'; import { spawninfo, zepetocharacter, zepetocharactercreator } from 'zepeto character controller'; import { animator, runtimeanimatorcontroller } from 'unityengine'; export default class npcgesture extends zepetoscriptbehaviour { // the zepeto id of the npc public zepetoid string; // the animator controller of the npc public npcanimator runtimeanimatorcontroller; // npc character object private npc zepetocharacter; start() { // create a new instance of spawninfo and set its position and rotation based on the object's transform const spawninfo = new spawninfo(); spawninfo position = this transform position; spawninfo rotation = this transform rotation; // use zepetocharactercreator to create a new character by zepeto id and assign it to npc variable zepetocharactercreator createbyzepetoid(this zepetoid, spawninfo, (character zepetocharacter) => { this npc = character; // get the animator component from the npc character and set its runtimeanimatorcontroller to npcanimator this npc getcomponentinchildren\<animator>() runtimeanimatorcontroller = this npcanimator; }); } } การทำงานของสคริปต์จะเป็นไปดังนี้ start() นำเข้า animator component ของวัตถุ npc แล้วตั้งค่าให้เป็น animator controller ที่กำหนดด้วยตัวแปร npcanimator หลังจากเขียนสคริปต์เสร็จแล้ว เพิ่มสคริปต์ลงในวัตถุตำแหน่งที่จะสร้าง npc กำหนด zepeto id, npc animator ใน inspector npc animator animation controller ของ npc เมื่อกดปุ่ม play เพื่อรัน จะสามารถตรวจสอบผลลัพธ์ในการทำท่าทางของ npc ได้ สามารถนำไปปรับใช้ในการทำให้ npc มีการเคลื่อนไหวที่หลากหลายมากขึ้นนอกเหนือจากการกระโดดและการทำท่าทางได้ สร้างกรอบคำพูดด้านบน npc สามารถสร้างกรอบคำพูดได้โดยการสร้าง canvas บนหัว npc และแสดงรูปภาพหรือข้อความ สร้าง prefab แคนวาสกรอบคำพูด เลือก hierachy > ui > canvas เพื่อสร้าง จากนั้นเปลี่ยนชื่อเป็น prefspeechbubblecanvas ตั้งค่า render mode เป็น world space เลือก hierachy > ui > image และสร้างให้เป็น child ของ prefspeechbubblecanvas จากนั้นเปลี่ยนชื่อเป็น speeachbubbleimage รูปภาพที่จะใช้เป็นพื้นหลังของกรอบคำพูด เลือก hierachy > ui > text และสร้างให้เป็น child ของ speeachbubbleimage จากนั้นเปลี่ยนชื่อเป็น speeachbubbletext ข้อความในกรอบคำพูด เพิ่ม component "content size fitter" เพื่อทำให้ข้อความมีขนาดเหมาะสม เมื่อตั้งค่าเสร็จแล้ว ลากไปยังหน้าต่าง project เพื่อสร้างเป็น prefab จากนั้นลบ prefspeechbubblecanvas ที่ยังเหลืออยู่ใน hierachy เขียนสคริปต์กรอบคำพูด npc เลือก project > create > zepeto > typescript และสร้างสคริปต์ จากนั้นเปลี่ยนชื่อเป็น npcspeechbubble เขียนสคริปต์ตัวอย่างตามด้านล่างนี้ import { zepetoscriptbehaviour } from 'zepeto script'; import { knowsockets, spawninfo, zepetocharacter, zepetocharactercreator } from 'zepeto character controller'; import { canvas, camera, vector3, object, gameobject } from 'unityengine'; import { text } from 'unityengine ui'; export default class npcspeechbubble extends zepetoscriptbehaviour { // zepeto id of the npc public zepetoid string; // dialogue content to be displayed in the speech bubble public speechbubbletext string; // prefab of the speech bubble canvas game object public speechbubbleprefab gameobject; // y axis offset value of the speech bubble canvas game object public speechbubbleyoffset number; // npc character object private npc zepetocharacter; // speech bubble canvas game object private speechbubbleobject gameobject; // text inside the speech bubble canvas game object private speechbubbletext text; // speech bubble canvas private canvas canvas; // world camera private cachedworldcamera camera; start() { // create a new instance of spawninfo and set its position and rotation based on the object's transform const spawninfo = new spawninfo(); spawninfo position = this transform position; spawninfo rotation = this transform rotation; // use zepetocharactercreator to create a new character by zepeto id and assign it to npc variable zepetocharactercreator createbyzepetoid(this zepetoid, spawninfo, (character zepetocharacter) => { this npc = character; // set the speech bubble this setbubble(); }) } // set the speech bubble setbubble() { // dynamically create the speech bubble canvas game object this speechbubbleobject = object instantiate(this speechbubbleprefab) as gameobject; // set the parent of the speech bubble canvas game object transform to be the npc transform this speechbubbleobject transform setparent(this npc transform); // set the position of the speech bubble canvas game object above the npc's head this speechbubbleobject transform position = vector3 op addition(this npc getsocket(knowsockets head upper) position, new vector3(0, this speechbubbleyoffset,0)); // set the text inside the speech bubble this speechbubbletext = this speechbubbleobject getcomponentinchildren\<text>(); this setbubbletext(this speechbubbletext); this canvas = this speechbubbleobject getcomponent\<canvas>(); this cachedworldcamera = object findobjectoftype\<camera>(); this canvas worldcamera = this cachedworldcamera; } // open the speech bubble canvas and set the text setbubbletext(bubbletext string) { this speechbubbleobject setactive(true); this speechbubbletext text = bubbletext; } private update() { if (this canvas != null) { this updatecanvasrotation(); } } // update the rotation of the speech bubble canvas to face the camera private updatecanvasrotation() { this canvas transform lookat(this cachedworldcamera transform); this canvas transform rotate(0, 180, 0); } } การทำงานของสคริปต์จะเป็นไปดังนี้ start() เรียกใช้ฟังก์ชันคัสตอม setbubble() setbubble() สร้างแคนวาสกรอบคำพูด (speechbubbleprefab) และปรับให้ตำแหน่งของกรอบคำพูดที่สร้างไปอยู่บนหัวของ npc เรียกใช้ฟังก์ชันคัสตอม setbubbletext() เพื่อตั้งค่าข้อความในกรอบคำพูด setbubbletext() เปิดใช้งานแคนวาสกรอบคำพูดของ ( speechbubbleobject) แสดง string ที่ให้มาเป็นพารามิเตอร์ (bubbletext) ในกรอบคำพูด update() เรียกใช้ฟังก์ชันคัสตอม updatecanvasrotation() เพื่อหมุน canvas ให้ตรงกับกล้อง หลังจากเขียนสคริปต์เสร็จแล้ว เพิ่มสคริปต์ลงในวัตถุตำแหน่งที่จะสร้าง npc กำหนด zepeto id, speech bubble text, speech bubble prefab และ speech bubble y offset ใน inspector speech bubble text ตัวแปรที่บันทึกบทสนทนาของตัวละคร npc ที่จะให้ปรากฏในกรอบคำพูด โดยจะบันทึกบทสนทนาเหมือนกับ "hello world" ในตัวอย่าง speech bubble prefab ตัวแปรที่บันทึก prefab ของ gameobject แคนวาสกรอบคำพูด speech bubble y offset ตัวแปรที่บันทึกค่า offset แกน y ของ gameobject แคนวาสกรอบคำพูด ซึ่งสามารถปรับระยะห่างระหว่างตัวละครและกรอบคำพูดเมื่อมีกรอบคำพูดปรากฏบนหัวของตัวละครได้ เมื่อกดปุ่ม play เพื่อรัน จะเห็นกรอบคำพูดปรากฏบนหัวของ npc interaction กับ npc สามารถสร้างคอนเทนต์น่าสนใจได้มากมายโดย interaction กับ npc ในคู่มือนี้ จะใช้ตัวอย่างสร้างกรอบคำพูดที่จะเปลี่ยนไปเมื่อเข้าใกล้ npc ตั้งค่า colider เพิ่ม colider component ลงในวัตถุเพื่อ interaction กับ npc จากนั้นกดเช็ก istrigger ปรับขนาด colider ให้มีขอบเขตเท่าที่ต้องการให้ผู้เล่นสามารถมี interaction กับ npc ได้ เขียนสคริปต์ interaction กับ npc เลือก project > create > zepeto > typescript และสร้างสคริปต์ จากนั้นเปลี่ยนชื่อเป็น npcinteraction เขียนสคริปต์ตัวอย่างตามด้านล่างนี้ import { zepetoscriptbehaviour } from 'zepeto script'; import { knowsockets, spawninfo, zepetocharacter, zepetocharactercreator, zepetoplayers } from 'zepeto character controller'; import { canvas, camera, vector3, object, gameobject, collider } from 'unityengine'; import { text } from 'unityengine ui'; export default class npcinteraction extends zepetoscriptbehaviour { // zepeto id of the npc public zepetoid string; // dialogue content to be displayed in the speech bubble public speechbubbletext string; public changedspeechbubbletext string; // prefab of the speech bubble canvas game object public speechbubbleprefab gameobject; // y axis offset value of the speech bubble canvas game object public speechbubbleyoffset number; // local character object private zepetocharacter zepetocharacter; // npc character object private npc zepetocharacter; // speech bubble canvas game object private speechbubbleobject gameobject; // text inside the speech bubble canvas game object private speechbubbletext text; // speech bubble canvas private canvas canvas; // world camera private cachedworldcamera camera; start() { // create a new instance of spawninfo and set its position and rotation based on the object's transform const spawninfo = new spawninfo(); spawninfo position = this transform position; spawninfo rotation = this transform rotation; // use zepetocharactercreator to create a new character by zepeto id and assign it to npc variable zepetocharactercreator createbyzepetoid(this zepetoid, spawninfo, (character zepetocharacter) => { this npc = character; // set the speech bubble this setbubble(); }) zepetoplayers instance onaddedlocalplayer addlistener(() => { this zepetocharacter = zepetoplayers instance localplayer zepetoplayer character; }); } // check if player character enter collider ontriggerenter(collider collider) { if (this zepetocharacter == null || collider gameobject != this zepetocharacter gameobject) { return; } this setbubbletext(this changedspeechbubbletext); } ontriggerexit(collider collider) { if (this zepetocharacter == null || collider gameobject != this zepetocharacter gameobject) { return; } this setbubbletext(this speechbubbletext); } // set the speech bubble setbubble() { // dynamically create the speech bubble canvas game object this speechbubbleobject = object instantiate(this speechbubbleprefab) as gameobject; // set the parent of the speech bubble canvas game object transform to be the npc transform this speechbubbleobject transform setparent(this npc transform); // set the position of the speech bubble canvas game object above the npc's head this speechbubbleobject transform position = vector3 op addition(this npc getsocket(knowsockets head upper) position, new vector3(0, this speechbubbleyoffset,0)); // set the text inside the speech bubble this speechbubbletext = this speechbubbleobject getcomponentinchildren\<text>(); this setbubbletext(this speechbubbletext); this canvas = this speechbubbleobject getcomponent\<canvas>(); this cachedworldcamera = object findobjectoftype\<camera>(); this canvas worldcamera = this cachedworldcamera; } // open the speech bubble canvas and set the text setbubbletext(bubbletext string) { this speechbubbleobject setactive(true); this speechbubbletext text = bubbletext; } private update() { if (this canvas != null) { this updatecanvasrotation(); } } // update the rotation of the speech bubble canvas to face the camera private updatecanvasrotation() { this canvas transform lookat(this cachedworldcamera transform); this canvas transform rotate(0, 180, 0); } } การทำงานของสคริปต์จะเป็นไปดังนี้ ontriggerenter(), ontriggerexit() เรียกใช้ฟังก์ชันคัสตอม setbubbletext() เพื่อตั้งข้อความในกรอบคำพูดเป็น changedspeechbubbletext เมื่อเข้ามาในขอบเขต collider แล้วตรวจจับทริกเกอร์ เรียกใช้ฟังก์ชันคัสตอม setbubbletext() เพื่อตั้งข้อความในกรอบคำพูดเป็น speechbubbletext เมื่อออกไปนอกขอบเขต collider หลังจากเขียนสคริปต์เสร็จแล้ว เพิ่มสคริปต์ลงในวัตถุตำแหน่งที่จะสร้าง npc กำหนด zepeto id, speech bubble text, speech bubble prefab, speech bubble y offset และ changed speech bubble ใน inspector speech bubble text ตัวแปรที่บันทึกบทสนทนาของตัวละคร npc ที่จะให้ปรากฏในกรอบคำพูด โดยจะบันทึกบทสนทนาเหมือนกับ "hello world" ในตัวอย่าง speech bubble prefab ตัวแปรที่บันทึก prefab ของ gameobject แคนวาสกรอบคำพูด speech bubble y offset ตัวแปรที่บันทึกค่า offset แกน y ของ gameobject แคนวาสกรอบคำพูด ซึ่งสามารถปรับระยะห่างระหว่างตัวละครและกรอบคำพูดเมื่อมีกรอบคำพูดปรากฏบนหัวของตัวละครได้ changed speech bubble บันทึกบทสนทนาที่จะแสดงในกรอบคำพูดของ npc เมื่อผู้เล่นเข้าไปใน colider ของ หลังจากกดปุ่ม play เพื่อรันแล้ว ข้อความในกรอบคำพูดจะเปลี่ยนเมื่อเข้าใกล้ npc ใช้การโต้ตอบกับ npc หากต้องการสร้างรูปแบบไดอะล็อก ให้สร้าง ui โดยใช้แผง ต่อไปนี้เป็นตัวอย่างของกล่องโต้ตอบง่ายๆ ที่ประกอบด้วยแผงและปุ่มต่างๆ นี่คือสคริปต์ตัวอย่างที่เปิดกล่องโต้ตอบเมื่อโต้ตอบกับ npc และประมวลผลเมื่อกดปุ่มแต่ละปุ่ม ใช้สิ่งนี้เพื่อปรับใช้เนื้อหาที่น่าสนใจ import { gameobject, humanbodybones, object, collider } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; import { button } from 'unityengine ui'; import { zepetocharacter, zepetoplayers } from "zepeto character controller"; export default class npcdialoginteraction extends zepetoscriptbehaviour { public npcdialogcanvas gameobject; public yesbutton button; public nobutton button; private zepetocharacter zepetocharacter; start() { zepetoplayers instance onaddedlocalplayer addlistener(()=>{ this zepetocharacter = zepetoplayers instance localplayer zepetoplayer character; }); // dialog select yes this yesbutton onclick addlistener(() => { console log("yes") this npcdialogcanvas setactive(false); }); // dialog select no this nobutton onclick addlistener(() => { console log("no") this npcdialogcanvas setactive(false); }); } // check if player character enter collider ontriggerenter(collider collider) { if (this zepetocharacter == null || collider gameobject != this zepetocharacter gameobject) { return; } this npcdialogcanvas setactive(true); } ontriggerexit(collider collider) { if (this zepetocharacter == null || collider gameobject != this zepetocharacter gameobject) { return; } this npcdialogcanvas setactive(false); } }