Sun Guangdong 2015.4.9 24:00
Let's take a look at the role of the use of the association a total of two points:
1) delay (wait) a period of time to execute code;
2) Wait for an operation to complete before executing the subsequent code. Summing up is a sentence: control code at a specific time to execute.
The process is not a thread, nor is it executed asynchronously. The Monobehaviour and the update function are also executed in Mainthread. Using the coprocessor you don't have to worry about syncing and locking issues.
It is not recommended to use the process to bring GC problems!.
Using the game I previously encapsulated simple control logic clock class can be perfectly solved
IEnumerator Myawesomecoroutine () { while (true) { doawesomestuff (); Yield return new waitforseconds (waitTime);
What I want to point out is that using "yield return new Waitforseconds ()" will cause the garbage allocation gc,21 bytes per frame, because the "new" section (relative to the standard coprocessor "yield return null" yields only 9 bytes).
To avoid this problem, just set your wait wait time in advance ...
Waitforseconds shortwait = new Waitforseconds (0.1f); Waitforseconds longwait = new Waitforseconds (5.0f), IEnumerator Myevenawesomercoroutine () {while (true) { if (ineedtodostufffast) { doawesomestuffreallyfast (); Yield return shortwait; } else{ Dontdomuch (); Yield return longwait;}} }
Now you coroutine each call will only cause the lowest 9 bytes of GC allocations (not including other allocations allocations, of course you may be led through your other code!). )。
The list of things you can do to prevent GC may be longer.
-Do not use the string invoke or Startcoroutine.
-Do not use Guilayout and mark your GUI Monobehaviour to prevent a GC of 800bytes per frame from occurring. Http://docs.unity3d.com/ScriptReference/MonoBehaviour-useGUILayout.html
-Do not use Gameobject.tag or gameobject.name
-Do not use getcomponent in update, cache it if possible
-Do not use foreach
-Do not use string +----"Stringbuild or String. Format ()
??
C # coprocessor Waitforseconds generating GC (garbage Collection) problem