1. coroutine
In unity 3D, when we first started writing scripts, we certainly encountered the following requirement: to launch a fireworks every three seconds, and to restart the system 20 seconds after the death of a monster. At the beginning, I like to insert all these things into the Update file, as shown below.
1 float nowtime = 3.0f; 2 bool isdead = true; 3 float deadtime = 20366f; 4 5 void startfireworks () 6 {7 // fireworks 8} 9 10 void revival () 11 {12 // resurrection 13} 14 15 void Update () 16 {17 if (nowtime <= 0) 18 {19 startfireworks (); 20 nowtime = random. range (2.5f, 3.5f); 21} 22 nowtime-= time. deltatime; 23 if (isdead) 24 {25 if (deadtime <= 0) 26 {27 revival (); 28 isdead = false; 29 deadtime = 30366f; 30} 31 deadtime-= time. deltatime; 32} 33}
When there are many such requirements, the update process is messy. If you need to add or modify them, it will be very troublesome. In particular, this requirement is similar to the need to be revived after death. It only waits for 30 seconds after death to be revived, and does not need to be executed at any time. Therefore, each frame is needed to be judged in the update, it seems cumbersome.
Fortunately, unity 3D supports yield coroutine. It doesn't matter if you don't understand it. Let's take a look at the following to use coroutine to implement the above functions.
1 void Start() 2 { 3 StartCoroutine(Fireworks()); 4 } 5 6 void deadHandle() 7 { 8 StartCoroutine(Revival()); 9 }10 11 IEnumerator Fireworks()12 {13 while (true)14 {15 startFireworks();16 yield return new WaitForSeconds(Random.Range(2.5f, 3.5f));17 }18 }19 20 IEnumerator Revival()21 {22 yield return new WaitForSeconds(30.0f);23 revival();24 }
In the above Code, the function that uses ienumerator as the return value is the coroutine. Call startcoroutine () to start the coroutine and call startcoroutine (Fireworks () in the Start function ());, it indicates that the coroutine fireworks () is started at the beginning, and startcoroutine (revival () is called in deadhandle (). It indicates that the coroutine is executed when the monster dies.
Now let's take a look at the yield Return Statement in coroutine fireworks () and revival (). Yield return New waitforseconds (30366f); indicates that the execution is interrupted from the return statement, execute the subsequent code in 30 seconds.
If you want to continue execution in the next frame, you should write yield return NULL in this way, so that the statement will be interrupted in return and wait for the code after the next frame to continue execution. As written in fireworks (), after return, continue the while judgment. If it is true, continue the loop. When yield return is interrupted, wait, and run it again and again, just like update. Of course, you can specify the judgment conditions in the while clause. When an interruption is required, set the judgment conditions in the while clause to false.
1 bool isContinue = true; 2 void stopFireworks() 3 { 4 isContinue = false; 5 } 6 IEnumerator Fireworks() 7 { 8 while (isContinue) 9 {10 startFireworks();11 yield return new WaitForSeconds(Random.Range(2.5f, 3.5f));12 }13 }
Of course, stopcoroutine can also be used to stop the execution of coroutine. However, this function is conditional. For details, refer to the Unity document or search for it online. There are a lot of materials, this is just to tell you that such a thing can be used.
With coroutine, it is much easier to write scripts. Like update, coroutine also detects calls at each frame. Therefore, time. deltatime can be used in coroutine. There have been no tests on the execution sequence such as coroutine and update, and some information is available on the Internet. You can refer to the specific implementations of different unity 3D versions, if you need to know the execution sequence of some functions, you can test the execution sequence.
Note that:Waitforseconds is affected by time. timescale. If you set it to 0, the coroutine cannot be executed. However, yield return NULL is not affected because each frame is executed, but time. deltatime is 0.
2. message transmission
In game development, message transmission is essential. There are usually three methods: Saving the reference of other objects, events in sendmessage and C # that come with unity.
For example, if a pause occurs, I need to notify the player that it has paused. Do not respond to keyboard and mouse operations. Notify the UI to display a pause panel. When all monsters are used, do not move or pause, take a break.
The first method is often used to write scripts at the beginning. It is very troublesome to save all object references. I need to get players, UIS, and all monster objects, then, call the corresponding pause function. As the program grows, adding, modifying, and deleting operations are a huge workload. In addition, many objects reference each other, and the coupling is very high, which is troublesome to use.
The second method is the messages message mechanism provided by unity 3D. However, this method has many drawbacks on the Internet, and only one parent-child relationship object can be notified. messages between different objects cannot be transmitted. I have never used this mechanism, so it is not clear whether it is as mentioned above.
The third method is the delegate and event in C #. This method is very useful for message transmission. From the design mode perspective, it is a typical observer mode. If you have used easytouch joystick, You should know to use easyjoystick. on_joystickmove + = onjoystickmove in onenable (); to register your own move function, here it is onjoystickmove. In fact, easytouch uses the C # event and uses ++ = to add its own response function. When the joystick moves, it will call the onjoystickmove function specified by you. For more information, see the following link.
Today I will write it here. These are all about the nature and there are a lot of detailed information on the Internet. I just want to tell beginners that unity 3D has these things, which may be what you need, so you can take less detours.
The coroutine and C # events are strongly recommended in Unity 3D. They are really important and must be used properly.
Reference 1: [Vomiting Blood recommendation] Brief Analysis of Yield in unity3d
Reference 2: C # events and unity3d