Unity makes a function has been called

In Unity, to keep a function being called, use a coroutine or the InvokeRepeating() method.

  1. Coroutines: A coroutine is an asynchronous operation that runs in Unity. Coroutines allow a function to be executed again after a certain period of time. The specific implementation is as follows:
IEnumerator MyFunction()
{
    
    
    while (true)
    {
    
    
        // 每隔一段时间执行一次
        yield return new WaitForSeconds(1f);
        // 执行你的操作
        Debug.Log("My function is running...");
    }
}

// 调用协程
StartCoroutine(MyFunction());
  1. InvokeRepeating() method: The InvokeRepeating() method allows a function to be executed repeatedly at regular intervals. The specific implementation is as follows:
void MyFunction()
{
    
    
    // 执行你的操作
    Debug.Log("My function is running...");
}

// 调用 InvokeRepeating() 方法,每隔 1 秒执行一次 MyFunction()
InvokeRepeating("MyFunction", 0f, 1f);

Guess you like

Origin blog.csdn.net/weixin_44919646/article/details/129638398