유니티 코루틴(Coroutine)

이미지
리턴형으로 IEnumerater를 사용한다 yield return을 사용해서 IEnumerater함수에서 빠져나올 수 있다. 또한 WaitForseconds함수를 사용하여 매개값으로 주어진 시간이 지난 후에 다시 복귀하게 할 수 있다. public class CRTest : MonoBehaviour { private void Start() { StartCoroutine("TestCoroutine"); } IEnumerator TestCoroutine() { Debug.Log("코루틴 시작"); yield return new WaitForSeconds(3.0f); Debug.Log("3초 후 빠져나감"); } } 특정 오브젝트를 페이드 아웃 시키는 예제 public class FadeOut : MonoBehaviour { private SpriteRenderer spriteRenderer; void Start () { spriteRenderer = GetComponent (); } void Update () { Color color = spriteRenderer.color; if (color.a > 0.0f) { color.a -= 0.1f; spriteRenderer.color = color; } } } 특정 오브젝트를 1초동안 페이드아웃 public class FadeOut : MonoBehaviour { private SpriteRenderer spriteRenderer; void Start () { spriteRenderer = GetComponent (); StartCoroutine ("RunFadeOut"); } void Update () { } IEnumerator RunFad...

유니티 싱글톤

이미지
public class Singleton: MonoBehaviour { public static Singleton Instance; //Awake is always called before any Start functions void Awake() { if (Instance) { DestroyImmediate(gameObject); } else { Instance = this; DontDestroyOnLoad(gameObject); } } }

유니티 씬 이동

using UnityEngine.SceneManagement; public class MoveScene: MonoBehaviour { public void GotoScene() { SceneManager.LoadScene("SceneName"); } }