Unity several ways to delay execution of the program

One, using the Invoke
Invoke method can easily delay the execution of the program, but it has certain limitations and can only be used for functions without parameters

void Updata()
{
    
    
	if(Input.GetKeyDown(KeyCode.A))
	{
    
    	
	Invoke("demo",2f);  //两秒后调用demo()函数
	}
}
void demo()
{
    
    
	Debug.log("This is a demo");
}

Second, use InvokeRepeatin

void Updata()
{
    
    
	if(Input.GetKeyDown(KeyCode.A))
	{
    
    	
	InvokeRepeating("demo", 3f,5); //每隔3秒调用demo(),一共调用5次
	}
}
void demo()
{
    
    
	Debug.log("This is a demo");
}

Guess you like

Origin blog.csdn.net/xinzhilinger/article/details/108412423
Recommended