Transform 变换

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

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.

变换组件在三维空间中始终使用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.

变换组件可以在Scene视图或监视窗中即时的进行操作,在监视窗上会列出它的属性并且可以简单的进行编辑,你也可以通过使用移动,旋转和缩放工具来修改场景中物体的变换属性,这些工具在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.

这些工具可以用于你场景中的任何物体,在物体上单击,你将会看到当前选中的工具的线框显示在物体周围,这些线框看起来各不相同,点击物体也会使变换组件的属性显示在监视窗中,如果如果监视窗中没有显示任何变换组件或属性,那么此时场景中没有高亮的物体(没有被选中的物体)。



All three Gizmos can be directly edited in Scene View.

这三种线框可以直接在Scene视图中进行操作。

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.

若想操作变换属性,只需在三个线框坐标轴上点击并拖动,你会发现被点击的轴颜色改变了,当你用鼠标拖动时,你会看到物体会沿着这些轴移动,旋转或缩放。当你释放鼠标按键时,你会发现坐标轴仍处于选中状态,你可以点击鼠标中键并拖动来操作物体沿着选中坐标轴的变换,如果想同时操作三个坐标轴,那么就点击并拖动三个坐标轴线框的中点。


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中的场景会包含这些变换层级的集合,最外层的父物体对象被称作根对象,当你移动、缩放或旋转一个父物体,所有的变换也会应用于其子物体。

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.
    你可以修改变换组件坐标轴(和其他用户界面元素)的颜色通过菜单调用Unity Menu->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建模应用程序中或导入设置中定型你的网格。