熟悉了混合现实工具之后,最好巩固知识的方法就是最项目。跟着微软官方的例程做一个火星漫游车置于场景中,使用 MRTK 功能来整理场景中的对象,并使用求解器功能智能跟踪对象,梳理用到的知识点。
预备工作 :

功能 | 版本号 |
---|
Mixed Reality Toolkit Foundation | 2.7.1 | Mixed Reality OpenXR Plugin | 1.0.1 |
- 下载
MRTK.HoloLens2.Unity.Tutorials.Assets.GettingStarted.2.5.0.1.unity 包
一、导入资源
1、在 Unity 菜单中,依次选择“Assets”>“Import Package”>“Custom Package”

2、选择MRTK.HoloLens2.Unity.Tutorials.Assets.GettingStarted.2.5.0.1.unity 包,然后打开

3、选择“All”导入包内所有资源

二、创建父对象
1、在“Hierachy”层次结构窗口中,右键单击某个空白处,然后选择“创建空白项”,将一个空对象添加到场景中。

2、右键单击新创建的对象,选择“重命名”,然后将名称更改为“RoverExplorer”。

3、选中“RoverExplorer”对象,在“检查器”窗口中,按以下所示配置“转换”组件
- 位置:X = 0, Y = -0.6, Z = 2
- 旋转:X = 0, Y = 0, Z = 0
- 缩放:X = 1,Y = 1,Z = 1

三、添加教程预制件
1、在“项目”窗口中,转到“Assets”→“MRTK.Tutorials.GettingStarted”→“Prefabs”文件夹

2、从“项目”窗口中,将“Table”预制件拖到“RoverExplorer”对象上,使其成为该对象的子对象。 然后,在“检查器”窗口中,按如下所示配置“转换”组件:
- 位置:X = 0, Y = -0.005, Z = 0
- 旋转:X = 0, Y = 0, Z = 0
- 缩放:X = 1.2,Y = 0.01,Z = 1.2

3、从“项目”中,将“RoverAssembly”预制件拖到“RoverExplorer”对象上,使其成为该对象的子对象。 然后,在“检查器”中,按如下所示配置“转换”组件:
- 位置:X = -0.1, Y = 0, Z = 0
- 旋转:X = 0, Y = -135, Z = 0
- 缩放:X = 1,Y = 1,Z = 1

四、使用集合组织和布局对象
1、在“Hierarchy”层次结构窗口中,右键单击“RoverExplorer”对象,然后选择“创建空白项”以将空对象添加为“RoverExplorer”对象的子对象。 将对象命名为“RoverParts”,并按如下所示配置“转换”组件:
- 位置:X = 0, Y = 0.06, Z = 0
- 旋转:X = 0, Y = 90, Z = 0
- 缩放:X = 1,Y = 1,Z = 1

2、在“Hierarchy”层次结构窗口中,选择以下所有子对象:“RoverExplorer”→“RoverAssembly”→“RoverModel”→“Parts”。 右键单击它们,然后选择“复制”以创建每个 parts 的副本。

3、在“RoverParts”对象上,粘贴选中的4个组件,使其成为该对象的子对象

4、为了便于场景的处理,在“层次结构”中,选择对象旁边的“眼睛”图标以禁用“RoverAssembly”对象的场景可见性。 此选项将隐藏“场景”窗口中的对象,而不会更改其在游戏中的可见性。

5、在“Hierarchy”层次结构窗口中,将所有组件重命名格式为“_Part”

6、在“Hierarchy”层次结构窗口中,选择“RoverParts”对象。 然后,在“检查器”窗口中,选择“添加组件”,然后搜索并选择“GridObjectCollection”。 此选项会将“GridObjectCollection”组件添加到“RoverParts”对象。

7、按如下所示配置“GridObjectCollection”组件值:
组件 | 组件值 |
---|
Sort Type | Alphabetical | Surface | Plane | Orient Type | None | Layout | Horizontal | Cell Width | 0.25 | Distance from parent | 0.38 |

8、然后选择“Update Collection”更新集合“RoverParts”子对象的位置。

五、方向指示器求解器
1、在“项目”窗口中,转到“资产”>“MRTK.Tutorials.GettingStarted”>“Prefab”。 将 Chevron 预制件拖到“层次结构”窗口上,并将其转换位置设置:
X = 0、Y = 0、Z = 2
以将其放置在 RoverExplorer 对象旁边

2、将新添加的 Chevron 对象重命名为“Indicator”。 然后,在“检查器”窗口中,选择“添加组件”以添加“DirectionalIndicator”。
 3、按如下所示配置“DirectionalIndicator”和“SolverHandler”组件:
- 验证“SolverHandler”组件的“Tracked TargetType”跟踪目标类型设置为“Head”
- 通过将“RoverExplorer”从“层次结构”拖到“无(转换)”字段,将其分配给“DirectionalIndicator”组件的“方向目标”
- 将“View Offset”视图偏移更改为“0.2”

|