Coroutine

JunDev·2025년 3월 11일

Unity

목록 보기
7/8

A Coroutine in Unity is a special function that allows you to pause execution and resume it later, making it perfect for delaying actions or performing asynchronous tasks like waiting for a few seconds, playing animations, or handling AI behavior.

A Coroutine allows you to execute code over multiple frames instead of all at once. This is useful when you want to wait for something without blocking the rest of the game.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(MyCoroutine()); // Start the coroutine
    }

    IEnumerator MyCoroutine()
    {
        Debug.Log("Coroutine started");
        
        yield return new WaitForSeconds(3f); // Waits for 3 seconds
        
        Debug.Log("3 seconds passed!"); // Executes after the wait time
    }
}
profile
Jun's Dev Journey

0개의 댓글