1 回答

TA贡献1840条经验 获得超5个赞
在Unity中一定时间间隔后重复执行代码
使用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 关注
- 241 浏览
添加回答
举报