The GameObject-Component Relationship 游戏对象和组件的关系

优质
小牛编辑
131浏览
2023-12-01

As described on the GameObjects page, a GameObject contains Components. We'll explore this relationship by discussing a GameObject and its most common Component -- the Transform Component. With any Unity Scene open, create a new GameObject (using Shift-Control-N on Windows or Shift-Command-N on Mac), select it and take a look at the Inspector.

正如上页所述,游戏对象包含组件。通过论述,我们将研究一个游戏对象和它最常见的组件——Transform组件。打开任意Unity场景,创建一个新的游戏对象(在Windows中使用Shift-Ctrl-N,在Mac中使用Shift-Command-N),选择它并在检视面板视图中查看。

The GameObject-Component Relationship 游戏对象和组件的关系
The Inspector of an Empty GameObject 一个空的游戏对象的检视视图

Notice that an empty GameObject still contains a Name, a Tag, and a Layer. Every GameObject also contains a Transform Component.

注意,一个空的游戏对象仍然包含一个名字、一个标签、一个层。每个游戏对象还包含一个变换组件——Transform。

The Transform Component 变换组件

It is impossible to create a GameObject in Unity without a Transform Component. The Transform Component is one of the most important Components, since all of the GameObject's Transform properties are enabled by its use of this Component. It defines the GameObject's position, rotation, and scale in the game world/Scene View. If a GameObject did not have a Transform Component, it would be nothing more than some information in the computer's memory. It effectively would not exist in the world.

在Unity中创建一个没有变换组件的游戏对象是不可能的。变换组件是最重要的组件之一,因为游戏对象的变换属性都是由这个组件启用的。在游戏世界/场景视图中,它定义了游戏对象的位置,旋转和缩放。如果一个游戏对象没有变换组件,这表示在计算机内存中没有任何信息,它实际上不存在于世界。

The Transform Component also enables a concept called Parenting, which is utilized through the Unity Editor and is a critical part of working with GameObjects. If you would like to learn more about the Transform Component and Parenting, please read the Transform Component Reference page.

变换组件也使用父子的概念,它为Unity编辑器所用,是应用游戏对象的关键。如果你想了解更多有关变换组件和父子系统,请阅读TransformComponentReferencepage

Other Components 其它组件

The Transform Component just happens to be critical to all GameObjects, so each GameObject has one. But GameObjects can contain other Components as well.

变换组件仅仅是所有游戏对象的决定性组件,所以每个游戏对象有一个,游戏对象可以包含其他组件。

The GameObject-Component Relationship 游戏对象和组件的关系
The Main Camera, added to each scene by default 主相机,默认情况下添加到每个场景

Taking a look at the Main Camera GameObject, we can see that it contains a different collection of Components. Specifically, a Camera Component, a GUILayer, a Flare Layer, and an Audio Listener. All of these Components provide additional functionality to the GameObject. Without them, there would be nothing rendering the graphics of the game for the person playing! Rigidbodies, Colliders, Particles, and Audio are all different Components (or combinations thereof) that can be added to any given GameObject.

以主相机对象为例,我们可以看到,它包含了组件的不同集合。具体来说,有一个相机组件,一个GUILayer(图形界面层),一个光晕层和一个音频侦听器。所有这些组件为游戏对象提供了更多的功能。没有他们,就不会为玩家描绘出任何游戏图形!刚体,碰撞,粒子,和音频都是不同的组件(或其组合),可以添加到任何特定的游戏对象中。