Yield return null vs yield break


















StartCoroutine SomeSequence ;. IEnumerator SomeSequence. Log "Start some sequence" ;. Log "In the middle of some sequence" ;. Log "End some sequence" ;. IEnumerator ActionOne. Log "Start action one" ;. Log "End action one waited 3 seconds " ;. IEnumerator ActionTwo. Log "Start action two" ;. Log "End action two waited 1 second " ;. MentalMoustache , Dec 31, Gekigengar , Nodrap and Deleted User like this. Thanks MentalMoustache for explanation, but this raise one more question in my mind: If outer coroutine will resume its execution once inner coroutine ends completely, so why should we call inner coroutine?

Same execution order can be achieved with single coroutine, if we just replace "yield return StartCoroutine ActionOne ;" with its implementation. Joined: Oct 21, Posts: 5, Dantus , Dec 31, LdapConnection System.

Bulk Delete. Bulk Update. Bulk Merge. A yield return statement can't be located in a try-catch block. A yield return statement can be located in the try block of a try-finally statement. A yield break statement can be located in a try block or a catch block but not a finally block.

If the foreach or await foreach body outside of the iterator method throws an exception, a finally block in the iterator method is executed. The call to MyIteratorMethod doesn't execute the body of the method.

On an iteration of the foreach loop, the MoveNext method is called for elements. This call executes the body of MyIteratorMethod until the next yield return statement is reached. On each subsequent iteration of the foreach loop, the execution of the iterator body continues from where it left off, again stopping when it reaches a yield return statement. The foreach loop completes when the end of the iterator method or a yield break statement is reached.

MoveNextAsync method is called for elements. The System. It always leads to problems. Not something that you'd want, or do intentionally. The yield keyword is not a Unity feature. It's a C feature called "iterator block". It allows to easily write iterator objects.

The C compiler actually turns your coroutine method into a compiler generated state machine class. This is also called a "generator method". When you call this method you actually create an instance of the internally generated statemachine class which implements the IEnumerable interface. This can be used for example in a foreach loop:. This will print those 13 values: 0, 42, 7, 0, 2, 4, 6, 8, 10, 12, 14, 16, If you return the value null or any other value Unity doesn't recognise Unity will simple re-schedule the coroutine the next frame.

However if you return an instance of the WaitForSeconds class, Unity will wait for the specified time until it continues your coroutine. This great blog article which unfortunately only exists in the web archieve explains this quite well with a simple example.



0コメント

  • 1000 / 1000