There are two built-in delay methods inside the monobehaviour
Invoke
123 |
Invoke(methodName: string , time: float ): void ; methodName:方法名 time:多少秒后执行 |
Invokerepeating
1234 |
InvokeRepeating(methodName: string , time: float , repeatRate: float ): void ; methodName:方法名 time:多少秒后执行 repeatRate:重复执行间隔 |
There are two important ways to do this:
- Isinvoking: Used to determine if a method is delayed and is about to be executed
- Cancelinvoke: Cancels all delay methods on the script
Here is the code note:
Using unityengine;using System.collections;public class Delayscript:monobehaviour { //Current time private float Nowtime; The number of times the repeating method was executed private int count; Use the this for initialization void Start () { nowtime = time.time; Debug.Log ("Time point:" +nowtime); This. Invoke ("SetTimeOut", 3.0f); This. Invokerepeating ("SetInterval", 2.0f, 1.0f); } private void SetTimeOut () { nowtime = time.time; Debug.Log ("Execution delay method:" + nowtime); } private void SetInterval () { nowtime = time.time; Debug.Log ("Execute repeating Method:" + nowtime); Count + = 1; if (count==5) this . Cancelinvoke (); }}
Unity Delay method Invoke and Invokerepeating