반응형
- 코드 작성
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());
}
}
참고할만한 글
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 이벤트 델리게이트 delegate Event 간단 사용법 (0) | 2022.06.21 |
---|---|
유니티 C# 로컬 데이터 저장, 불러오기 PlayerPrefs 간단 사용법 (0) | 2022.06.21 |
유니티 C# 타이머 만들기 시분초 00:00:00 Timer (0) | 2022.06.21 |
유니티 C# 충돌 처리 OnTrigger Enter, Stay, Exit 간단 사용법 (0) | 2022.06.21 |
유니티 C# 씬 로드 동기, 비동기 간단 사용법 Load Scene Async (0) | 2022.06.21 |
유니티 C# 랜덤 난수 생성 Random.Range 간단 사용법 (0) | 2022.06.21 |
유니티 C# 특수문자 제한 간단 사용법 input text (0) | 2022.06.21 |
유니티 C# 캐릭터 키보드 이동 간단 구현 (0) | 2022.06.20 |
댓글