Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel
Author:Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel
Language: eng
Format: epub
Publisher: Packt
Published: 2022-11-15T00:00:00+00:00
MathAction += AddOne
Written another way is:
MathAction = MathAction + AddOne;
You then see that we assigned UnityAction to the buttonâs listener. When you press the button, both of these functions will be running because UnityAction is assigned to both of them.
Before we can go further into the code, we need to cover one more topic, coroutines.
Coroutines
A coroutine allows you to spread a task over multiple frames. This is not a form of multithreading. Every action is still being run on the main thread. The power of coroutines is that they allow directable pausing through a new term, yield. Taking a look at the execution order in the figure below, you may remember seeing yield null after Update in the Game Logic section. If you donât have the execution order up on a browser tab, look at Figure 7.3. The small note on the left says it nicely. If a coroutine previously yielded or paused, and itâs due to resume, it will resume at that point in the execution order.
Figure 7.3: Game logic of the execution order
Thatâs awesome, isnât it? How does it know to resume, you ask? Good question, reader. It knows that it should resume because of the logic in the code. There is a fantastic example from the Unity Docs going over a basic fade from opaque to transparent using a coroutine. Letâs go through it quickly:
void Update() { if (Input.GetKeyDown("f")) { StartCoroutine(Fade()); } } IEnumerator Fade() { Color c = renderer.material.color; for (float alpha = 1f; alpha >= 0; alpha -= 0.1f) { c.a = alpha; renderer.material.color = c; yield return null; } }
I put in bold the three things that may be new to you. StartCoroutine(Fade()) is asking the application to start a coroutine with the Fade method. You will start the coroutine during the start of the game logic at the bottom of the yield statements; refer to Figure 7.3 again for this.
IEnumerator is stating that this method is iterable. Remember back to the last time you made a method. The keyword before the name is the type. We use void if it returns nothing, but since this will be iterated on, it needs to know. We let the computer know this by adding IEnumerable as the return type.
The last part is yield return null. This is tricky the first time looking over the for loop. In most cases, a return will take you out of a loop, but since we have a yield there, Unity asks if weâve finished with everything in the method. It pauses after subtracting 0.1f from the current alpha and waits for the game logic portion to start again to do it again until it satisfies the for loop logic. Once that is completed, it no longer yields.
Summarizing this code, pressing F will fade the GameObject this script is on out from the scene. We think you have a good enough grasp of these concepts. Letâs get back to the code in our project to finish up our implementation.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9861)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9750)
The Mikado Method by Ola Ellnestam Daniel Brolund(9741)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8252)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7739)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7734)
Grails in Action by Glen Smith Peter Ledbrook(7661)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7509)
Windows APT Warfare by Sheng-Hao Ma(6485)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6372)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6231)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6100)
Kotlin in Action by Dmitry Jemerov(5013)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4291)
Functional Programming in JavaScript by Mantyla Dan(4012)
Solidity Programming Essentials by Ritesh Modi(3829)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3605)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3555)
The Ultimate iOS Interview Playbook by Avi Tsadok(3520)
