Unity c# for designer 3강 / 제어문

C#이 보이는 그림책을 참고로 작성했습니다.

강의용 자료로 만들었기 때문에 구체적인 설명은 없습니다.



오늘은 다양한 조건문을 배우고 유니티 API를 이용해

오브젝트를 간단히 움직여 보겠습니다.

NGUI도 간단히 사용해 보겠습니다.




유니티 스크립트를 시작하는데 꼭 필요한 자료 입니다.

즐찾 하세요.~ ^^


Unity 스크립트 자습서

http://unity3d.com/kr/learn/tutorials/topics/scripting




Unity Script API

https://docs.unity3d.com/ScriptReference/30_search.html?q=Translate




1. if 문을 이용한 오브젝트 이동과 UI Label

using UnityEngine;

using System.Collections;


public class if001 : MonoBehaviour {


public bool go;

public UILabel uiText;

public float speed;


void Start () {

}

void Update () {

if (go == true) {

this.gameObject.transform.Translate (Vector3.right * Time.deltaTime * speed);

uiText.GetComponent<UILabel>().text = "right!";

} else {

this.gameObject.transform.Translate (Vector3.left * Time.deltaTime * speed);

uiText.GetComponent<UILabel>().text = "left!";

}

}

}





2. for문을 이용한 연산과 UI Label



using UnityEngine;

using System.Collections;


public class for001 : MonoBehaviour {


public UILabel uiText;

public int count;


void Start () {

for(int i = 0; i <= count; i++){

Debug.Log ("i = "+i);

Debug.Log ("count = "+count);


if(i == 10){

uiText.GetComponent<UILabel> ().text = "finish!!";

}

}

}

void Update () {

}

}






3. foreach문을 이용한 UI Label 문자 바꾸기



using UnityEngine;

using System.Collections;


public class foreach001 : MonoBehaviour {


public UILabel[] TT = new UILabel[3];

public string text = "Oh!!";



// Use this for initialization

void Start () {


TT [0] = GameObject.Find ("Label_1").GetComponent<UILabel>();

TT [1] = GameObject.Find ("Label_2").GetComponent<UILabel>();

TT [2] = GameObject.Find ("Label_3").GetComponent<UILabel>();


foreach( UILabel result in TT){

result.text = text;

}

}

// Update is called once per frame

void Update () {

}

}






4. while문을 이용한 UI Label 문자 바꾸기



using UnityEngine;

using System.Collections;


public class while001 : MonoBehaviour {


public int count;

public int whileCount;

public UILabel uiText;


void Start () {


while(whileCount < count){

Debug.Log (whileCount);

whileCount++;

}


uiText.GetComponent<UILabel> ().text = whileCount.ToString ();

}

void Update () {

}

}





5. switch문으로 오브젝트 제어하기


using UnityEngine;

using System.Collections;


public class swirch001 : MonoBehaviour {


public Move moveState;


public GameObject obj;

public float speed;

public UILabel uiText;


public enum Move {

go, stop

}


// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {


switch(moveState){

case Move.go:

obj.transform.Translate (Vector3.right * Time.deltaTime * speed);

uiText.GetComponent<UILabel>().text = "GO!";

break;

case Move.stop:

Debug.Log ("STOP!");


break;

}


}

}









과제

이번 과제는 복습입니다.

복습 복습 복습~



Posted by 프리랜서 디자이너