在Unity中,每1秒仅将对动作的反应限制为一次? [英] In Unity, limit reaction to an action only once every 1 second?
本文介绍了在Unity中,每1秒仅将对动作的反应限制为一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Unity3D的Scene
中,如何让代码在运行循环的MonoBehaviour
中每秒仅对动作做出一次反应?
In a Scene
in Unity3D, how can I make code react to an action only once every one second in a MonoBehaviour
in the runloop?
推荐答案
If you're in a Unity3D environment, stop reading and look at Joe Blow's answer. Otherwise, continue reading.
您可以使用 Stopwatch
为您的活动计时.创建一个 Stopwatch
作为私有字段/属性,并通过构造函数对其进行初始化:
You could use a Stopwatch
to time your events. Create one Stopwatch
as a private field/property and initialize it from your constructor:
public YourClass()
{
ScoreStopwatch = new Stopwatch();
ScoreStopwatch.Start();
// Other initialization...
}
private Stopwatch ScoreStopwatch { get; set; }
Then you can use the Elapsed
property to get the time since your last score increase, like this:
if(Input.GetMouseButtonUp(0) && ScoreStopwatch.Elapsed.TotalSeconds > 1)
{
score++;
ScoreStopwatch.Reset();
ScoreStopwatch.Start();
}
这篇关于在Unity中,每1秒仅将对动作的反应限制为一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文