unity 添加c#脚本_向Unity游戏项目添加C#脚本
unity 添加c#脚本
向Unity游戏项目添加C#脚本 (Adding a C# Script to Unity Game Project)
Of course, having objects that don't move in our game doesn't really make our game interesting. So, let's understand how Unity C# Script can be used to move the game objects, and from there on we'll move to Rigidbodies.
当然,拥有无法在我们的游戏中移动的对象并不会真正使我们的游戏变得有趣。 因此,让我们了解如何使用Unity C#脚本移动游戏对象,然后从那里移至“刚体”。
First, of all, let's create a script. To do so, right click in the Assets area, and go to Create → C# Script.
首先,让我们创建一个脚本。 为此, 右键单击 Assets区域,然后转到Create→C#Script 。
This will create a new file, with the default name NewBehaviourScript. Rename the script to Movement, and press Enter. This will create a new script with name Movement in the Assets section. Double click on it to open it, and let's see what happens. Don't type anything in this script for now.
这将创建一个默认名称为NewBehaviourScript的新文件。 将脚本重命名为Movement ,然后按Enter。 这将在Assets部分中创建一个名称为Movement的新脚本。 双击打开它,然后看看会发生什么。 现在不要在此脚本中键入任何内容。
Since we're now moving into scripting, let me take a few moments to mention that Unity refers to objects in a scene as gameObjects. So, from this point forward, we will refer to the used objects in the scene as gameObjects. Mr. Star will now be referred to as a gameObject instead of an object.
既然我们现在开始编写脚本,那么让我花点时间提及Unity将场景中的对象称为gameObjects 。 因此,从现在开始,我们将场景中使用的对象称为gameObjects 。 现在将Star先生称为gameObject而不是对象。
You will notice that the script that we just created, comes with two pre-defined methods, and that your script automatically inherits from a base class called MonoBehaviour
. Let's go through these one by one and try to understand what they mean.
您会注意到,我们刚刚创建的脚本带有两个预定义的方法,并且您的脚本会自动从名为MonoBehaviour
的基类MonoBehaviour
。 让我们一个个地研究这些,并尝试理解它们的含义。
单性行为 (MonoBehaviour)
This is the base class that all scripts inherit basic properties from, in Unity. This class defines and provides a lot of useful values, methods and properties which you can use in your script, saving you a lot of hassle. For example, MonoBehaviour contains definitions for the position of a gameObject (gameObject.transform.position.x/y/z
), meaning you can use these values directly instead of having to define them.
这是Unity中所有脚本都继承其基本属性的基类。 此类定义并提供了许多有用的值,方法和属性,可以在脚本中使用它们,从而节省了很多麻烦。 例如,MonoBehaviour包含游戏对象位置的定义( gameObject.transform.position.x/y/z
),这意味着您可以直接使用这些值,而不必定义它们。
You generally shouldn't get rid of the inheritance declaration, since most of the time you'll need the stuff this parent class provides, to get your work done. MonoBehaviour also contains the definition for the Start()
and Update()
methods, which we will explain up ahead.
通常,您不应该摆脱继承声明,因为在大多数情况下,您需要此父类提供的内容来完成工作。 MonoBehaviour还包含Start()
和Update()
方法的定义,我们将在后面进行解释。
Start()
方法 (Start()
method)
This method is run by the script once, at the very beginning of when the gameObject is initialized and is enabled. This means that this method is run as soon as an object becomes active. If an object is already active when the scene is opened, the initialization and enabling process are considered simultaneous. This method is very useful when you need to declare components or set values. For example, you can use the Start method to set the initial value of a gun's bullet count. You can also use it to access other components attached to any gameObject, as we'll see later on.
在初始化并启用gameObject的一开始,脚本便会运行此方法一次。 这意味着一旦对象变为活动状态,就会立即运行此方法。 如果打开场景时某个对象已经处于活动状态,则初始化和启用过程将视为同时进行。 当您需要声明组件或设置值时,此方法非常有用。 例如,您可以使用Start方法来设置枪支子弹计数的初始值。 您也可以使用它来访问附加到任何gameObject的其他组件,我们将在后面看到。
Check out the below code example, and do go through the comments to understand it.
查看下面的代码示例,并仔细阅读注释以了解它。
Update()
方法 (Update()
method)
This method is called 60 times per second by Unity (Or, 60 frames per second). It's where the main action of your code usually happens. For example, detecting input, adding forces, adding score, spawning enemies or bullets etc.
Unity每秒将这种方法调用60次(或每秒60 帧 )。 这是代码的主要动作通常发生的地方。 例如,检测输入,增加力量,增加得分,生成敌人或子弹等。
There are plenty of other pre-defined methods that MonoBehaviour class gives us. You can actually find a list of these methods in the Unity Documentation. They're for a variety of purposes, but for the time being, and for simplicity's sake, we will stick to just these two methods and the ones that we define on our own.
MonoBehaviour类还为我们提供了许多其他预定义的方法。 实际上,您可以在Unity文档中找到这些方法的列表。 它们有多种用途,但是就目前而言,为了简单起见,我们将只使用这两种方法以及我们自己定义的方法。
翻译自: https://www.studytonight.com/game-development-in-2D/basics-of-unity-script
unity 添加c#脚本