📜 ⬆️ ⬇️

Custom Cortina in Unity 5.3

Hello. This is a translation of an article from the official blog of the Unity engine. It will talk about how to create your own korutin, which appeared in version 5.3.



Among the brilliant new features of Unity 5.3, I discovered one small thing that turned out to be useful to me, and I think it can be useful to you. These are custom corutines, represented by the class CustomYieldInstruction . Thanks to him, you can very easily create your own Cortina yield operators. Let's take a look at this with a live example.

Real life example - bug fix


Recently, I was studying an error in a Dropdown UI element that appeared in Unity 5.2. When setting the time scale Time.timeScale to 0, Dropdown worked only once, after which it did not appear again until the time scale was set to some non-zero value.
')
After a short search, we found that the problem occurs in the Show method:


The m_Dropdown variable is not null and this prevents the display of a dropdown

After the first mapping, m_Dropdown is minimized and destroyed. Yes, it should be destroyed, but when Time.timeScale is zero, this does not happen.

Take a look at the destruction method and try to find the problem:


Note from the translator
In the original article (and here) there is an error in the code - the call to m_Items.Clear () must be after the for loop, and not inside it. Thanks to Hruks for the tip.

Thanks to the title of this article, you can guess that the problem is in WaitForSeconds . This class uses scaled time. This means that when you ask WaitForSeconds to wait 1 second with a time scale of 0.5, it will actually take 2 seconds (1 / 0.5 = 2). If the time scale is 0, then you have to wait indefinitely (until the scale becomes non-zero). Dropdown will never be destroyed after its first use due to our use of the WaitForSeconds class.

Decision


We need to implement a delay using real time. The most elegant approach would be to create a new yield operator — the WaitForSecondsRealtime class. Of course, if our own developers have not implemented WaitForSeconds using scaled time, then we should inform them about it. WaitForSecondsRealtime reinforces this message. We also need to update the documentation for WaitForSeconds, because we did not mean scaled time anywhere.

So I discovered the CustomYieldInstruction class, which was added recently to create new yield operators.

Adding a new operator is very simple, and this will be the solution to our problem:


In any custom yield operator, you need to override the keepWaiting property. If we want the program to continue, this property should return false .

Now our bug fix will look like this:


In our example, we take the real time and compare it with each check. It could not be simpler ... although it can be made easier with the help of the new yield operators WaitUntil and WaitWhile . Using them, we can pass a delegate who will be called for our verification.

Here is an alternative way to solve the problem (suppose we just want to make a five-second delay):


So it was a simple but potentially wide opportunity. I believe that the moral here is this: you must not forget to read the descriptions for the release versions, because you can find new useful features in them! And if you like custom korutiny, also take a look at UnityEvents - another favorite of mine that you might have missed.

Source: https://habr.com/ru/post/274305/


All Articles