- Home /
Blinking & resource consumption
Hi,
I am coding some "blinking" stuff, which I can find many threads here, but any refering to the efficiency of those methods. So the too main options are:
i) Use yield WaitForSeconds() ii) Use a code I found in this thread, checking if it has passed enough time in the Update() function
var rate = .5; // seconds
var lastCheck = 0.0;
Update(){
if ((Time.time - lastCheck) >= rate)
{
Do Something
lastCheck = Time.time;
}
}
Even I saw an infinite loop placed in the Start() function, which I don't know how effiencient is that.
So, my question is, what is more efficient?
Other question I want to ask is about the 'yield' instruction. If i call yield WaitForSeconds() the execution thread is paused, isn't it?
And to make all my picture about 'threads' and 'yields' clear, there is one thread (per script) executing its script, isn't it?
Thanks in advance
Answer by Mike 3 · Mar 17, 2011 at 02:24 PM
There is one thread total (when it comes to scripts) - each script is run sequentially.
Yielding is done by giving back execution after each yield to unity, and then the engine queues it up for calling back again - it is completely non blocking.
I would tend to use InvokeRepeating for your above example, it's called in a way which keeps the function calls equal length, doing it with WaitForSeconds in a loop will slowly go out of sync (as it'll usually be called after seconds + <= frametime)
There wouldn't be much difference between your update example and a coroutine, though coroutines have the benefit of being able to be stopped, which will be a benefit over Update which has to keep checking conditions
Thank you very much $$anonymous$$ike for your fast, accuarate and professional answer. Tremendous help, I tried to vote you up more than once but it didn't work ;)
Hi, Unfortunately for GUI functions InvokeRepeating is not an option since GUI functions must be called inside OnGUI()
Set a bool in the invoked function, check if that bool is true or false in OnGUI