7.与 3D 对象交互7. Interacting with 3D objects

本教程介绍如何启用 3D 对象的远近操作以及如何限制允许的操作类型。In this tutorial, you will learn how to enable near and far manipulation of 3D objects and limit the allowed types of manipulation. 你还将了解如何在 3D 对象周围添加边界框,以便更轻松地控制对象操作。You will also learn how to add bounding boxes around 3D objects to make it easier to control the object manipulation.

目标Objectives

  • 了解如何配置 3D 对象,以便可以与它们进行交互Learn how to configure 3D objects so they can be interacted with
  • 了解如何将边界框添加到 3D 对象Learn how to add bounding boxes to 3D objects

操作 3D 对象Manipulating 3D objects

在本部分中,你将添加相应功能,用于操作在教程定位场景中的对象期间在表格中包含的所有探测器部件。In this section, you will add the ability to manipulate all the Rover parts you organized on the table during the Positioning objects in the scene tutorial.

若要实现此目的,请执行以下主要步骤:The main steps you will take to achieve this are:

  1. 将“对象操控器(脚本)”组件添加到所有部件对象Add the Object Manipulator (Script) component to all the part objects
  2. 将“NearInteractionGrabbable”组件添加到所有部件对象Add the NearInteractionGrabbable component to all the part objects
  3. 配置“对象操控器(脚本)”组件Configure the Object Manipulator (Script) component

备注

若要 操作某个对象,该对象必须具有以下组件:To be able to manipulate an object, the object must have the following components:

  • “碰撞体”组件,例如盒碰撞体Collider component, for example, a Box Collider
  • “对象操控器(脚本)”组件Object Manipulator (Script) component

若要使用跟踪手 操作抓取某个对象,该对象必须具有以下组件:To be able to manipulate and grab an object with tracked hands, the object must have the following components:

  • “碰撞体”组件,例如盒碰撞体Collider component, for example, a Box Collider
  • “对象操控器(脚本)”组件Object Manipulator (Script) component
  • “NearInteractionGrabbable”组件NearInteractionGrabbable component

此外,你将配置漫游者探测器,以便能够将探测器部件放置到探测器上,使其成为完整的探测器。Additionally, you will configure the Rover Explorer so that you can place the rover parts on to the Rover to make it a complete rover assembly.

在“层次结构”窗口中展开“RoverExplorer”>“RoverParts”对象并选择其所有子探测器部件对象和“RoverAssembly”对象,然后在“检查器”窗口中,使用“添加组件”按钮将以下组件添加到所有选定对象 :In the Hierarchy window, expand the RoverExplorer > RoverParts object and select all its child rover part objects and the RoverAssembly object, then in the Inspector window, use the Add Component button to add the following components to all the selected objects:

  • “对象操控器(脚本)”组件Object Manipulator (Script) component
  • “NearInteractionGrabbable”组件NearInteractionGrabbable component
  • “部件程序集控制器(脚本)”组件Part Assembly Controller (Script) component

选中了 RoverAssembly 和所有探测器部件对象并添加了组件的 Unity

提示

若要选择不相邻的多个对象,请在按住 CTRL 键的同时使用鼠标选择任何对象。To select multiple objects that are not next to each other, press-and-hold the CTRL key while using the mouse to select any object.

备注

在本教程中,碰撞体已添加到探测器部件中。For the purpose of this tutorial, colliders have already been added to the rover parts. 若要详细了解碰撞体,可访问 Unity 的碰撞体文档。To learn more about colliders, you can visit Unity's Collider documentation.

备注

“部件程序集控制器(脚本)”并不是 MRTK 的一部分,而是属于本教程的资源。The Part Assembly Controller (Script) is not part of the MRTK but was included with the tutorial assets.

在所有探测器部件对象和 RoverAssembly 对象仍处于选中状态的情况下,在“检查器”窗口中,配置“对象操控器(脚本)”组件,如下所示:With all the rover part objects and the RoverAssembly object still selected, in the Inspector window, configure the Object Manipulator (Script) component as follows:

  • 从“双手操作类型”下拉列表中取消选中“缩放”,以便仅启用“移动”和“旋转” From the Two Handed Manipulation Type dropdown, uncheck the Scale, so only Move and Rotate is enabled

配置了“双手操作类型”的 Unity

备注

至此,已为所有探测器部件对象和 RoverAssembly 对象启用了对象操作。At this point, you have enabled object manipulation for all the rover part objects and the RoverAssembly object.

在“项目”窗口中,导航到“资产” > “MRTK” > “SDK” > “StandardAssets” > “音频”文件夹来查找音频剪辑 :In the Project window, navigate to the Assets > MRTK > SDK > StandardAssets > Audio folder to locate the audio clips:

选中了音频文件夹的 Unity 项目窗口

