CREATE YOUR WORLD
Scripting
GameObject와 Component
13min
세상에서는 객체를 생성하거나 특정 구성 요소를 수정하거나 추가하는 것이 매우 일반적입니다 zepetoscript는 unity 스크립트에서 사용되는 것과 동일한 인터페이스를 지원합니다 gameobject 및 component의 기본 예제 게임 객체를 생성하고 제거하기 gameobject // gameobject 생성 const tempobj = new gameobject(); const obj = object instantiate(tempobj); // gameobject 파괴 object destroy(obj); gameobjects의 구성 요소를 가져오거나 새로 추가하려면 getcomponent 또는 addcomponent를 사용하세요 component // 제네릭을 사용한 getcomponent const mytransform = this getcomponent\<transform>(); // 제네릭을 사용한 addcomponent 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초 후 객체 파괴 this startcoroutine(this destroyobject()); } destroyobject() { yield new waitforseconds(5); // gameobject 파괴 object destroy(this testobject); } } find 활용하기 find 관련 메서드는 unity 스크립트와 동일한 인터페이스 스타일을 zepetoscript에서도 지원합니다 메서드 설명 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() { // 지정된 이름을 가진 대상 gameobject를 찾습니다 // 동일한 이름을 가진 여러 gameobject가 있는 경우, 처음 발견된 것이 반환됩니다 this tempobj1 = gameobject find("cube"); if(this tempobj1 != null) { console log("gameobject 찾기 성공"); } // 지정된 태그를 가진 대상 gameobject를 찾습니다 // 동일한 태그를 가진 여러 gameobject가 있는 경우, 처음 발견된 것이 반환됩니다 this tempobj2 = gameobject findgameobjectwithtag("3d"); if(this tempobj2 != null) { console log("findgameobjectwithtag 성공"); } // 지정된 태그를 가진 gameobject의 배열을 반환합니다 this tempobj3 = gameobject findgameobjectswithtag("3d"); if(this tempobj3 != null) { console log("findgameobjectswithtag 성공"); } } } findsample 객체에 스크립트를 추가하면 검사기가 비어 있습니다 재생 버튼을 눌러 실행하면 검사기 창에서 각 객체가 할당되었는지 확인할 수 있으며, 콘솔 창을 통해 객체 찾기의 성공 여부를 확인할 수 있습니다 프리팹 만들기 런타임 동안 프리팹을 만드는 방법을 탐색해 보겠습니다 먼저, 3d 오브젝트 > 큐브를 씬에 추가합니다 큐브의 인스펙터에서 구성 요소 추가를 누르고 rigidbody를 추가합니다 그런 다음 큐브를 프로젝트 영역으로 드래그하여 프리팹으로 만듭니다 프리팹을 사용할 것이므로 씬에서 큐브를 삭제합니다 아래 스크립트를 작성해 주세요 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 편집기로 돌아가서 프리팹을 스크립트 검사기로 드래그하여 추가합니다 재생 버튼을 눌러 3d 객체가 생성되고 매초 위에서 떨어지는지 확인합니다 아래 링크를 클릭하여 zepetoscript에서 제공하는 unity gameobjects 및 components에 대해 자세히 알아보세요 📘 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 📘 컴포넌트 사용하기 https //docs unity3d com/kr/current/manual/usingcomponents html https //docs unity3d com/kr/current/manual/usingcomponents html