반응형
- 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
private void Start()
{
StartCoroutine(DelayCoroution());
Invoke("Delay", 5);
}
IEnumerator DelayCoroution()
{
yield return new WaitForSeconds(5);
Debug.Log("5초가 지났습니다.");
}
void Delay()
{
Debug.Log("5초가 지났습니다.");
}
}
- 다른 방식으로 코루틴 사용 예시
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public int playerHp = 100;
private void Start()
{
StartCoroutine(HpCoroution());
}
IEnumerator HpCoroution()
{
if(playerHp <= 0)
{
Debug.Log("플레이어 사망");
yield break;
}
else
{
playerHp -= 1;
Debug.Log("남은 체력 : " + playerHp);
}
yield return new WaitForSeconds(1);
StartCoroutine(HpCoroution());
}
}
참고할만한 글
반응형