'유니티3d'에 해당되는 글 2건

  1. 2011.05.27 일전에 발표했던 Unity3D세미나 내용
  2. 2011.03.16 유니티3D 슬슬 관심가는데!
Unity3D2011. 5. 27. 10:20


[ 게임오브젝트와 이를 눈에 보이게 하기 위해 Attach된 4개의 클래스 ]
GameObject -> Object

유니티 모든 개체의 형태를 갖는 기본 클래스 : 아래의 컴퍼넌트를 갖는다.

Create Empty의 경우 Transform만 생성된다.

 

1 Transform -> Component

위치, 회전, 크기를 다루는 객체

 

2 MeshFilter -> Component

눈에 보이는 모형을 설정

 

3 Collider -> Component

모든 객체의 기본(최소) 입자,물체 단위

게임오브젝트의 물리적 영역을 결정

 

4 MeshRenderer -> Renderer

물체 표면에 렌더링하는 객체


 

[ Display와 무관한 물리적 작용과 동작(Event), 화면출력에 관여한 클래스 ]

-------------------------------------------------------------------------------------------

Component -> Object

GameObject attach할 수 있는 기본 클래스

대부분의 클래스가 Component를 취하고 있다.

-------------------------------------------------------------------------------------------

Behaviour -> Component

게임오브젝트가 동작할 여부를 활성 비활성 시키는 구성요소

변수로는 enabled만 선언되어있다.

 

MonoBehaviour -> Behaviour

모든 스크립트에서 파생되는 기본클래스

Start(), Awake(), Update(), FixedUpdate(), and OnGUI()등을 실행으로 스크립트로 객체를 제어한다.

-------------------------------------------------------------------------------------------

Rigidbody -> Component

물리 시뮬레이션을 통한 객체들의 위치제어

 

Camera -> Behaviour

플레이어에게 화면을 보여주는 장치

 

Light -> Behaviour

조명 효과를 내기 위한 구성요소의 스크립트 인터페이스


 

[ 유니티에서 출력]

 

********** PrintAndDebug

private var num = 1;

function Update () {

 

1 유니티 지원

Debug.Log("*********************************");


2자바스크립트에서 지원

print("return num :" + runCount() ); }

 

 

private function runCount():int

{

num++;

print("num with print : "+ num);

Debug.Log("num with Debug :" + num);

print("childCount :" + transform.childCount);

return num;

}

 

 

[ 변형1 ]

 

********** TranslateScript

public var speed:int = 0;

public function Update () {

변화가 없으면 실행되지 않는다.

print("myStaticVariable :" + RotateScript.myStaticVariable);

                                                                         

if( this.transform.position.x > 5 ){

speed = -1* speed;

}else if( this.transform.position.x < -5){

speed = -1* speed;

}

var nTmp:float = Time.deltaTime * speed;

                                        

 

//Translate( x , y, z );

this.transform.Translate( nTmp , 0, 0 );

}

 

 

********** RotateScript

public var speed:int = 0;

// Time.deltaTime사용여부

public var isDepandFrame:boolean = false;

public static var  myStaticVariable:int = 100;

 

function Update () {

if(isDepandFrame == true){

this.transform.Rotate(0,Time.deltaTime * speed,0);

}else{

this.transform.Rotate(0,speed,0);

}

}

 

[ 변형2 ]

 

********** LookAtScript  : Transform으로 제어
public var target : Transform;

public function Update() {

LookAt은 설정된 객체를 바라본다.

this.transform.LookAt(target);

}

 

 

 

********** LookAtScript  : GameObject 으로 제어

public var target2 : GameObject;

public function Update() {

if( target2 != null ){

Debug.Log("target2 :"+target2);

Debug.Log("target2 :"+target2.transform);

this.transform.LookAt(target2.transform);

}

}

 

컴퍼넌트 검색 ]

GetComponent는 객체에 attach된 컴퍼넌트로 접근한다.

attach를 포함하여 이 객체(Cube)는 한 개의 클래스로 간주한다.

GetComponent는 클래스에서 컴퍼넌트 조회에 사용되는 것이다.

 

>>>>목표

1 자신의 transform컴퍼넌트에 직접 접근하여 위치를 조정한다.

2 GetComponent를 사용하여 자신의 transform컴퍼넌트에 접근하여 회전한다.

 

********** GetComponentScript1

