유니티 하이라키창에 빈 게임 오브젝트를 만든 후 다음의 스크립트를 추가한다.



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class CubeControl : MonoBehaviour {


// Use this for initialization

void Start () {

                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);    //큐브 오브젝트 생성

cube.transform.position = new Vector3(0, 0, 0);                                 //큐브 포지션 설정

}

// Update is called once per frame

void Update () {

}

}




실행시켜보면 0,0,0좌표에 큐브가 생성된 것을 확인할 수 있다.


게임오브젝트를 큐브뿐만아니라 GameObject.CreatePrimitive(PrimitiveType.Cube); 에서 Cube를 Plane, Sphere, Capsule, Cylinder 등으로 바꾸면 여러가지 


오브젝트로 실행이 가능하다.


그럼 조금 더 업그레이드 해서 큐브를 무한대로 생성시켜보자.



using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class CubeControl : MonoBehaviour {

int i = 0;

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

i=i+5;

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

cube.transform.position = new Vector3(i, 0, 0);

}

}


이렇게되면 업데이트 메서드가 호출될때마다 x축으로 5의 간격으로 큐브가 생성될 것이다.




 



+ Recent posts