Yield & yield keyword of C# for Unity protocol

Keywords: Unity

Yield & yield keyword of C# for Unity protocol

Unity Program:

This is to say that Unity opens the yield correlation in the IEnumerator process through Start Coroutine:

1.yield return 0,yield return null

Wait for the next frame to execute the following


2.yield return new WaitForSeconds(float secs)

Wait for the specified number of seconds, then execute the following


3.yield return www;

Use WWW to download and wait for the download to complete before executing the following code

WWW www=new WWW("Here is the address.");
Debug.Log("1");
yield return www; //Wait here for www.isDone, that is, the download is completed before you go back to the code
Debug.Log("2");

4.yield return Start Coroutine

Execute the coordinating method first, and wait until the coordinating method is executed before executing the following content


5.yield break

Exit the consortium and do not execute the code after break


Example:

public static bool temp = true;
    private void Start()
    {
        StartCoroutine(ForExample());
    }
    public IEnumerator ForExample()
    {
        yield return "1";  // Execution on the first call  
        Debug.Log("hiha1");
        yield return "2";  // Execution on the second call  
        Debug.Log("hiha2");
        if (temp)            // Execution on the third call  
        {
            // The following statement is not executed after yield break is executed  
            Debug.Log("bareak");
            yield break;
        }
        // Otherwise, temp is false  
        yield return "3";  // Execution on the fourth call  
        Debug.Log("hiha3");
        yield return "4";  // Execution on the fifth call  
        Debug.Log("hiha4");
    }
Result:

hiha1
hiha2
bareak


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


2.C# yield: (Reference link: http://www.cnblogs.com/vivid-stanley/p/5272736.html)

yield is used in an iterator block to provide values to an enumerator object or to send an end-of-iteration signal. It takes the form of one of the following:
yield return <expression>;
yield break

e.g.

class CustomCollection : IEnumerable
    {
        static void Main()
        {
            CustomCollection cc = new CustomCollection();
           foreach (String word in cc)
            {
               Console.WriteLine("Info:" + word);
            }
        }
        public IEnumerator GetEnumerator()
        {
            yield return "T1";
            yield return "T2";
            yield return "T3";
            yield return "T4";
        }
    }
The above example implements a custom iterator; to implement an iterative (foreach-enabled) data set, you must implement the GetEmumerator() method to return an object instance that implements IEmumerator.

There are two ways to accomplish this:

1/One is that the above method of yield return. yield return needs to be used in conjunction with IEmumerator. In the external foreach loop, it will execute the GetEmumerator() method, encounter yield return, and do the following two things:

1. Record the current code location to be executed;

2. Return code control to the outside, and yield return to the following value as the current value of the iteration.

When the next loop is executed, the code continues to execute after the code location just recorded.

Simply put, yield return is a super simplified version of IEmumerator. Is it simple?

2/Another is to implement IEmumerator version:

 public class HelloBoyGirls : IEnumerator
    {
        private int cusor = -1;
        private String[] words = {"T1", "T2", "T3", "T4"};
        
        public bool MoveNext ()
        {
            cusor++;
            return cusor < words.Length;
       }

       public void Reset ()
       {
            cusor = 0;
        }
       public object Current {
            get {
                return words [cusor];
            }
        }
    }
    class CustomCollection : IEnumerable
    {
        static void Main()
        {
            CustomCollection cc = new CustomCollection();
            foreach (String word in cc)
            {
                Console.WriteLine("Info:" + word);
            }
        }
        public IEnumerator GetEnumerator()
        {
            return new HelloBoyGirls();
        }
    }
The results are:

Info:T1
Info:T2
Info:T3
Info:T4

So the question arises again. How does yield return decide when the loop should end? When does the code after yield return execute?

Modify the example above, instead of using convenient foreach, use the while loop to control yourself:
public class CustomCollection : IEnumerable
    {
        public IEnumerator GetEnumerator()
        {
            yield return "T1";
            yield return "T2";
            yield return "T3";
            yield return "T4";
            Console.WriteLine("After all yield returns.");
        }
        public static void Main(string[] args)
         {
             CustomCollection cc = new CustomCollection();
 
             IEnumerator enumerator = cc.GetEnumerator();
             while (true) {
                 bool canMoveNext = enumerator.MoveNext();
                 Console.WriteLine("canMoveNext:" +canMoveNext);
                 if (!canMoveNext)
                    break;
                 Object obj = enumerator.Current;
                 Console.WriteLine("current obj:" +obj);
             }
 //            foreach (String word in cc) {
 //                Console.WriteLine ("word:" +word);
 //            }
             Console.WriteLine("Main End.");
 
        }
    }
Result:

canMoveNext:True
current obj:Hello
canMoveNext:True
current obj:Boys
canMoveNext:True
current obj:And
canMoveNext:True
current obj:Girls
After all yield returns.
canMoveNext:False
Main End.
Explain that in C #, every time MoveNext() is called, MoveNext() goes to the next yield field (from the first, first), and then the Current value is the value after yield return, stops until the next yield field is called again; if no next yield field is found, MoveNext() returns false.

In addition to yield return, there is yield break; yield break's function is to stop the loop, MoveNext() is false, the statement after yield break will not be executed!

Posted by Flames on Wed, 02 Jan 2019 22:33:10 -0800