'as'에 해당되는 글 1건

  1. 2018.06.08 [유니티] Instantiate 뒤에 as GameObject를 붙이는 이유

Instantiate로 생성된 게임오브젝트는 한마디로 복제품(clone)이다.

이러한 클론들은 프로그램이 Play 도중에는 Hierarchy에 생성이 되었다가 종료하면 사라지 일회성을 갖는다. 

복제품들의 위치정보나 충돌처리를 위해서는 복제품의 Component에 접근을 해야 되는데 이것을 위해 사용하는 것이 바로 as GameObject라던지 as Transform 등이다.


GameObject go = Instantiate(playerprefab, container.transform) as GameObject;
go.GetComponentInChildren<Text>().text = data;


이렇게 하면 clone이 생성돼었을 때 clone의 Component에 접근이 가능하고,

GetComponent를 이용해서 Transform에 접근하거나 다른 Component를 제어할 수 있다. 



Posted by sungho88
,