Using Components 使用组件

Date:2011-08-01

Components are the nuts & bolts of objects and behaviors in a game. They are the functional pieces of every GameObject. If you don't yet understand the relationship between Components and GameObjects, we recommend that you read the GameObjects page before going any further.

组件是游戏对象和其行为的螺栓与螺母,它们是每个游戏对象的功能零件。如果你还不了解组件和游戏对象之间关系,我们建议你在进一步学习之前阅读上一节。

A GameObject is a container for many different Components. By default, all GameObjects automatically have a Transform Component. This is because the Transform dictates where the GameObject is located, and how it is rotated and scaled. Without a Transform Component, the GameObject wouldn't have a location in the world. Try creating an empty GameObject now as an example. Click the GameObject->Create Empty menu item. Select the new GameObject, and look at the Inspector.

一个游戏对象是许多不同的组件的容器。默认情况下,所有游戏对象自动拥有一个变换组件。这是因为变换组件决定了游戏对象的位置,以及它如何旋转和缩放。如果没有变换组件,游戏对象就不会存在于游戏世界中。作为一个例子,我们来尝试建立一个空的游戏对象。点击菜单项GameObject->Create Empty,选择新建的游戏对象,并在检视视图中查看。

变换组件

Even empty GameObjects have a Transform Component
即使空的游戏对象也有一个变换组件

Remember that you can always use the Inspector to see which Components are attached to the selected GameObject. As Components are added and removed, the Inspector will always show you which ones are currently attached. You will use the Inspector to change all the properties of any Component (including scripts)

记住,你始终可以使用检视视图,看看哪些组件连接到了选定的游戏对象。添加和删除组件时,检视视图将始终显示哪些是目前已连接的。你将使用检视视图改变任何组件(包括脚本)的所有属性。

Adding Components 添加组件

You can add Components to the selected GameObject through the Components menu. We'll try this now by adding a Rigidbody to the empty GameObject we just created. Select it and choose Component->Physics->Rigidbody from the menu. When you do, you will see the Rigidbody's properties appear in the Inspector. If you press Play while the empty GameObject is still selected, you might get a little surprise. Try it and notice how the Rigidbody has added functionality to the otherwise empty GameObject. (The y-component of the GameObject starts to decrease. This is because the physics engine in Unity is causing the GameObject to fall under gravity.)

你可以通过组件菜单为选择的游戏对象添加组件。现在我们试着为刚刚创建的空对象增加一个刚体,选中它,然后从菜单选择Component->Physics->Rigidbody。当你操作完,你会看到刚体属性出现在检视视图中。如果你在空对象仍被选中时按Play,你可能会得到一个惊喜。尝试一下,请注意添加了刚体功能的游戏对象是如何的不同。(对象的Y坐标值开始减小,这是因为Unity的物理引擎,导致游戏对象在重力的作用下落下。)

Adding Components 添加组件

An empty GameObject with a Rigidbody Component attached
一个附加了刚体组件的空对象

You can attach any number or combination of Components to a single GameObject. Some Components work best in combination with others. For example, the Rigidbody works with any Collider. The Rigidbody controls the Transform through the NVIDIA PhysX physics engine, and the Collider allows the Rigidbody to collide and interact with other Colliders. A different example of Component combinations is a Particle System. They use a Particle Emitter, Particle Animator, and Particle Renderer to create a group of moving particles.

你可以将任意数量或组合的组件添加到一个单一的游戏对象。有些组件最好与其它组件结合起来发挥作用。例如,刚体可以和任何碰撞组件协同工作。刚体组件通过NVIDIA PhysX物理引擎来控制变换,碰撞组件则允许刚体与其他碰撞体发生碰撞和相互作用。一个关于组件组合的不同的例子是粒子系统。它们使用一个粒子发射器、粒子动画、粒子渲染器创建一个运动粒子的群集。

If you want to know more about using a particular Component, you can read about any of them in the Component Reference. You can also access the reference page for a Component from Unity by clicking on the small ? on the Component's header in the Inspector.

如果你想知道更多使用特定组件的内容,你可以阅读Component Reference。你也可以在Unity里面,通过点击检视视图中组件标题(头部)的小问号来进入组件的参考页。

Editing Components 编辑组件

