코루틴용 데이터 |
엔진이 수행하는 기능 |
yield return null |
다음 프레임까지 대기 |
yield return new WaitForSeconds(float) |
지정된 초 만큼 대기 |
yield return new WaitForFixedUpdate() |
다음 물리 프레임까지 대기 |
yield return new WaitForEndOfFrame() |
모든 렌더링작업이 끝날 때까지 대기 |
yield return StartCoRoutine(string) |
다른 코루틴이 끝날 때까지 대기 |
yield return new WWW(string) |
웹 통신 작업이 끝날 때까지 대기 |
yield return new AsyncOperation |
비동기 작업이 끝날 때까지 대기 ( 씬로딩 ) |
public class UpdateTimer : MonoBehaviour { float accumulator = 0.0f; bool wait1Finished = false; bool wait2Finished = false; float waitTime1 = 1.0f; float waitTime2 = 2.0f; void Start() { Debug.Log ("Action Start!"); } void Update () { accumulator += Time.deltaTime; if(!wait1Finished && !wait2Finished) { if(accumulator >= waitTime1) { Debug.Log ("Action1 End"); wait1Finished = true; accumulator = 0.0f; } } else if(wait1Finished) { if(accumulator >= waitTime2) { Debug.Log ("Action2 End"); wait2Finished = true; accumulator = 0.0f; } } } } |
public class CoRoutineTimer : MonoBehaviour { float waitTime1 = 1.0f; float waitTime2 = 2.0f;
IEnumerator Start () { Debug.Log ("Action Start"); yield return new WaitForSeconds(waitTime1); Debug.Log ("Action1 End"); yield return new WaitForSeconds(waitTime2); Debug.Log ("Action2 End"); } }
|
public class WebBasic : MonoBehaviour {
public string url; WWW www;
bool isDownloading = false;
IEnumerator Start() { www = new WWW(url); isDownloading = true; yield return www; isDownloading = false; Debug.Log ("Download Completed!"); }
void Update() { if(isDownloading) Debug.Log (www.progress); }
} |
public class WebAdvanced : MonoBehaviour {
public string url; WWW www;
IEnumerator Start() { www = new WWW(url); StartCoroutine("CheckProgress"); yield return www; Debug.Log ("Download Completed!"); }
IEnumerator CheckProgress() { Debug.Log (www.progress); while(!www.isDone) { yield return new WaitForSeconds(0.5f); Debug.Log (www.progress); } }
} |
public class DialogExample : MonoBehaviour {
bool showDialog = false; string answer = "";
IEnumerator Start() { yield return StartCoroutine("ShowDialog"); yield return StartCoroutine(answer); }
IEnumerator ShowDialog() { showDialog = true; do { yield return null; } while(answer == "");
showDialog = false; }
IEnumerator ActionA() { Debug.Log ("Action A"); yield return new WaitForSeconds(1f); }
IEnumerator ActionB() { Debug.Log ("Action B"); yield return new WaitForSeconds(2f); }
void OnGUI() { if(showDialog) { if(GUI.Button(new Rect(10f, 10f, 100f, 20f), "A")) { answer = "ActionA"; } else if(GUI.Button(new Rect(10f, 50f, 100f, 20f), "B")) { answer = "ActionB"; } } }
} |
using UnityEngine; using System.Collections;
public class DialogExampleEnum : MonoBehaviour {
enum DialogActions { ShowDialog, ActionA, ActionB };
bool showDialog = false; DialogActions action = DialogActions.ShowDialog;
IEnumerator Start() { yield return StartCoroutine(action.ToString()); yield return StartCoroutine(action.ToString()); }
IEnumerator ShowDialog() { showDialog = true; do { yield return null; } while(action == DialogActions.ShowDialog);
showDialog = false; }
IEnumerator ActionA() { Debug.Log ("Action A"); yield return new WaitForSeconds(1f); }
IEnumerator ActionB() { Debug.Log ("Action B"); yield return new WaitForSeconds(2f); }
void OnGUI() { if(showDialog) { if(GUI.Button(new Rect(10f, 10f, 100f, 20f), "A")) { action = DialogActions.ActionA; } else if(GUI.Button(new Rect(10f, 50f, 100f, 20f), "B")) { action = DialogActions.ActionB; } } }
} |
'unity C#' 카테고리의 다른 글
[Unity] 리스트 재 정렬하기 list resorting (0) | 2015.10.26 |
---|---|
[Unity] C# Threading 사용하기 (0) | 2015.03.04 |
[Unity] RectTransform anchor 스크립트상에서 변경하기. (0) | 2015.02.14 |
[Unity] sublime 연동하기 (0) | 2015.02.11 |
[Unity] Resources.load text json 파일 불러오기 (0) | 2015.02.04 |