Invoke
Invoke(methodName: string, time: float): void;
methodName:方法名
time:多少秒后執(zhí)行
InvokeRepeating
InvokeRepeating(methodName: string, time: float, repeatRate: float): void;
methodName:方法名
time:多少秒后執(zhí)行
repeatRate:重復(fù)執(zhí)行間隔
還有兩個(gè)重要的方法:
IsInvoking:用來判斷某方法是否被延時(shí)嵌戈,即將執(zhí)行
CancelInvoke:取消該腳本上的所有延時(shí)方法
using UnityEngine;
using System.Collections;
public class DelayScript : MonoBehaviour {
//當(dāng)前時(shí)間
private float nowTime;
//執(zhí)行重復(fù)方法的次數(shù)
private int count;
// Use this for initialization
void Start () {
nowTime = Time.time;
Debug.Log("時(shí)間點(diǎn):"+nowTime);
this.Invoke("setTimeOut", 3.0f);
this.InvokeRepeating("setInterval", 2.0f, 1.0f);
}
private void setTimeOut()
{
nowTime = Time.time;
Debug.Log("執(zhí)行延時(shí)方法:" + nowTime);
}
private void setInterval()
{
nowTime = Time.time;
Debug.Log("執(zhí)行重復(fù)方法:" + nowTime);
count += 1;
if(count==5)
this.CancelInvoke();
}
}
[緣矗钟,妙不可言]