Transforms 变换

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

Transforms are a key Component in every GameObject. They dictate where the GameObject is positioned, how it is rotated, and its scale. It is impossible to have a GameObject without a Transform. You can adjust the Transform of any GameObject from the Scene View, the Inspector, or through Scripting.

变换是每个游戏物体(GameObject)的关键组件。它们决定GameObject的位置,旋转和缩放。有不带变换组件的游戏物体是不可能的。可以从场景视图、检视面板、或通过脚本调整任意GameObject的变换。

The remainder of this page's text is from the Transform Component Reference page.

Transform Component Reference页面查看更多描述。

Transform 变换

The Transform Component determines the actual Position, Rotation, and Scale of all objects in the scene. Every object has a Transform.

变换组件决定场景中所有物体的实际位置、旋转和缩放。每个物体都具有变换组件。


The Transform Component is viewable and editable in the Inspector
变换组件在检视面板是可见可编辑的。

Properties 属性

  • Position 位置 Position of the Transform in X, Y, and Z coordinates.
    变换在x,y,z坐标的位置
  • Rotation 旋转 Rotation of the Transform around the X, Y, and Z axes, measured in degrees.
    变换围绕x,y,z轴旋转,以度为单位测量。
  • Scale 缩放 Scale of the Transform along X, Y, and Z axes. Value "1" is the original size (size at which the object was imported).
    变换沿x,y,z轴缩放,值为1表示原始大小

All properties of a Transform are measured relative to the Transform's parent. If the Transform has no parent, the properties are measured relative to World Space.

变换的所有属性相对于变换的父级测量。如果变换没有父级,该属性将相对于世界空间测量。

Using Transforms 使用变换

Transforms are always manipulated in 3D space using X, Y, and Z axes. In Unity, these axes are represented by the colors red, green, and blue respectively. Remember: XYZ = RGB.

变换总是在3D空间使用x,y,z轴操控。在Unity,这些轴的颜色用红色,绿色,和蓝色分别代表。这样记忆:XYZ = RGB 。


Color-coded relationship between 3 axes and Transform properties 变换属性的三轴颜色的关系

Transform Components can be directly manipulated using the Scene View or the Inspector. The Inspector will list the properties stated above for easy editing. You can also modify Transforms in your Scene by interacting with them, using the Move, Rotate, and Scale tools. These tools are located in the upper left-hand corner of the Unity Editor.

变换组件可以使用场景视图工具或检视面板直接操作。在检视面板将列出属性,轻松的编辑。也可以在场景直接交互修改变换,使用移动、旋转和缩放工具。这些工具在Unity编辑器的左上角。


The View, Translate, Rotate, and Scale tools 视图、移动、旋转和缩放工具

The tools will be usable on any object in your scene. Click on the object, and you will see the tool gizmo appear around it. Depending on the current tool, the gizmo will look slightly different. Clicking the object will also cause the Transform component to become visible in the Inspector. If the Inspector does not display the Transform component or its properties, then you do not have an object highlighted in the Scene View.

这些工具可以用于场景中任何物体。在物体上点击,可以看到工具的gizmo显示。根据当前选择的工具gizmo的样式将会不同。点击物体也会使变换组件在检视面板变为可见。如果在检视面板不显示变换组件或它的属性,那么是你在场景中没有选择物体。



All three Gizmos can be directly edited in Scene View.

在场景视图中可以直接编辑的三种Gizmo。

To manipulate the Transform, click and drag on one of the 3 gizmo axes, you'll notice its color changes. As you drag the mouse, you will see the object translate, rotate, or scale along the axis. When you release the mouse button, you'll notice that the axis remains selected. You can click the middle mouse button and drag the mouse to manipulate the Transform along the selected axis. To access all 3 axes at once click and drag the center point of all 3 gizmos.

若要操纵变换,点击并拖拽其中一个gizmo轴,你会发现轴的颜色会改变。拖动鼠标看物体沿轴的移动、旋转和缩放。当释放鼠标时,你会发现,轴仍在保持选中状态,点鼠标中键拖拽鼠标来操作沿着选择的轴操作变换。要访问全部3个轴点击3个轴中间的方块拖动鼠标。


Any individual axis will become selected when you click on it 任意轴当你点击它是被选择

Parenting 父子级

Parenting is one of the most important concepts to understand when using Unity. When a GameObject is a Parent of another GameObject, the Child GameObject will move, rotate, and scale exactly as its Parent does. Just like your arms are attached to your body, when you turn your body, your arms move because they're attached. Any object can have multiple children, but only one parent.

在使用Unity时,父子级是一个需要理解的很重要的概念。当你游戏物体是另一个游戏物体的父级时,子游戏物体将准确的安装父物体那样移动、旋转和缩放,就如同胳膊附着在身体上一样,当你活动身体,胳膊也会移动,因为它是附着在身体上的。任何物体可以有多个子物体,但只会有一个父物体。

