4.定位场景中的对象4. Positioning objects in the scene

概述Overview

在本教程中,你将导入教程资产,并定位场景中提供的对象。In this tutorial, you will import the tutorial assets and position the provided objects in the scene.

目标Objectives

  • 了解如何定位场景中的对象Learn how to position objects in the scene
  • 了解如何使用 MRTK 的网格对象集合功能Learn how to use MRTK's Grid Object Collection feature

导入教程资产Importing the tutorial assets

下载并导入以下 Unity 自定义包:Download and import the following Unity custom package:

导入教程资产后,“项目”窗口应如下所示:After you have imported the tutorial assets your Project window should look similar to this:

导入教程资产后的 Unity“层次结构”、“场景”和“项目”窗口

提示

有关如何导入 Unity 自定义包的提示,可参阅导入 MRTK 说明。For a reminder on how to import a Unity custom package, you can refer to the Importing the MRTK instructions.

创建父对象Creating the parent object

在“层次结构”窗口中,右键单击空白区域,然后选择“创建空白项”将空对象添加到场景中:In the Hierarchy window, right-click on an empty spot, and select Create Empty to add an empty object to your scene:

Unity“创建空上下文弹出菜单”

提示

若要按上图所示并排显示“场景”和“游戏”窗口,请将“游戏”窗口拖放到“场景”窗口的右侧。To display your Scene and Game window side by side as shown in the image above, drag the Game window to the right side of the Scene window. 若要详细了解如何自定义工作区,可参阅 Unity 的自定义工作区文档。To learn more about customizing your workspace, you can refer to Unity's Customizing Your Workspace documentation.

右键单击新创建的对象,选择“重命名”,然后将名称更改为“RoverExplorer” :Right-click on the newly created object, select Rename, and change the name to RoverExplorer:

Unity“重命名上下文弹出菜单”

在仍选中 RoverExplorer 对象的情况下,在“检查器”窗口中,按如下所示配置“转换”组件:With the RoverExplorer object still selected, in the Inspector window, configure the Transform component as follows:

  • 位置:X = 0, Y = -0.6, Z = 2Position: X = 0, Y = -0.6, Z = 2
  • 旋转:X = 0, Y = 0, Z = 0Rotation: X = 0, Y = 0, Z = 0
  • 缩放:X = 1,Y = 1,Z = 1Scale: X = 1, Y = 1, Z = 1

选中并定位了 RoverExplorer 对象的 Unity

备注

摄像头表示用户头部,位于原点,即 X = 0,Y = 0,Z = 0。The camera represents the users head and is positioned at origin, X = 0, Y = 0, Z = 0. 一般情况下,Unity 中的 1 个单位大致对应现实生活中的 1 米。In general, 1 unit in Unity is roughly 1 meter in the physical world. 但也存在例外情况,例如,当对象是缩放对象的子级时。However, there are exceptions to this, for example, when objects are children of scaled objects. 在上述场景中,RoverExplorer 位于用户头部前方 2 米、下方 0.6 米处。In the scenario above, the RoverExplorer is positioned 2 meters in front of and 0.6 meters below the user's head.

添加教程预制件Adding the tutorial prefabs

在“项目”窗口中,导航到“资产” > “MRTK.Tutorials.GettingStarted” > “预制件”文件夹 :In the Project window, navigate to the Assets > MRTK.Tutorials.GettingStarted > Prefabs folder:

选中了预制件文件夹的 Unity 项目窗口

提示

预制件是作为 Unity 资产存储的预配置 GameObject,可在整个项目中重复使用。A prefab is a pre-configured GameObject stored as a Unity Asset and can be reused throughout your project.

在“项目”窗口中,单击“Table”预制件并将其拖动到 RoverExplorer 对象上,使其成为 RoverExplorer 对象的子对象,然后在“检查器”窗口中,按如下所示配置“转换”组件 :From the Project window, click-and-drag the Table prefab on to the RoverExplorer object to make it a child of the RoverExplorer object, then in the Inspector window, configure the Transform component as follows:

  • 位置:X = 0, Y = -0.005, Z = 0Position: X = 0, Y = -0.005, Z = 0
  • 旋转:X = 0, Y = 0, Z = 0Rotation: X = 0, Y = 0, Z = 0
  • 缩放:X = 1.2,Y = 0.01,Z = 1.2Scale: X = 1.2, Y = 0.01, Z = 1.2

选中并定位了新增的“表”预制件的 Unity

提示

若要按下图所示显示场景,请使用“场景”窗口右上角的场景 Gizmo,将视角调整为沿正向 Z 轴,双击 MixedRealityPlayspace 对象以将焦点对准摄像头,然后根据需要放大。To display your scene as shown in the image above, use the Scene Gizmo, located in the top right corner of the Scene window, to adjust the viewing angle to be along the forward Z axis, double-click the MixedRealityPlayspace object to focus on the camera, and zoom in as needed.