One of the great aspects of Components is flexibility. When you attach a Component to a GameObject, there are different values or Properties in the Component that can be adjusted in the editor while building a game, or by scripts when running the game. There are two main types of Properties: Values and References.

组件的一个重要方面就是灵活性。当你为游戏对象附加一个组件,该组件有不同的值或属性,它们可以在你建立游戏时在编辑器里进行调整,或者在运行游戏时由脚本来调整。有两种主要类型的属性:赋值属性和引用属性。

Look at the image below. It is an empty GameObject with an Audio Source Component. All the values of the Audio Source in the Inspector are the default values.

如下图所示,是一个具有音频源组件的空对象,检视视图中音频源的所有值是默认值。

Editing Components 编辑组件

This Component contains a single Reference property, and seven Value properties. Audio Clip is the Reference property. When this Audio Source begins playing, it will attempt to play the audio file that is referenced in the Audio Clip property. If no reference is made, an error will occur because there is no audio to be played. You must reference the file within the Inspector. This is as easy as dragging an audio file from the Project View onto the Reference Property or using the Object Selector.

该组件包含一个单独的引用属性,七个赋值属性。Audio Clip是引用属性。当这个音频源开始播放时,它会试图播放由Audio Clip属性引用的音频文件,如果没有属性(None),会发生错误,因为没有要播放的音频。你必须在检视视图中引用文件,这非常容易,只需从项目视图中拖动一个音频文件到引用属性上即可,或者使用对象选择器来完成。

Editing Components 编辑组件

Now a sound effect file is referenced in the Audio Clip property
现在Audio Clip属性已经引用了一个音效文件

Components can include references to any other type of Component, GameObjects, or Assets. You can read more about assigning references on the Assigning References page.

组件可以将引用属性包含到任何类型的组件、游戏对象、或资源。更多有关指派引用属性的内容,请阅读Assigning References。

The remaining properties on the Audio Clip are all Value properties. These can be adjusted directly in the Inspector. The Value properties on the Audio Clip are all toggles, numeric values, drop-down fields, but value properties can also be text strings, colors, curves, and other types. You can read more about these and about editing value properties on the Editing Value Properties page.

Audio Clip的其余属性都是赋值属性,这些属性可以直接在检视视图中调整。Audio Clip的赋值属性都是切换开关、数字值、下拉字段,赋值属性也可以是字符串、颜色、曲线、和其他类型。有关这些和有关编辑属性值的更多内容,你可以阅读Editing Value Properties

Testing out Properties 测试属性

While your game is in Play Mode, you are free to change properties in any GameObject's Inspector. For example, you might want to experiment with different heights of jumping. If you create a Jump Height property in a script, you can enter Play Mode, change the value, and press the jump button to see what happens. Then without exiting Play Mode you can change it again and see the results within seconds. When you exit Play Mode, your properties will revert to their pre-Play Mode values, so you don't lose any work. This workflow gives you incredible power to experiment, adjust, and refine your gameplay without investing a lot of time in iteration cycles. Try it out with any property in Play Mode. We think you'll be impressed.

当你的游戏处于播放模式时,你可以在任何游戏对象的检视视图中自由更改属性。例如,你可能想实验不同高度的跳跃。如果你在脚本中创建一个Jump Height属性,你可以进入播放模式,更改该值,然后按下跳跃按钮看看发生了什么。然后不退出播放模式再次改变该值,在接下来的几秒看看结果。当你退出播放模式,属性将恢复到上次播放前的数值,这样你就不会丢失任何工作。这样的工作流程给你难以置信的能力去试验、调整和完善你的游戏而无需投入大量的时间周期。尝试在播放模式下调整任意属性,我们相信你会留下深刻的印象。

Removing Components 删除组件

If you want to remove a Component, option- or right-click on its header in the Inspector, and choose Remove Component. Or you can left-click the options icon next to the ? on the Component header. All the property values will be lost and this cannot be undone, so be completely sure you want to remove the Component before you do.

如果你要删除组件,在检视视图中选择或右键单击其标题(头部),选择Remove Component。或者你可以左键单击组件标题(头部)问号旁边的选项图标,所有的属性值将丢失且不能撤销,所以在删除之前,你要完全确定。

分类:Manual|评论: 1|浏览: 359 翻译: 第七把剑
评论
Jare:参照属性,翻译成引用属性更合适点。
回复:已修正
发表评论
用户名:

评论审核后才会显示!