CREATE YOUR WORLD
Scripting

$ref & $unref

5min
그 ref 및 out 키워드는 c#에서 변수를 메서드에 전달할 때 참조를 전달하는 데 사용됩니다 typescript에서는 이러한 키워드를 사용할 수 없지만, zepetoscript에서는 다음을 사용하여 유사한 기능을 구현할 수 있습니다 $ref 및 $unref 함수 설명 $ref(x?\ t) $ref 매개변수에 대한 참조를 생성합니다 $unref(x $ref)\ t 매개변수의 참조를 해제하고 원래 값을 반환합니다 예를 들어, 다음과 같이 장면을 설정해 보겠습니다 장면에 3d 객체인 큐브를 추가합니다 추가된 큐브의 검사기에서 "구성 요소 추가"를 클릭하고 rigidbody를 추가합니다 장면에 빈 gameobject 추가하기 아래의 refsample 스크립트를 작성하고 3단계에서 생성한 gameobject에 추가하세요 import { gameobject, rigidbody } from 'unityengine' import { zepetoscriptbehaviour } from 'zepeto script' import testscript from ' /testscript'; export default class refsample extends zepetoscriptbehaviour { start() { // "cube"라는 이름의 gameobject 가져오기 const testobject = gameobject find("cube"); // "cube" 객체에서 rigidbody 컴포넌트 가져오기 const testcomponent = gameobject find("cube") getcomponent\<rigidbody>(); // "cube" 객체에서 testscript 컴포넌트 가져오기 const testscript = gameobject find("cube") getcomponent\<testscript>(); // 컴포넌트와 객체에 대한 참조 생성하기 let tempobj = $ref(testobject); let tempcomponent = $ref(testcomponent); let tempscript = $ref(testscript); // rigidbody 컴포넌트가 null이 아닌지 확인하기 if(tempcomponent != null) { // 해당 참조의 실제 값에 접근하기 let component = $unref(tempcomponent); console log(`컴포넌트 이름 ${component}`); } // testscript 컴포넌트가 null이 아닌지 확인하기 if(tempscript != null) { // 해당 참조의 실제 값에 접근하기 let script = $unref(tempscript); script dotest(); } // gameobject 참조가 null이 아닌지 확인하기 if(tempobj != null) { // 해당 참조의 실제 값에 접근하기 let objname = $unref(tempobj); console log(`tempobj 이름 ${objname name}`); } } } 다른 zepetoscript를 생성하고 아래의 testscript를 작성하세요 import { zepetoscriptbehaviour } from 'zepeto script' export default class testscript extends zepetoscriptbehaviour { dotest() { console log(`testscript의 dotest()가 실행되었습니다 `); } } testscript를 cube의 inspector에 드래그하여 연결하세요 결과를 관찰하기 위해 \[▶︎(재생)] 버튼을 클릭하세요 콘솔 로그 창에서 객체 참조, rigidbody 구성 요소 및 zepetoscript 구성 요소의 성공 여부를 확인할 수 있습니다