1 回答

TA貢獻(xiàn)1840條經(jīng)驗 獲得超5個贊
在Unity中一定時間間隔后重復(fù)執(zhí)行代碼
使用Update()方法:
// Invoke the method after interval seconds
public float interval = 0.1f;
// time counter
float elapsed = 0f;
void Update()
{
elapsed += Time.deltaTime;
// if time is elapsed, reset the time counter and call the method.
if (elapsed >= interval)
{
elapsed = 0;
TakeShot();
}
}
void TakeShot()
{
// do your thing here...
}
使用InvokeRepeating()方法:
// Invoke the method after interval seconds
public float interval = 0.1f;
float delaySeconds = 0f; // delay the first call by seconds
void Start()
{
InvokeRepeating("TakeShot", delaySeconds, interval);
}
void TakeShot()
{
// do your thing here...
}
注意:這兩種方法都是framerate和time-scale依賴的。
- 1 回答
- 0 關(guān)注
- 258 瀏覽
添加回答
舉報