สร้างโลกของคุณเอง
การเขียนสคริปต์
สคริปต์อิมพอร์ต
3นาที
นอกจากการเชื่อมต่อกับ gameobject แล้ว zepetoscript สามารถใช้โดยตรงจากสคริปต์อื่นได้ คุณสามารถนำเข้าและใช้ zepetoscript ได้ง่ายๆ เช่นเดียวกับที่คุณใช้ไลบรารีหรือโมดูล ในไวยากรณ์ typescript คุณต้องใช้เส้นทางสัมพัทธ์ไปยังไฟล์ zepetoscript พิจารณาสถานการณ์ด้านล่าง หากคุณต้องการนำเข้า extracomponent ไปยัง scriptimport คุณจะต้องใช้ไวยากรณ์ดังต่อไปนี้ ตัวอย่างต่อไปนี้เป็นการประกาศใน scriptimport ts เพื่อเรียก extracomponent ts ที่ตั้งอยู่ในโฟลเดอร์ lib typescript import extracomponent from ' /lib/extracomponent'; 👍 เคล็ดลับ สิ่งสำคัญคือต้องระบุเส้นทางให้ถูกต้องเมื่อทำการนำเข้าไฟล์ ts อื่น โปรดทำความคุ้นเคยกับการใช้สัญลักษณ์เส้นทางสัมพัทธ์ / หมายถึงไดเรกทอรีปัจจุบัน / หมายถึงไดเรกทอรีแม่ หมายถึงไดเรกทอรีที่อยู่หนึ่งระดับขึ้นไปจากไดเรกทอรีปัจจุบัน คุณสามารถเข้าถึงฟังก์ชันและตัวแปรที่ส่งออกซึ่งประกาศในสคริปต์ที่นำเข้าได้โดยการอ้างอิงโดยตรง เช่นที่แสดงในตัวอย่าง this gameobject addcomponent\<extracomponent>(); const extracomponent = this gameobject getcomponent\<extracomponent>(); // รับค่าจากการเรียกใช้เมธอด const count = extracomponent getcount(); // ตั้งค่าจากการเรียกใช้เมธอด extracomponent setcount(0); // รับคุณสมบัติสาธารณะ const resultstring = extracomponent stringproperty; ต่อไปนี้คือตัวอย่างโค้ดที่แสดง typescript ที่นำเข้า import { zepetoscriptbehaviour } from 'zepeto script'; import { text } from 'unityengine ui'; // นำเข้าสคริปต์ที่กำหนดเองจากเส้นทาง import extracomponent from ' /lib/extracomponent'; export default class scriptimport extends zepetoscriptbehaviour { public resultui text; private extcomponent extracomponent; start() { // เพิ่มส่วนประกอบสคริปต์ this gameobject addcomponent\<extracomponent>(); this extcomponent = this gameobject getcomponent\<extracomponent>(); } update() { // รับค่าจากการเรียกใช้เมธอด const count = this extcomponent getcount(); if (count > 10) { // ตั้งค่าโดยการเรียกใช้เมธอด this extcomponent setcount(0); } // รับคุณสมบัติสาธารณะ const resultstring = this extcomponent stringproperty; // พิมพ์ผลลัพธ์ console log(`result ${resultstring}`); this resultui text = resultstring; } } ตัวอย่างโค้ดสำหรับ extracomponents import { zepetoscriptbehaviour } from 'zepeto script'; export default class extracomponent extends zepetoscriptbehaviour { public stringproperty string; private message string; private count int; start() { this message = "สวัสดี zepeto!"; this count = 0; } update() { this stringproperty = `${this message} ${this count++}`; } getcount() { return this count; } setcount(newcount int) { this count = newcount; } } ตรวจสอบหน้าจอผลลัพธ์ของโค้ดทดสอบด้านล่าง