0x00 Preface
In the process of using unity, the association only knows how to use, but does not know the internal mechanism of the process, for its unclear part is like a big stone pressure heart, let oneself feel worry and discomfort. This article to explore, thoroughly uncover the veil of the process, so that everyone in use no longer have to worry about.
0x01 concept
The process is: program components to generate non-preemptive multi-task sub-functions, the resulting sub-function allows to suspend and wake operations in the program.
0x02 Usage Scenarios
It is often possible to implement delay operations and asynchronous load operations with ease. Here are two examples of simple process use.
Delay operation
// Use this for initializationvoidStart () {Startcoroutine (Wait ());} IEnumerator Wait () {Debug.Log("Start time:" +Time.Time);yield return NewWaitforseconds (1); Debug.Log("Time:" +Time.Time);yield return NewWaitforseconds (2); Debug.Log("Time:" +Time.time);}
Loading resources asynchronously
// Use this for initializationvoidStart () {; System.Action<string>CallBack=DelegatestringText) {Debug.Log(text); }; Startcoroutine (Loadres (CallBack));} IEnumerator Loadres (System.Action<string>callBack) {www www= NewWWW ("Http://www.baidu.com");yield returnwwwif(string.IsNullOrEmpty (www.Error) {CallBack (www.Text); Debug.Log("Load Success"); }Else{Debug.Log("Load Failed"); }}
0x03 principle
The co-process in unity defines a function that returns the IEnumerator type by first using a function to see if unity can return those types:
IEnumerator Test () {yield return 2;//Return integer yield return 4.2;//Return floating-point number yield return NULL;//return null yield return NewWaitforseconds (1);//Return to instance yield return NewWWW ("Http://www.baidu.com");//Return to instance}
What are the requirements for the returned types? Tidying up unity implements those return types:
1, int type, number of frames to wait
2, float type, the time to wait (in seconds)
3, NULL, waiting for a frame
4, break, end of the co-process
5, instance, must have bool IsDone () member function, etc. isDone return True
6, IEnumerator, etc IEnumerator instance of MoveNext () return False
The return type of unity knows, how are these return types captured? See how IEnumerator is implemented?
publicinterface IEnumerator{ // // Properties // object Current { get; } // // Methods // bool MoveNext (); void Reset ();}
By studying the IEnumerator interface, we get the value returned by calling MoveNext, which we can get through all yield returns, which can be obtained by current. Each call to MoveNext executes the code sandwiched between yield. Write a test program to verify our theory:
Public classGame_client:monobehaviour {// Use this for initialization voidStart () {IEnumerator i = Test (); while(true) {if(!i.movenext ()) { Break; }Objectcur = i.current;if(cur! =NULL) Debug.Log (cur. GetType ());ElseDebug.Log ("type is null"); }} IEnumerator Test () {yield return 2;yield return 4.2;yield return NULL;yield return NewWaitforseconds (1);yield return NewWWW ("Http://www.baidu.com"); }}
By validating the program, you can get the value returned by yield, and with these values, you can implement your own process.
0X04 implementation
Design interface:
class ScheduleCoroutine{ publicvoidStartCoroutine(IEnumerator coroutine); publicvoidStopCoroutine(IEnumerator coroutine); publicvoidUpdate(intfloat time);}
Design Data Structures:
class CoroutineNode{ public IEnumerator itor; publicstring name; publicint frame; publicfloat time; public Object instance; public CoroutineNode pre; public CoroutineNode next;}
Specific implementation of the code, for different project requirements, there are different ways to implement. This article explores the implementation of the unity process in a major way. After figuring out the principle, it will be more handy to use.
0x05 Reference
The article refers to a lot of other blog posts, thanks to their dedication. In the first reference link, there is a specific implementation code.
1, Unifywiki
2. wikipedia
Unity Process Usage Guide