You can create a Parent by dragging any GameObject in the Hierarchy View onto another. This will create a Parent-Child relationship between the two GameObjects.

同在层级视图拖拽任意游戏物体到另一个上面就可以创建父物体,这会在两个游戏物体直接创建父子关系。


A real-world example of a Parent-Child hierarchy. All the GameObjects with arrows to the left are Parents.

父子级的例子,所有带有向左箭头的都是父物体

In the above example, we say that the arms are parented to the body, and the hands are parented to the arms. The scenes you make in Unity will contain collections of these Transform hierarchies. The topmost parent object is called the Root object. When you move, scale, or rotate a parent, all the changes in its Transform are applied to its children as well.

在上面的例子中我们说身体是胳膊的父物体,而胳膊又是手的父物体。在Unity中制作场景包含这些变换层级,最顶级父物体被称为Root object(根物体)。当移动、缩放或旋转父级,所有变换的改变将应用到它的子物体。

It is worth pointing out that the Transform values in the Inspector of any Child GameObject are displayed relative to the Parent's Transform values. These are also called the Local Coordinates. Through scripting, you can access the Global Coordinates as well as the Local Coordinates.

需要特别指出的是,在检视面板任何子游戏物体显示的变换的值相对于父变换的值。这也称为局部坐标,通过脚本,也可以访问全局坐标,像访问局部坐标一样。

You can build compound objects by parenting multiple separate objects together, like the skeletal structure of a human ragdoll. You can also create small yet effective objects with single parents. For example, if you have a horror game that takes place at night, you can create an effective atmosphere with a flashlight. To create this object, you would parent a spotlight Transform to the flashlight Transform. Then, any alteration of the flashlight Transform will affect the spotlight, creating a convincing flashlight effect.

可以通过父子级多个单独的物体到一起,来构建复合物体,像人偶的骨骼结构。还可以创建只带一个父物体小而有效的物体。例如,你有一个恐怖游戏,发生在夜间,可以创建带有手电筒的实际氛围。要创建这个物体,需要父子化聚光灯变换到手电筒变换,然后,手电筒变换的任何改变将影响聚光灯,创建一个具有说服力的手电筒效果。

Importance of Scale 缩放的重要性

The scale of the Transform determines the difference between the size of your mesh in your modeling application, and the size of your mesh in Unity. The mesh's size in Unity (and therefore the Transform's scale) is very important, especially during physics simulation. There are 3 factors that can determine the Scale of your object:

变换的缩放决定网格物体在建模程序和Unity中的大小。在Unity网格物体的大小是非常重要的,尤其是在物理模拟时。有三个因素决定物体的缩放:

  • The size of your mesh in your 3D modeling application.
    在3D建模程序中网格物体的大小
  • The Mesh Scale Factor setting in the object's Import Settings.
    在物体的导入设置中设置的网格物体缩放系数
  • The Scale values of your Transform Component.
    变换组件的缩放值

Ideally, you should not adjust the Scale of your object in the Transform Component. The best option is to create your models at real-life scale, so you won't have to change your Transform's scale. The second-best option is to adjust the scale at which your mesh is imported in the Import Settings for your individual mesh. Certain optimizations occur based on the import size, and instantiating an object that has an adjusted scale value can decrease performance. For more information, read through the Rigidbody component's section on optimizing scale.

理想的情况是不在物体的变换组件中调整缩放。最佳选项是建模时按照实际大小,这样就不用修改变换的缩放。第二较好的选项是,针对个别网格导入时,在导入设置中来调节缩放。某些优化是基于导入时的大小,实例化一个物体要调整缩放值,可能会影响性能。更多信息参考刚体组件章节中的优化缩放。

Hints 提示

  • When parenting Transforms, put the parent's location at 0,0,0 before applying the child. This will save you many headaches later.
    当父子级变换时,应用到子物体之前,把父物体放置到坐标0,0,0,这将在后期节省你很多头疼问题。
  • Particle Systems are not affected by the Transforms Scale. In order to scale a Particle System, you need to modify the properties in the System's Particle Emitter, Animator and Renderer.
    粒子系统不受变换缩放的影响。为了缩放粒子系统,需要修改系统的粒子发射器,动画和渲染的属性。
  • If you are using Rigidbodies for physics simulation, there is some important information about the Scale property to be read on the Rigidbody page.
    如果为物理模拟使用刚体,有一些重要的信息,在刚体页面阅读缩放属性。
  • You can change the colors of the Transform axes (and other UI elements) from the Unity Menu->Preferences->Colors & keys.
    可以从菜单修改变换轴的颜色,Edit->Preferences->Colors & keys
  • If you can avoid scaling, do so. Try to have the scales of your object finalized in your 3D modeling application, or in the Import Settings of your mesh.
    如果能够避免缩放,尽量避免。尽量在3D建模软件中按实际物体大小,或者在网格物体导入设置中设置好。

页面最后更新:2007-11-16