Interpretation of Unity process

Keywords: Unity

  1. StartCoroutine opens a process, and yield return is where the iterator block returns the call iteration.
  2. Unity's explanation for StartCoroutine is that the execution of a contract can be paused anywhere with a yield statement. The value of yield return determines when the contract resumes execution. It is very useful to coordinate the operations performed in several frames. It has almost no performance overhead. StartCoroutine usually returns immediately, but you can also get the value of the returned result. However, this step will not take effect until the end of the agreement.
  3. Use the yield statement to pause the execution of the coroutine, and the return value of yield specifies when to resume the coroutine.
  4. yield retun ...
    1. yield return null pauses the process and waits for the next frame to continue.
    2. Yield return waitforfixedrupdate(); pause the process until the next call to the FixedUpdate method.
    3. yield return new WaitForSeconds(1.0f), pause the process and wait for 1 second to continue.
    4. Yield return startcoroutine ("somecoroutinemethod"); / / pause the coroutine, open the somecoroutinemethod coroutinemethod coroutinemethod coroutinemethod, and continue to execute until somecoroutinemethod is finished
  5. Use of yield return.
    1, Yeld return null
    void Start(){
        Debug.Log("start1");
        StartCoroutine(Test());
        Debug.Log("start2");
    }
    
    IEnumerator Test(){
        Debug.Log("test1");
        yeild return null;
        Debug.Log("test1");
    }
    
    The result is: strat1 test1 start2 test2
    
    When the called function executes to yield return null (pause the cooperation, wait for the next frame to continue), the cooperation program will be paused according to the Unity interpretation. In fact, I personally think this interpretation is not accurate enough. First return to the place where the cooperation starts, and then pause the cooperation. That is to say, first notify the dispatcher, "go ahead, leave me alone" and then suspend the cooperation.
    
    
    
    2, Yild return new waitforsecondes (1.0F)
    void Start(){
        Debug.Log("start1");
        StartCoroutine(Test());
        Debug.Log("start2");
    }
    
    IEnumerator Test(){
        Debug.Log("test1");
        yeild return new WaitForSecondes(1.0f);
        Debug.Log("test1");
    }
    
    The running result is: strat1 test1 start2 test2(test2 waits for three seconds to print out)
  6. yield turn example
    IEnumerator Init()
    {
        yield return StartCoroutine(init1());
        Debug.Log("init1 finish");
        yield return StartCoroutine(init2());
        Debug.Log("init2 finish");
        yield return StartCoroutine(init3());
        Debug.Log("init3 finish");
    }
     
    IEnumerator init1()
    {
        // Analog initialization
        yield return new WaitForSeconds(2);
    }
    IEnumerator init2()
    {
        // do somthing..
        yield return new WaitForSeconds(2);
    }
    IEnumerator init2()
    {
        // do somthing..
        yield return new WaitForSeconds(2);
    }
    
    //In this way, the call can ensure the execution of init1, init2 and init3 one by one, so that the code to be executed later will not refer to a previously uninitialized variable
    
    
    
    
    
    void Start () {
        Debug.Log("start1");
        StartCoroutine(Test());
        Debug.Log("start2");
    }
    
    IEnumerator Test()
    {
        Debug.Log("test1");
        yield return StartCoroutine(DoSomething());
        Debug.Log("test2");
    }
    
    IEnumerator DoSomething()
    {
        Debug.Log("load 1");
        yield return null;
        Debug.Log("load 2");
    }
    	
    //Execution result: start1 test1 load1 start2 load2 test2
    	//A yield return StartCoroutine is nested in this StartCoroutine, The first StartCoroutine will wait until all the codes in the second StartCoroutine are finished, and the yield statement in the second StartCoroutine will first return the first one, and then immediately return to its calling place, that is, the calling place will continue to execute, and the first StartCoroutine will wait for the second one to finish before continuing to execute.

     

7. yield return

IEnumerator Queue()
{
    for (int i = 0; i < activity.Count; i++)
    {
        PlayerControl pc = activity[i];
        yield return CoroutineManger.GetInstance().StartCoroutine(pc.Target1());
        
        //When the process of AtkTarget1 takes a long time, it is possible to execute AtkTarget2 without completing the process of AtkTarget1
        yield return CoroutineManger.GetInstance().StartCoroutine(pc.Target2());

        //Execution of AtkTarget3 is not performed until the process in AtkTarget2
        yield return CoroutineManger.GetInstance().StartCoroutine(pc.Target3());
    }
}


public IEnumerator Target1()
{
    CoroutineManger.GetInstance().StartCoroutine(A1());
    CoroutineManger.GetInstance().StartCoroutine(A2());
}

public IEnumerator Target2()
{
    yield return CoroutineManger.GetInstance().StartCoroutine(A1());
    yield return CoroutineManger.GetInstance().StartCoroutine(A2());
    _temp.Clear();
}

public IEnumerator Target3()
{
    yield break;
}

 

Posted by Shandrio on Sat, 27 Jun 2020 18:50:40 -0700