print("run GetComponentScript1");

this.transform.Translate(0, -1, 0);

this.GetComponent(Transform).Rotate(0, 50, 0);

 

 

>>>>목표

동일한 게임오브젝트(Cube)attach  다른 스크립트 또는 builtin 컴포넌트들을 찾을 수 있다.

큐브에 attach myOtherScript에 함수 myFunction()를 실행시킨다.

myFunction()의 리턴값은 int 100이다.

 

********** myOtherScript : attach된 스크립트, 실행할 함수 부분

public function myFunction():int

{

print("run myFunction");

return 100;

};

 

********** GetComponentScript2

function Update () {

print("run GetComponentScript2");

otherScript = GetComponent(myOtherScript);

print("return from otherScript.myFunction : " + otherScript.myFunction());

}


 

 

스크립트 컴퍼넌트 검색 ]

외부의 클래스에 선언된 변수에 접근하기 위해서는 static으로 선언되어있어야 한다.

아래의 변수 var01~var03까지는 variablesScript static으로 선언되었고 variablesScript는 어디에도 attach되지 않았다. 이 컴퍼넌트는 Cube attach되어있고 ownerScript에 의해 runMyFunc()가 실행된다.  리턴 값으로는 전달된 num의 값에 100을 더하여 건낸다.

 

>>>>목표

1 GetComponent()를 사용하여 외부 스크립트를 link

 

********** variablesScript : 변수만 선언된 스크립트, attach되지 않았다. 따라서 public static으로 선언

public static var var01:int = 100;

public static var var02:String = "String";

public static var var03:boolean = false;

 

 

********** myScript1 : 위부에 선언된 변수 접근 및 연산

public function runMyFunc( num:int ):int

{

print( "variablesScript.var01 :"+ variablesScript.var01);

print( "variablesScript.var02 :"+ variablesScript.var02);

print( "variablesScript.var03 :"+ variablesScript.var03);

return variablesScript.var01+num;

}

 

********** ownerScript : Cube attach myScript1 GetComponent()으로  otherScript link한다.

public var num:int = 0;

public function Update () {

var otherScript = GetComponent( myScript1 );

print( otherScript.runMyFunc(num) );

}

 

 


 

[ 게임오브젝트 검색 ]

 

inspector target을 설정하여 타겟 오브젝트에 회전을 가했다.

런타임 중 버튼을 클릭하여 target을 선택하도록 GameObject.Find()을 사용하여 객체를 검색 추출하였다.

 

********** runScript

public var target:Transform;

function Update () {

target.Rotate( 0, 1 , 0 );

print("target :"+target);

}

 

private var go:GameObject;

function OnGUI () {

 

aText = "Select Cube";

GUI.Label( Rect(10, 10, 300, 200), aText );

if( GUI.Button( Rect(10, 60, 200, 50), "Select Left Cube") ){

 go = GameObject.Find("Cube1");

target = go.transform;

};

 

if( GUI.Button( Rect(250, 60, 200, 50), "Select Right Cube") ){

go = GameObject.Find("Cube2");

target = go.transform;

};

}

 

 

키입력 체크 ]

 

********** InputTestScript

public  function OnGUI():void

{

print("OnGUI");

                                        

//키보드키 검색

if( Input.GetKeyDown( KeyCode.W ) ){

 

guiText.text = "You pressed the 'W' key";

}else if( Input.GetKeyDown( KeyCode.A ) ){

guiText.text = "You pressed the 'A' key";

}else if( Input.GetKeyDown( KeyCode.S ) ){

guiText.text = "You pressed the 'S' key";

}else if( Input.GetKeyDown( KeyCode.D ) ){

guiText.text = "You pressed the 'D' key";

}else if( Input.GetKeyDown( KeyCode.Space ) ){

guiText.text = "You pressed the 'Space ' key";

}

 

//마우스 버튼 검색

if( Input.GetMouseButton( 0 ) ){

guiText.text = "You pressed left click";

}else if( Input.GetMouseButton( 1 ) ){

guiText.text = "You pressed right click";

}else if( Input.GetMouseButton( 2 ) ){

guiText.text = "You pressed middle click";

}

}

 

 

이상 얼마전에 발표한 유니티3D 개발 1차 세미나 내용이였습니다~

Posted by 버터백통
Unity3D2011. 3. 16. 10:51

Posted by 버터백통