Quản lý hành động theo thời gian với Coroutine
C# Code
using UnityEngine;
using System.Collections;
public class CoroutineExample : MonoBehaviour
{
void Start()
{
Debug.Log("Hành động sẽ bắt đầu sau 2 giây.");
StartCoroutine(DelayedAction(2f));
}
private IEnumerator DelayedAction(float delay)
{
// Dừng thực thi của hàm này trong 'delay' giây
yield return new WaitForSeconds(delay);
// Code bên dưới sẽ chạy sau khi chờ xong
Debug.Log("Đã 2 giây trôi qua!");
GetComponent<Renderer>().material.color = Color.blue;
}
}Một ví dụ cơ bản về cách sử dụng Coroutine để thực hiện một hành động sau một khoảng thời gian chờ, mà không làm đóng băng game.