สร้างโลกของคุณเอง
การเขียนสคริปต์
GameObjects and Components
13นาที
ในโลกนี้ การสร้างวัตถุหรือการปรับเปลี่ยนหรือเพิ่มส่วนประกอบเฉพาะให้กับวัตถุเป็นเรื่องที่พบได้ทั่วไป zepetoscript รองรับอินเทอร์เฟซเดียวกับที่ใช้ในสคริปต์ unity ตัวอย่างพื้นฐานของ gameobject และ component การสร้างและลบวัตถุเกม gameobject // gameobject สร้าง const tempobj = new gameobject(); const obj = object instantiate(tempobj); // gameobject ลบ object destroy(obj); เพื่อให้ได้ส่วนประกอบของ gameobjects หรือเพิ่มใหม่ ให้ใช้ getcomponent หรือ addcomponent component // getcomponent ด้วย generic const mytransform = this getcomponent\<transform>(); // addcomponent ด้วย generic const animator = this gameobject addcomponent\<animator>(); ตรวจสอบตัวอย่างโค้ดทั้งหมดสำหรับ gameobject component นี่คือตัวอย่างที่ครอบคลุมการสร้างและการปรับเปลี่ยนคุณสมบัติของ gameobject การเพิ่มส่วนประกอบ และการทำลาย gameobjectsample import { gameobject, object, transform, animator, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class gameobjectsample extends zepetoscriptbehaviour { private testobject gameobject; start() { // สร้าง gameobject ว่าง const tempobj = new gameobject(); this testobject = object instantiate(tempobj) as gameobject; // เปลี่ยนชื่อวัตถุ this testobject name = "testobject"; // เปลี่ยนข้อมูลการแปลงของวัตถุ this testobject transform position = new vector3(3,3,3); // แนบส่วนประกอบไปยังวัตถุ this testobject gameobject addcomponent\<animator>(); // ตัวอย่างการใช้ getcomponent const animator = this testobject getcomponent\<animator>(); if(animator != null) { console log("getcomponent สำเร็จ"); } // ทำลายวัตถุหลังจาก 5 วินาทีโดยใช้ coroutine this startcoroutine(this destroyobject()); } destroyobject() { yield new waitforseconds(5); // ทำลาย gameobject object destroy(this testobject); } } การใช้ find วิธีการค้นหาที่เกี่ยวข้องยังรองรับสไตล์อินเทอร์เฟซเดียวกันใน zepetoscript เช่นเดียวกับในสคริปต์ unity วิธีการ คำอธิบาย gameobject find() ค้นหาและส่งคืนวัตถุ gameobject ที่ใช้งานอยู่ตามชื่อในฉากปัจจุบัน。 \ ส่งคืน null หากไม่พบ gameobject findgameobjectwithtag() ค้นหาและส่งคืนวัตถุ gameobject ที่ใช้งานอยู่ตามแท็กในฉากปัจจุบัน。 \ ค้นหาและส่งคืนวัตถุแรกที่ตรงกับแท็กที่กำหนดในวัตถุที่ใช้งานอยู่ ส่งคืน null หากไม่พบ gameobject findgameobjectswithtag() ค้นหาวัตถุ gameobject ที่ใช้งานอยู่ทั้งหมดที่มีแท็กเฉพาะในฉากปัจจุบันและส่งคืนเป็นอาร์เรย์。 \ ส่งคืนอาร์เรย์ว่างหากไม่พบ สำหรับตัวอย่าง ให้ตั้งค่าฉากดังนี้ เพิ่มวัตถุ 3d หลายตัวและกำหนดแท็กทั้งหมดเป็น 3d สร้างวัตถุว่างเพื่อแนบสคริปต์และเปลี่ยนชื่อเป็น findsample typescript import { zepetoscriptbehaviour } from 'zepeto script'; import { gameobject, transform } from 'unityengine'; export default class findsample extends zepetoscriptbehaviour { public tempobj1 gameobject; public tempobj2 gameobject; public tempobj3 gameobject\[]; start() { // finds the target gameobject with the specified name // if multiple gameobjects have the same name, the first one found will be returned this tempobj1 = gameobject find("cube"); if(this tempobj1 != null) { console log("gameobject find success"); } // finds the target gameobject with the specified tag // if multiple gameobjects have the same tag, the first one found will be returned this tempobj2 = gameobject findgameobjectwithtag("3d"); if(this tempobj2 != null) { console log("findgameobjectwithtag success"); } // returns an array of gameobjects that have the specified tag this tempobj3 = gameobject findgameobjectswithtag("3d"); if(this tempobj3 != null) { console log("findgameobjectswithtag success"); } } } ผู้ตรวจสอบว่างเปล่าเมื่อคุณเพิ่มสคริปต์ในวัตถุ findsample กดปุ่มเล่นเพื่อรันมัน และคุณสามารถยืนยันในหน้าต่างผู้ตรวจสอบว่าทุกวัตถุได้รับการกำหนดค่าแล้วและตรวจสอบความสำเร็จในการค้นหาวัตถุผ่านหน้าต่างคอนโซล การสร้าง prefabs มาสำรวจวิธีการสร้าง prefabs ในระหว่างการทำงานกันเถอะ ขั้นแรก เพิ่มวัตถุ 3d > cube ลงในฉาก กดปุ่ม add component ใน inspector ของ cube และเพิ่ม rigidbody จากนั้นลาก cube ไปยังพื้นที่โปรเจกต์เพื่อทำให้มันเป็น prefab เนื่องจากเราจะใช้ prefab ให้ลบ cube ในฉากออก กรุณาเขียนสคริปต์ด้านล่างนี้ typescript import { gameobject, object, vector3, waitforseconds } from 'unityengine'; import { zepetoscriptbehaviour } from 'zepeto script'; export default class clonesample extends zepetoscriptbehaviour { public cloneprefab gameobject; start() { this startcoroutine(this doroutine()); } doroutine() { while(true) { yield null; // create a clone const clone = object instantiate(this cloneprefab) as gameobject; // set the height of the clone object clone transform position = new vector3(0, 10, 0); // destroy it after 5 seconds of creation gameobject destroy(clone, 5); yield new waitforseconds(1); } } } จากนั้นกลับไปที่ unity editor ลาก prefab ไปที่ script inspector เพื่อเพิ่มมัน กดปุ่มเล่นเพื่อตรวจสอบว่า 3d object ถูกสร้างขึ้นและตกจากด้านบนทุกๆ วินาที คลิกที่ลิงก์ด้านล่างเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับ unity gameobjects และ components ที่เสนอโดย zepetoscript 📘 การสร้าง gameobjects https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html https //docs unity3d com/2020 3/documentation/scriptreference/object instantiate html 📘 การทำลาย gameobjects https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html https //docs unity3d com/2020 3/documentation/scriptreference/object destroy html 📘 การใช้ components https //docs unity3d com/kr/current/manual/usingcomponents html https //docs unity3d com/kr/current/manual/usingcomponents html