สร้างโลกของคุณเอง
การเขียนสคริปต์
$ref & $unref
5 นาที
the ref and out คำสำคัญใน c# ถูกใช้เมื่อส่งผ่านการอ้างอิงไปยังตัวแปรหรือโครงสร้างไปยังวิธีการ ใน typescript, คำสำคัญเหล่านี้ไม่มีให้ใช้, แต่ใน zepetoscript, คุณสามารถทำให้ฟังก์ชันการทำงานที่คล้ายกันได้โดยใช้ $ref และ $unref true left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type left unhandled content type สำหรับตัวอย่าง, มาตั้งค่า scene ดังนี้ เพิ่มวัตถุ 3d, ลูกบาศก์, ลงในฉาก ใน inspector สำหรับลูกบาศก์ที่เพิ่มเข้ามา ให้คลิก "add component" และเพิ่ม rigidbody เพิ่ม gameobject เปล่าในฉาก เขียนสคริปต์ refsample ด้านล่างและเพิ่มมันไปยัง gameobject ที่สร้างในขั้นตอนที่ 3 import { gameobject, rigidbody } from 'unityengine' import { zepetoscriptbehaviour } from 'zepeto script' import testscript from ' /testscript'; export default class refsample extends zepetoscriptbehaviour { start() { // get the gameobject named "cube" const testobject = gameobject find("cube"); // get the rigidbody component from the "cube" object const testcomponent = gameobject find("cube") getcomponent\<rigidbody>(); // get the testscript component from the "cube" object const testscript = gameobject find("cube") getcomponent\<testscript>(); // create references to the components and object let tempobj = $ref(testobject); let tempcomponent = $ref(testcomponent); let tempscript = $ref(testscript); // check if the rigidbody component is not null if(tempcomponent != null) { // access the actual value of that reference let component = $unref(tempcomponent); console log(`component name ${component}`); } // check if the testscript component is not null if(tempscript != null) { // access the actual value of that reference let script = $unref(tempscript); script dotest(); } // check if the gameobject reference is not null if(tempobj != null) { // access the actual value of that reference let objname = $unref(tempobj); console log(`tempobj name ${objname name}`); } } } สร้าง zepetoscript อีกอันและเขียน testscript ด้านล่าง import { zepetoscriptbehaviour } from 'zepeto script' export default class testscript extends zepetoscriptbehaviour { dotest() { console log(`testscript's dotest() ถูกเรียกใช้งาน `); } } ลากและเชื่อมต่อ testscript กับ inspector ของ cube คลิกที่ปุ่ม \[▶︎(เล่น)] เพื่อสังเกตผลลัพธ์ คุณสามารถตรวจสอบความสำเร็จของการอ้างอิงวัตถุ, คอมโพเนนต์ rigidbody, และคอมโพเนนต์ zepetoscript ในหน้าต่างบันทึกคอนโซล