在“层次结构”窗口中重新选择所有探测器部件对象,然后在“检查器”窗口中,使用“添加组件”按钮添加“音频资源”组件,并按如下所述配置该组件 :In the Hierarchy window, reselect all the rover part objects, then in the Inspector window, use the Add Component button to add the Audio Sources component and configure it as follows:

  • 将“MRTK_Scale_Start”音频剪辑分配到“AudioClip”字段 Assign the MRTK_Scale_Start audio clip to the AudioClip field
  • 取消选中“唤醒时播放”复选框Uncheck the Play On Awake checkbox
  • 将“空间混合”更改为 1Change Spatial Blend to 1

选中了所有探测器部件并添加和配置了“音频源”组件的 Unity

在“层次结构”窗口中,展开“RoverAssembly”>“RoverModel_PlacementHints_XRay”>“Parts_PlacementHints”对象以显示所有放置提示对象,然后选择第一个探测器部件,“RoverParts”>“Camera_Part”,并配置“部件程序集控制器(脚本)”组件,如下所示 :In the Hierarchy window, expand the RoverAssembly > RoverModel_PlacementHints_XRay > Parts_PlacementHints object to reveal all of the placement hint objects, then select the first rover part, RoverParts > Camera_Part, and configure the Part Assembly Controller (Script) component as follows:

  • 将“Camera_PlacementHint”对象分配到“定位位置”字段 Assign the Camera_PlacementHint object to the Location To Place field

配置了 Camera_Part PartAssemblyController 组件的 Unity

对其余的每个探测器部件对象重复该步骤,以配置“部件程序集控制器(脚本)”组件,如下所示 :Repeat this step for each of the remaining rover part objects and the RoverAssembly object to configure the Part Assembly Controller (Script) component as follows:

  • 对于“Generator_Part”,请将“Generator_PlacementHint”对象分配到“定位位置”字段 For the Generator_Part, assign the Generator_PlacementHint object to the Location To Place field
  • 对于“Lights_Part”,请将“Lights_PlacementHint”对象分配到“定位位置”字段 For the Lights_Part, assign the Lights_PlacementHint object to the Location To Place field
  • 对于“UHFAntenna_Part”,请将“UHFAntenna_PlacementHint”对象分配到“定位位置”字段 For the UHFAntenna_Part, assign the UHFAntenna_PlacementHint object to the Location To Place field
  • 对于“Spectrometer_Part”,请将“Spectrometer_PlacementHint”对象分配到“定位位置”字段 For the Spectrometer_Part, assign the Spectrometer_PlacementHint object to the Location To Place field
  • 对于“RoverAssembly”,将对象本身(即同一 RoverAssembly 对象)分配到“定位位置”字段 For the RoverAssembly, assign the object itself, i.e. the same RoverAssembly object, to the Location To Place field

在“层次结构”窗口中,选择“RoverExplorer”>“按钮”>“重置”按钮对象,然后在“检查器”窗口中配置可交互的“OnClick ()”事件,如下所示 :In the Hierarchy window, select the RoverExplorer > Buttons > Reset button object, then in the Inspector window, configure the Interactable OnClick () event as follows:

  • 向“无(对象)”字段分配“RoverAssembly”对象 Assign the RoverAssembly object to the None (Object) field
  • 从“无函数”下拉列表中,选择“PartAssemblyController” > “ResetPlacement ()”将此函数设置为触发事件时要执行的操作 From the No Function dropdown, select PartAssemblyController > ResetPlacement () to set this function as the action to be executed when the event is triggered

配置了“重置”按钮对象 OnClick 事件的 Unity

如果现在进入“游戏”模式,就可以使用近距或远距交互将探测器部件放置到探测器上。If you now enter Game mode, you can use near or far interaction to place the rover parts on to the Rover. 一旦部件靠近相应的放置提示,它将贴靠到位并成为探测器的一部分。Once the part is close to the corresponding placement hint, it will snap into place and become part of the Rover. 若要重置放置,可以按“重置”按钮:To reset the placements, you can press the Reset button:

显示正在按下“重置”按钮的 Unity“播放”模式分屏视图

若要详细了解“对象操控器”组件及其相关属性,可参阅 MRTK 文档门户中的对象操控器指南。To learn more about the Object Manipulator component and its associated properties, you can visit the Object Manipulator guide in the MRTK Documentation Portal.

添加边界框Adding bounding boxes

边界框提供用于缩放和旋转的控点,在近距和远距交互时,使用边界框可以更轻松、更直观地单手操作对象。Bounding boxes make it easier and more intuitive to manipulate objects with one hand for both near and far interaction by providing handles that can be used for scaling and rotating.

在此示例中,将向 RoverExplorer 对象添加一个边界框,以便可以轻松实现整体移动、旋转和缩放。In this example, you will add a bounding box to the RoverExplorer object so the whole experience can easily be moved, rotated, and scaled. 此外,还将配置菜单,以便可以打开和关闭边界框。Additionally, you will configure the Menu so you can turn the Bounding Box on and off.

在“层次结构”窗口中选择“RoverExplorer”对象,然后在“检查器”窗口中,使用“添加组件”按钮添加以下组件 :In the Hierarchy window, select the RoverExplorer object, then in the Inspector window, use the Add Component button to add the following components:

  • BoundingBox 组件BoundingBox component
  • “对象操控器(脚本)”组件Object Manipulator (Script) component

