官术网_书友最值得收藏!

Using Unity's Update and Start methods

Every time you create a script in Unity, these two skeleton methods are included. That's because they are rather important. These are the most commonly used MonoBehaviour methods, see the next screenshot for others. I like to call these Unity's magic methods because you don't call these methods, Unity does. It's usually important that at least one MonoBehaviour method is included in a Unity script to cause the script to execute. I say usually because other methods in the script may be called from another script or class.

How do I know these two methods are called by Unity and that they are MonoBehaviour methods? Here, the Unity Scripting Reference is your friend.

Here's just a portion of the methods Unity can call in a script. This is from the Scripting Reference. Just search for MonoBehavior:

Look at line 4 of LearningScript:

public class LearningScript : MonoBehaviour

This line says that LearningScript inherits from MonoBehaviour. Any script that inherits from MonoBehaviour will automatically call the methods Update() and Start() if they are in the script. Therefore, if you want, you can create a script in MonoDevelop instead of Unity, just have it inherit from MonoBehavior by adding:: MonoBehaviour after the class name.

Note

Please notice the colon that needs to be included.

The Start method is called one time

Unity calls this method only one time. When the GameObject your script is attached to is first used in your scene, the Start() method is called. This method is primarily used to initialize, or setup, the member variables in your script. This allows everything in your script to be ready to go before Update() is called for the first time.

You've probably noticed that many of the examples I used in LearningScript are making use of Start(). These examples weren't initializing any code, I was just taking advantage of the fact that since Start() is only called once, displaying output to the Console would, therefore, only be displayed once, which just made it easier to see the output displayed.

The Update method is called over and over and over…

As you study the sample code in the Scripting Reference, you will notice that a vast majority of the code is in the Update() method. As your game runs, the Scene is displayed many times per second. This is called Frames per Second, or FPS. After each frame is displayed, the Update() method is called by Unity to run your code.

Since Update() is called every frame, it allows your game to detect input, such as mouse clicks and key presses, every frame. User input is one of the topics we are about to cover in the next chapter.

Pop quiz – understanding method operation

Q1. What are the minimum requirements for defining a method?

Q2. What is the purpose of the parentheses at the end of the method's name?

Q3. What does void mean in a method definition?

Q4. In a Unity script, how is the Update() called?

主站蜘蛛池模板: 黄陵县| 乐安县| 九龙城区| 桃园市| 抚远县| 彰化市| 西和县| 齐齐哈尔市| 和硕县| 安龙县| 吐鲁番市| 二手房| 乌鲁木齐市| 开江县| 郯城县| 洪湖市| 临洮县| 化德县| 三门县| 印江| 汝州市| 夹江县| 博乐市| 英超| 民权县| 渝中区| 乌海市| 汝阳县| 奇台县| 长武县| 沙湾县| 罗源县| 南岸区| 鄢陵县| 大邑县| 永州市| 陆丰市| 孟州市| 扎赉特旗| 清水河县| 合川市|