在“项目”窗口中,单击“RoverAssembly”预制件并将其拖动到 RoverExplorer 对象上,使其成为 RoverExplorer 对象的子对象,然后在“检查器”窗口中,按如下所示配置“转换”组件 :From the Project window, click-and-drag the RoverAssembly prefab on to the RoverExplorer object to make it a child of the RoverExplorer object, then in the Inspector window, configure the Transform component as follows:

  • 位置:X = -0.1, Y = 0, Z = 0Position: X = -0.1, Y = 0, Z = 0
  • 旋转:X = 0, Y = -135, Z = 0Rotation: X = 0, Y = -135, Z = 0
  • 缩放:X = 1,Y = 1,Z = 1Scale: X = 1, Y = 1, Z = 1

选中并定位了新增的 RoverAssembly 预制件的 Unity

组织集合中的对象Organizing objects in a collection

在“层次结构”窗口中,右键单击“RoverExplorer”对象,然后选择“创建空白项”添加一个空对象作为 RoverExplorer 的子对象,将该对象命名为“RoverParts”,并按如下所示配置“转换”组件 :In the Hierarchy window, right-click on the RoverExplorer object and select Create Empty to add an empty object as a child of the RoverExplorer, name the object RoverParts, and configure the Transform component as follows:

  • 位置:X = 0, Y = 0.06, Z = 0Position: X = 0, Y = 0.06, Z = 0
  • 旋转:X = 0, Y = 90, Z = 0Rotation: X = 0, Y = 90, Z = 0
  • 缩放:X = 1,Y = 1,Z = 1Scale: X = 1, Y = 1, Z = 1

选中并定位了新创建的 RoverParts 对象的 Unity

在“层次结构”窗口中,选择所有“RoverExplorer”>“RoverAssembly”>“RoverModel”>“Parts”子对象,右键单击它们,然后选择“复制”,创建每个部件的副本 :In the Hierarchy window, select all the RoverExplorer > RoverAssembly > RoverModel > Parts child objects, right-click on them and select Duplicate to create a copy of each of the parts:

选中了所有部件且具有“复制上下文弹出菜单”的 Unity

提示

若要选择多个相邻的对象,请按住 SHIFT 键,并同时使用鼠标选择第一个和最后一个对象。To select multiple adjacent objects, press-and-hold the SHIFT key while using the mouse to select the first and last object.

在仍选中新复制的 Parts 子对象的情况下,单击它们并将其拖动到 RoverParts 对象上,使其成为 RoverParts 对象的子对象:With the newly duplicated Parts child objects still selected, click-and-drag them on to the RoverParts object to make them child objects of the RoverParts object:

将新复制的部件作为 RoverParts 对象的子类的 Unity

为了更轻松地在场景中操作,请在“层次结构”窗口中,单击“RoverAssembly”对象左侧的眼睛图标,将此对象的“场景可见性”切换为关闭 。To make it easier to work with your scene, in the Hierarchy window, click the eye icon to the left of the object to toggle the scene visibility for the RoverAssembly object off. 这会在“场景”窗口中隐藏该对象,但在游戏中不会改变其可见性:This hides the object in the Scene window without changing its in-game visibility:

RoverAssembly 场景可见性为关闭的 Unity

提示

若要详细了解“场景可见性”控件以及如何使用它们来优化场景视图和工作流,可参阅 Unity 的场景可见性文档。To learn more about the Scene Visibility controls and how you can use them to optimize your scene view and workflow, you can refer to Unity's Scene Visibility documentation.

在“层次结构”窗口中,将后缀“(1)”替换为“_Part”以整理 RoverParts 子对象的名称 :In the Hierarchy window, clean up the RoverParts child objects' names by replacing the appended (1) with _Part:

清除了复制的部件名称的 Unity

在“层次结构”窗口中,选择“RoverParts”对象,然后在“检查器”窗口中单击“添加组件”按钮,搜索并选择“GridObjectCollection”,以将“GridObjectCollection”组件添加到“RoverParts”对象 :In the Hierarchy window, select the RoverParts object, then in the Inspector window, click the Add Component button, and search for and select GridObjectCollection to add the GridObjectCollection component to the RoverParts object:

正在“添加组件网格对象集合”的 Unity RoverParts 对象

按如下所示配置“GridObjectCollection”组件值:Configure the GridObjectCollection component values as follows:

  • 排序类型:按字母顺序Sort Type: Alphabetic
  • 布局:水平Layout: Horizontal
  • 单元格宽度:0.25Cell Width: 0.25
  • 到父项的距离:0.38Distance from parent: 0.38

配置了 GridObjectCollection 组件的 Unity

然后单击“更新集合”按钮以更新 RoverParts 子对象的位置:Then click the Update Collection button to update the position of the RoverParts child objects:

应用了 GridObjectCollection 组件的 Unity

祝贺Congratulations

本教程介绍了如何在场景中相对于用户定位对象,以及如何使用 MRTK 的网格对象集合功能来组织集合中的对象。In this tutorial, you learned how to position objects in the scene relative to the user and use MRTK's Grid Object Collection feature to organize objects in a collection.

下一教程:5.使用求解器创建动态内容Next Tutorial: 5. Creating dynamic content using Solvers