Unity
코루틴 내용 기록
박도치
2024. 1. 7. 23:15
유니티에서 코루틴을 통해 시간을 조절하여 다양한 효과를 줄 수 있다.
호출 및 정지
호출 방법으로는 StartCoroutine() 에 함수를 호출하여 시작하면 된다.
StartCoroutine(코루틴 함수());
멈출때는 StopCoroutine()과 StopAllCoroutine()이 있는데 둘 은 차이가 있다.
StopCoroutine("함수명 String");
StopAllCoroutine(코루틴 함수());
StopCoroutine은 왜인지는 모르겠지만 string 문자열로 함수를 호출해야 가능하다.
자주 사용되는 코루틴 정리
1. return null
IEnumerator _Coroutine()
{
yield return null;
}
2. yield return new WaitForEndOfFrame()
프로그램에서 한 프레임워크가 완전히 종료되었을 때 호출이 된다.
IEnumerator _Coroutine()
{
yield return new WaitForEndOfFrame()
// 실행
}
3. yield return new WaitForFixedUpdate()
FixedUpdate()가 끝나면 실행됨
IEnumerator _Coroutine()
{
yield return new WaitForFixedUpdate()
// 실행
}
4. yield return new WaitForSecond(2f)
2초 후에 코드가 실행 됨
IEnumerator _Coroutine()
{
yield return new WaitForSecond(2f)
// 실행
}
5. yield return new WaitForSecondRealtime(2f)
위와 비슷한 느낌이지만 Realtime은 절대적인 시간을 나타내는 것이기 때문에 Time.timeScale의 영향을 받지 않는다.
IEnumerator _Coroutine()
{
yield return new WaitForSecondRealtime(2f)
// 실행
}