然后取消选中两个组件旁边的复选框,以使其默认为“禁用” :Then uncheck the checkbox next to both components to make them disabled by default:

选中 RoverExplorer 对象并添加和禁用了组件的 Unity

备注

边界框可视化效果是在运行时创建的,因此在进入“游戏”模式之前不可见。The Bounding Box visualization is created at runtime and, therefore, not visible before you enter Game mode.

备注

BoundingBox 组件将在运行时自动添加 NearInteractionGrabbable 组件。The BoundingBox component will automatically add the NearInteractionGrabbable component at runtime. 因此,无需添加此组件即可使用跟踪的双手抓取包含的对象。Therefore, we do not need to add this component to grab the enclosed objects with tracked hands.

在“层次结构”窗口中展开“菜单”>“ButtonCollection”对象以显示第四个按钮,并将第三个按钮重命名为“BoundingBox_Enable”,然后在“检查器”窗口中配置“按钮配置帮助程序(脚本)”,如下所示 :In the Hierarchy window, expand the Menu > ButtonCollection object to reveal the four buttons and rename the third button to BoundingBox_Enable, then in the Inspector window, configure the Button Config Helper (Script) component as follows:

  • 将“主标签文本”更改为“启用” Change the Main Label Text to Enable
  • 向“无(对象)”字段分配“RoverExplorer”对象 Assign the RoverExplorer object to the None (Object) field
  • 在“无函数”下拉列表中选择“BoundingBox” > “启用布尔”,以便在触发事件时更新此属性 From the No Function dropdown, select BoundingBox > bool Enabled to update this property value when the event is triggered
  • 验证是否已选中参数复选框Verify that the argument checkbox is checked
  • 单击小 + 图标以添加另一个事件Click the small + icon to add another event
  • 向“无(对象)”字段分配“RoverExplorer”对象 Assign the RoverExplorer object to the None (Object) field
  • 在“无函数”下拉列表中选择“ObjectManipulator” > “启用布尔”,以便在触发事件时更新此属性 From the No Function dropdown, select ObjectManipulator > bool Enabled to update this property value when the event is triggered
  • 验证是否已选中参数复选框Verify that the argument checkbox is checked
  • 将图标保留为“带有边界框的多维数据集”图标Leave the Icon as the 'cube with bounding box' icon

选中了 BoundingBox_Enable 按钮对象并配置了“按钮配置帮助程序”组件的 Unity

将第四个和最后一个按钮重命名为“BoundingBox_Disable”,然后在“检查器”窗口中配置“按钮配置帮助程序(脚本)”组件,如下所示 :Rename the forth and last button to BoundingBox_Disable, then in the Inspector window, configure the Button Config Helper (Script) component as follows:

  • 将“主标签文本”更改为“禁用” Change the Main Label Text to Disable
  • 向“无(对象)”字段分配“RoverExplorer”对象 Assign the RoverExplorer object to the None (Object) field
  • 在“无函数”下拉列表中选择“BoundingBox” > “启用布尔”,以便在触发事件时更新此属性 From the No Function dropdown, select BoundingBox > bool Enabled to update this property value when the event is triggered
  • 验证是否已取消选中参数复选框Verify that the argument checkbox is unchecked
  • 单击小 + 图标以添加另一个事件Click the small + icon to add another event
  • 向“无(对象)”字段分配“RoverExplorer”对象 Assign the RoverExplorer object to the None (Object) field
  • 在“无函数”下拉列表中选择“ObjectManipulator” > “启用布尔”,以便在触发事件时更新此属性 From the No Function dropdown, select ObjectManipulator > bool Enabled to update this property value when the event is triggered
  • 验证是否已取消选中参数复选框Verify that the argument checkbox is unchecked
  • 将图标更改为“带有边界框的多维数据集”图标Change the Icon to the 'cube with bounding box" icon

选中了 BoundingBox_Disable 按钮对象并配置了“按钮配置帮助程序”组件的 Unity

如果现在进入“游戏”模式并通过单击“启用”按钮启用边界框,则可以使用近距或远距交互来移动、旋转和缩放边界框,然后使用“禁用”按钮再次禁用边界框:If you now enter Game mode and enable the Bounding Box by clicking the Enable button, you can use near or far interaction to move, rotate, and scale the Bounding Box, and use the Disable button to disable the Bounding Box again:

显示正在操作边界框的 Unity“播放”模式分屏视图

若要详细了解“边界框”组件及其相关属性,可以访问 MRTK 文档门户中的边界框指南。To learn more about the Bounding Box component and its associated properties, you can visit the Bounding box guide in the MRTK Documentation Portal.

祝贺Congratulations

在本教程中,你了解了如何为 3D 对象启用近距和远距操作,以及如何限制允许的操作类型。In this tutorial, you learned how to enable near and far manipulation for 3D objects and how to limit the allowed types of manipulation. 还了解了如何在 3D 对象周围添加边界框,以便更轻松地控制对象操作。You also learned how to add bounding boxes around 3D objects to make it easier to control the object manipulation.