Among new brilliant opportunities of Unity 5.3 I found one small thing which was to me useful, and, I think, can be useful also to you. These are the custom korutina provided by the class CustomYieldInstruction. Thanks to it, you will be able very just to create own korutinny yield-operators. Let's consider it on a living example.
Example from real life — bug fix
Recently I studied an error in the element of the user interface Dropdown which appeared in Unity 5.2. At installation of time scale Time.timeScale in value 0, Dropdown worked only once then it did not appear again while the time scale was not established in some nonzero value.
After short search, we found out that the problem arises in the Show method:
The m_Dropdown variable is not equal to null and it interferes with the Dropdown display
After the first m_Dropdown display it is turned and destroyed. Yes, it has to be destroyed, but when Time.timeScale is equal to zero, it does not occur.
Look at a method of destruction and try to find a problem:
Thanks to heading of this article it is possible to guess that a problem in WaitForSeconds. This class uses the scaled time. It means that when you ask WaitForSeconds to wait 1 moment at time scale equal 0.5, actually there will pass 2 seconds (1/0.5 = 2). If the time scale is equal 0, then it is necessary to wait infinitely (so far scale will not become nonzero). Dropdown will never be destroyed after the first use because of use of the class WaitForSeconds by us.
We need to implement a delay, using real time. Creation of the new yield-operator — the class WaitForSecondsRealtime will be the most elegant approach. Of course, if our own developers did not implement WaitForSeconds, using the scaled time, then we have to report to them about it. WaitForSecondsRealtime will strengthen this message. We need also to update documentation for WaitForSeconds, we did not mean the scaled time anywhere.
So I also found the class CustomYieldInstruction added recently for creation of new yield-operators.
It is very simple to add the new operator, and it will be a solution of our problem:
In any custom yield-operator it is necessary to redefine keepWaiting property. If we want that execution of the program continued, this property has to return false.
Now our bug fix will look here so:
In our example we take real time and we compare it at each check. More simply also cannot be … though, it is possible to make more simply by means of the new yield-operators WaitUntil and WaitWhile. Using them, we can transfer the delegate who will be called for our check.
Here an alternative method of a solution (we will assume that we just want to make a five-second delay):
So, it were simple, but potentially ample opportunities. I consider that the morals here is as follows: need not to forget to be read descriptions to releases of versions, it is possible to find new useful opportunities in them! And if you liked custom korutina, look also at UnityEvents — other my favourite opportunity which you could pass.
This article is a translation of the original post at habrahabr.ru/post/274305/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: firstname.lastname@example.org.
We believe that the knowledge, which is available at the most popular Russian IT blog habrahabr.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.