GameObjects and Components
ในโลกนี้ การสร้างวัตถุหรือการปรับเปลี่ยนหรือเพิ่มส่วนประกอบเฉพาะให้กับวัตถุเป็นเรื่องที่พบได้ทั่วไป
ZEPETOScript รองรับอินเทอร์เฟซเดียวกับที่ใช้ในสคริปต์ Unity
การสร้างและลบวัตถุเกม:
เพื่อให้ได้ส่วนประกอบของ GameObjects หรือเพิ่มใหม่ ให้ใช้ GetComponent หรือ AddComponent
ตรวจสอบตัวอย่างโค้ดทั้งหมดสำหรับ GameObject Component
นี่คือตัวอย่างที่ครอบคลุมการสร้างและการปรับเปลี่ยนคุณสมบัติของ GameObject การเพิ่มส่วนประกอบ และการทำลาย
วิธีการค้นหาที่เกี่ยวข้องยังรองรับสไตล์อินเทอร์เฟซเดียวกันใน ZEPETOScript เช่นเดียวกับในสคริปต์ Unity.
วิธีการ | คำอธิบาย |
---|---|
GameObject.Find() | - ค้นหาและส่งคืนวัตถุ GameObject ที่ใช้งานอยู่ตามชื่อในฉากปัจจุบัน。 - ส่งคืน null หากไม่พบ. |
GameObject.FindGameObjectWithTag() | - ค้นหาและส่งคืนวัตถุ GameObject ที่ใช้งานอยู่ตามแท็กในฉากปัจจุบัน。 - ค้นหาและส่งคืนวัตถุแรกที่ตรงกับแท็กที่กำหนดในวัตถุที่ใช้งานอยู่ ส่งคืน null หากไม่พบ. |
GameObject.FindGameObjectsWithTag() | - ค้นหาวัตถุ GameObject ที่ใช้งานอยู่ทั้งหมดที่มีแท็กเฉพาะในฉากปัจจุบันและส่งคืนเป็นอาร์เรย์。 - ส่งคืนอาร์เรย์ว่างหากไม่พบ. |
สำหรับตัวอย่าง ให้ตั้งค่าฉากดังนี้:
- เพิ่มวัตถุ 3D หลายตัวและกำหนดแท็กทั้งหมดเป็น 3D.
- สร้างวัตถุว่างเพื่อแนบสคริปต์และเปลี่ยนชื่อเป็น FindSample.

- ผู้ตรวจสอบว่างเปล่าเมื่อคุณเพิ่มสคริปต์ในวัตถุ FindSample.

- กดปุ่มเล่นเพื่อรันมัน และคุณสามารถยืนยันในหน้าต่างผู้ตรวจสอบว่าทุกวัตถุได้รับการกำหนดค่าแล้วและตรวจสอบความสำเร็จในการค้นหาวัตถุผ่านหน้าต่างคอนโซล.

มาสำรวจวิธีการสร้าง prefabs ในระหว่างการทำงานกันเถอะ
- ขั้นแรก เพิ่มวัตถุ 3D > Cube ลงในฉาก

- กดปุ่ม Add Component ใน inspector ของ Cube และเพิ่ม Rigidbody

- จากนั้นลาก Cube ไปยังพื้นที่โปรเจกต์เพื่อทำให้มันเป็น prefab

เนื่องจากเราจะใช้ Prefab ให้ลบ Cube ในฉากออก
- กรุณาเขียนสคริปต์ด้านล่างนี้
- จากนั้นกลับไปที่ Unity editor ลาก prefab ไปที่ script inspector เพื่อเพิ่มมัน.

- กดปุ่มเล่นเพื่อตรวจสอบว่า 3D Object ถูกสร้างขึ้นและตกจากด้านบนทุกๆ วินาที.

คลิกที่ลิงก์ด้านล่างเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับ Unity GameObjects และ Components ที่เสนอโดย ZEPETOScript.
📘 การสร้าง GameObjects https://docs.unity3d.com/2020.3/Documentation/ScriptReference/Object.Instantiate.html
📘 การทำลาย GameObjects https://docs.unity3d.com/2020.3/Documentation/ScriptReference/Object.Destroy.html
📘 การใช้ components https://docs.unity3d.com/kr/current/Manual/UsingComponents.html