MR 输入 210:凝视MR Input 210: Gaze

备注

混合现实学院教程在制作时考虑到了 HoloLens(第一代)和混合现实沉浸式头戴显示设备。The Mixed Reality Academy tutorials were designed with HoloLens (1st gen) and Mixed Reality Immersive Headsets in mind. 因此,对于仍在寻求这些设备的开发指导的开发人员而言,我们觉得很有必要保留这些教程。As such, we feel it is important to leave these tutorials in place for developers who are still looking for guidance in developing for those devices. 我们 不会 在这些教程中更新 HoloLens 2 所用的最新工具集或集成相关的内容。These tutorials will not be updated with the latest toolsets or interactions being used for HoloLens 2. 我们将维护这些教程,使之持续适用于支持的设备。They will be maintained to continue working on the supported devices. 已经为 HoloLens 2 发布了一系列新教程A new series of tutorials has been posted for HoloLens 2.

"注视" 是输入的第一种形式,它显示用户的意图和认知。Gaze is the first form of input and reveals the user's intent and awareness. MR Input 210 (亦称为 "项目资源管理器") 是深入了解 Windows Mixed Reality 的与注视相关的概念。MR Input 210 (aka Project Explorer) is a deep dive into gaze-related concepts for Windows Mixed Reality. 我们会将上下文感知添加到游标和全息影像,充分利用你的应用程序对用户外观的了解。We will be adding contextual awareness to our cursor and holograms, taking full advantage of what your app knows about the user's gaze.

这里有一个友好的 astronaut,可帮助你学习注视的概念。We have a friendly astronaut here to help you learn gaze concepts. 尊敬的基本知识 101中,我们有了一个简单的光标,只需跟随你的注视。In MR Basics 101, we had a simple cursor that just followed your gaze. 今天,我们要将一个步骤移到简单的游标之外:Today we're moving a step beyond the simple cursor:

  • 我们要做的是光标,我们的全息影像看起来很清楚:这两项操作都将根据用户的查找位置或用户 在查找的位置而变化。We're making the cursor and our holograms gaze-aware: both will change based on where the user is looking - or where the user is not looking. 这使它们可以识别其上下文。This makes them context-aware.
  • 我们会将反馈添加到游标和全息影像,以便为用户提供更多的目标目标上下文。We will add feedback to our cursor and holograms to give the user more context on what is being targeted. 这种反馈可以是音频和视觉对象。This feedback can be audio and visual.
  • 我们将向你展示用于帮助用户达到更小目标的目标技术。We'll show you targeting techniques to help users hit smaller targets.
  • 我们将向你展示如何使用定向指示器将用户的注意力吸引到你的全息影像。We'll show you how to draw the user's attention to your holograms with a directional indicator.
  • 我们将指导你在世界各地四处移动时,与用户一起使用全息影像。We'll teach you techniques to take your holograms with the user as she moves around in your world.

重要

以下各章中嵌入的视频使用较旧版本的 Unity 和混合现实工具包记录。The videos embedded in each of the chapters below were recorded using an older version of Unity and the Mixed Reality Toolkit. 虽然分步说明准确且最新,但你可能会看到处于过期状态的相应视频中的脚本和视觉对象。While the step-by-step instructions are accurate and current, you may see scripts and visuals in the corresponding videos that are out-of-date. 视频仍包含在 posterity 中,因为涵盖的概念仍适用。The videos remain included for posterity and because the concepts covered still apply.

设备支持Device support

课程Course HoloLensHoloLens 沉浸式头戴显示设备Immersive headsets
MR 输入 210:凝视MR Input 210: Gaze ✔️✔️ ✔️✔️

准备工作Before you start

必备条件Prerequisites

项目文件Project files

  • 下载项目所需的 文件Download the files required by the project. 需要 Unity 2017.2 或更高版本。Requires Unity 2017.2 or later.
  • 取消将文件存档到桌面或其他易于访问的位置。Un-archive the files to your desktop or other easy to reach location.

备注

如果要在下载之前查看源代码, 可在 GitHub 上找到。If you want to look through the source code before downloading, it's available on GitHub.

勘误表和说明Errata and Notes

  • 在 Visual Studio 中,需要禁用 "工具->选项" 下 >的 "仅我的代码" (取消选中) ,以便在代码中命中断点。In Visual Studio, "Just My Code" needs to be disabled (unchecked) under Tools->Options->Debugging in order to hit breakpoints in your code.

第1章-Unity 设置Chapter 1 - Unity Setup

目标Objectives

  • 针对 Hololens 开发优化 Unity。Optimize Unity for HoloLens development.
  • 导入资产并设置场景。Import assets and setup the scene.
  • 查看 HoloLens 中的 astronaut。View the astronaut in the HoloLens.

说明Instructions

  1. 启动 Unity。Start Unity.
  2. 选择 “新建项目”Select New Project.
  3. 将项目命名为 ModelExplorerName the project ModelExplorer.
  4. 输入 "位置" 作为先前未存档的 " 注视 " 文件夹。Enter location as the Gaze folder you previously un-archived.
  5. 请确保将项目设置为“3D”。Make sure the project is set to 3D.
  6. 单击“创建项目”。Click Create Project.

HoloLens 的 Unity 设置Unity settings for HoloLens

我们需要让 Unity 知道我们要导出的应用程序应创建 沉浸式视图 而不是2d 视图。We need to let Unity know that the app we are trying to export should create an immersive view instead of a 2D view. 为此,我们添加了 HoloLens 作为虚拟现实设备。We do that by adding HoloLens as a virtual reality device.

  1. 请参阅 " 编辑 > 项目设置" > Player"。Go to Edit > Project Settings > Player.
  2. 在 "播放器设置" 的 检查器面板 中,选择 " Windows 应用商店 " 图标。In the Inspector Panel for Player Settings, select the Windows Store icon.
  3. 展开“XR 设置”组。Expand the XR Settings group.
  4. 在“呈现”部分,选中“支持虚拟现实”复选框,添加新“虚拟现实 SDK 的”列表 。In the Rendering section, check the Virtual Reality Supported checkbox to add a new Virtual Reality SDKs list.
  5. 验证列表中是否显示“Windows 混合现实”。Verify that Windows Mixed Reality appears in the list. 如果没有,请选择 + 列表底部的 "" 按钮,然后选择 " Windows 全息"。If not, select the + button at the bottom of the list and choose Windows Holographic.

接下来,我们需要将脚本后端设置为 .NET。Next, we need to set our scripting backend to .NET.

  1. 请参阅 " 编辑 > 项目设置" > Player " (你仍可以从上一步) 执行此操作。Go to Edit > Project Settings > Player (you may still have this up from the previous step).
  2. 在 "播放器设置" 的 检查器面板 中,选择 " Windows 应用商店 " 图标。In the Inspector Panel for Player Settings, select the Windows Store icon.
  3. 在 "其他设置" "配置" 部分中,确保 "脚本后端" 设置为 " .net "In the Other Settings Configuration section, make sure that Scripting Backend is set to .NET

最后,我们将更新质量设置,以便在 HoloLens 上实现更快的性能。Finally, we'll update our quality settings to achieve a fast performance on HoloLens.

  1. 请参阅 编辑 > 项目设置 > 质量Go to Edit > Project Settings > Quality.
  2. 在 Windows 应用商店图标下,单击 默认 行中的向下箭头。Click on downward pointing arrow in the Default row under the Windows Store icon.
  3. 对于 Windows 应用商店应用,请选择 "非常低"。Select Very Low for Windows Store Apps.

导入项目资产Import project assets

  1. 右键单击 "项目" 面板中的 "资产" 文件夹。Right click the Assets folder in the Project panel.
  2. 单击 " 导入包 > 自定义包"。Click on Import Package > Custom Package.
  3. 导航到下载的项目文件,然后单击 " ModelExplorer. unitypackage"。Navigate to the project files you downloaded and click on ModelExplorer.unitypackage.
  4. 单击“打开”。Click Open.
  5. 加载包后,单击 " 导入 " 按钮。After the package loads, click on the Import button.

设置场景Setup the scene

  1. 在层次结构中,删除 主摄像机In the Hierarchy, delete the Main Camera.
  2. HoloToolkit 文件夹中,打开 " 输入 " 文件夹,然后打开 " prototyping " 文件夹。In the HoloToolkit folder, open the Input folder, then open the Prefabs folder.
  3. MixedRealityCameraParent Prefab 从 prototyping 文件夹拖放到 层次结构 中。Drag and drop the MixedRealityCameraParent prefab from the Prefabs folder into the Hierarchy.
  4. 右键单击层次结构中的 定向光 ,然后选择 " 删除"。Right-click the Directional Light in the Hierarchy and select Delete.
  5. 全息影像 文件夹中,将以下资产拖放到 层次结构 的根中:In the Holograms folder, drag and drop the following assets into the root of the Hierarchy:
    • AstroManAstroMan
    • 光线Lights
    • SpaceAudioSourceSpaceAudioSource
    • SpaceBackgroundSpaceBackground
  6. 启动 播放模式 ▶查看 astronaut。Start Play Mode ▶ to view the astronaut.
  7. 再次单击 播放模式停止Click Play Mode ▶ again to Stop.
  8. 全息影像 文件夹中,找到 Fitbox 资产,并将其拖到 层次结构 的根。In the Holograms folder, find the Fitbox asset and drag it to the root of the Hierarchy.
  9. 在 "层次结构" 面板中选择 " Fitbox "。Select the Fitbox in the Hierarchy panel.
  10. 在 "检查器" 面板中,将 AstroMan 集合从 层次结构 中拖到 Fitbox 的 全息图集合 属性中。Drag the AstroMan collection from the Hierarchy to the Hologram Collection property of the Fitbox in the Inspector panel.

保存项目Save the project

  1. 保存新场景: File > 将场景另存为Save the new scene: File > Save Scene As.
  2. 单击 " 新建文件夹 ",然后将文件夹命名为 " 场景"。Click New Folder and name the folder Scenes.
  3. 将该文件命名为 "ModelExplorer" 并将其保存在 幕后 文件夹中。Name the file “ModelExplorer” and save it in the Scenes folder.

生成项目Build the project

  1. 在 Unity 中,选择 " 文件 > 生成设置"。In Unity, select File > Build Settings.
  2. 单击 " 添加打开的场景 " 添加场景。Click Add Open Scenes to add the scene.
  3. 选择 "平台" 列表中的 "通用 Windows 平台",然后单击 "切换平台"。Select Universal Windows Platform in the Platform list and click Switch Platform.
  4. 如果要专门针对 HoloLens 进行开发,请将 " 目标设备 " 设置为 " hololens"。If you're specifically developing for HoloLens, set Target device to HoloLens. 否则,请将其留在 任何设备 上。Otherwise, leave it on Any device.
  5. 确保将 " 生成类型 " 设置为 " D3D ",并将 " Sdk " 设置为 " 最新安装 的 (,这应是 SDK 16299 或更高) 版本Ensure Build Type is set to D3D and SDK is set to Latest installed (which should be SDK 16299 or newer).
  6. 单击“生成”。Click Build.
  7. 创建名为 "App" 的 新文件夹Create a New Folder named "App".
  8. 单击 应用 文件夹。Single click the App folder.
  9. 按 " 选择文件夹"。Press Select Folder.

当 Unity 完成后,将显示文件资源管理器窗口。When Unity is done, a File Explorer window will appear.

  1. 打开 应用程序 文件夹。Open the App folder.
  2. 打开 ModelExplorer Visual Studio 解决方案Open the ModelExplorer Visual Studio Solution.

如果部署到 HoloLens:If deploying to HoloLens:

  1. 使用 Visual Studio 中的顶部工具栏,将目标从 "调试" 更改为 " 发布 ",将 "从 ARM" 更改为 " x86"。Using the top toolbar in Visual Studio, change the target from Debug to Release and from ARM to x86.
  2. 单击 "本地计算机" 按钮旁的下拉箭头,然后选择 " 远程计算机"。Click on the drop down arrow next to the Local Machine button, and select Remote Machine.
  3. 输入 HoloLens 设备 IP 地址 ,并将身份验证模式设置为 通用 (未加密协议)Enter your HoloLens device IP address and set Authentication Mode to Universal (Unencrypted Protocol). 单击“选择” 。Click Select. 如果你不知道设备 IP 地址,请在 "设置" 中查找 " > 网络 & Internet > 高级选项"。If you do not know your device IP address, look in Settings > Network & Internet > Advanced Options.
  4. 在顶部菜单栏中,单击 " 调试-> 启动而不调试 " 或按 Ctrl + F5In the top menu bar, click Debug -> Start Without debugging or press Ctrl + F5. 如果这是首次部署到设备,则需要将 其与 Visual Studio 配对If this is the first time deploying to your device, you will need to pair it with Visual Studio.
  5. 当应用程序已部署后,使用 选择手势 关闭 FitboxWhen the app has deployed, dismiss the Fitbox with a select gesture.

如果要部署到沉浸式耳机:If deploying to an immersive headset:

  1. 使用 Visual Studio 中的顶部工具栏,将目标从 "调试" 更改为 " 发布 ",将 "从 ARM" 更改为 " x64"。Using the top toolbar in Visual Studio, change the target from Debug to Release and from ARM to x64.
  2. 确保将部署目标设置为 " 本地计算机"。Make sure the deployment target is set to Local Machine.
  3. 在顶部菜单栏中,单击 " 调试-> 启动而不调试 " 或按 Ctrl + F5In the top menu bar, click Debug -> Start Without debugging or press Ctrl + F5.
  4. 当应用程序已部署后,通过将触发器拖到运动控制器上来关闭 FitboxWhen the app has deployed, dismiss the Fitbox by pulling the trigger on a motion controller.

第2章-游标和目标反馈Chapter 2 - Cursor and target feedback

目标Objectives

  • 游标视觉对象设计和行为。Cursor visual design and behavior.
  • 基于注视的光标反馈。Gaze-based cursor feedback.
  • 基于注视的全息影像反馈。Gaze-based hologram feedback.

我们将使用一些游标设计原则,即:We're going to base our work on some cursor design principles, namely:

  • 游标始终存在。The cursor is always present.
  • 不要让光标变得太小或太大。Don't let the cursor get too small or big.
  • 避免阻碍内容。Avoid obstructing content.

说明Instructions

  1. HoloToolkit\Input\Prefabs 文件夹中,找到 " InputManager " 资产。In the HoloToolkit\Input\Prefabs folder, find the InputManager asset.
  2. InputManager 拖放到 层次结构 中。Drag and drop the InputManager onto the Hierarchy.
  3. HoloToolkit\Input\Prefabs 文件夹中,找到 光标 资产。In the HoloToolkit\Input\Prefabs folder, find the Cursor asset.
  4. 光标 拖放到 层次结构 中。Drag and drop the Cursor onto the Hierarchy.
  5. 选择 层次结构 中的 InputManager 对象。Select the InputManager object in the Hierarchy.
  6. 光标 对象从 层次结构 中拖放到 检查器 底部的 InputManager 的 SimpleSinglePointerSelector游标 字段。Drag the Cursor object from the Hierarchy into the InputManager's SimpleSinglePointerSelector's Cursor field, at the bottom of the Inspector.

简单的单指针选择器设置

生成和部署Build and Deploy

  1. 文件 > 生成设置 重新生成应用。Rebuild the app from File > Build Settings.
  2. 打开 应用程序文件夹Open the App folder.
  3. 打开 ModelExplorer Visual Studio 解决方案Open the ModelExplorer Visual Studio Solution.
  4. 单击 " 调试-> 启动但不调试 " 或按 Ctrl + F5Click Debug -> Start Without debugging or press Ctrl + F5.
  5. 观察如何绘制光标,以及如何在触摸全息图时更改其外观。Observe how the cursor is drawn, and how it changes appearance if it is touching a hologram.

说明Instructions

  1. 在 "层次结构" 面板中,展开 " AstroMan -> GEO_G -> Back_Center " 对象。In the Hierarchy panel, expand the AstroMan->GEO_G->Back_Center object.
  2. 双击 " Interactible.cs " 在 Visual Studio 中打开它。Double click on Interactible.cs to open it in Visual Studio.
  3. 取消注释 IFocusable. OnFocusEnter 中的行 ( # B1IFocusable ( # B3 in Interactible.cs 中的回调。Uncomment the lines in the IFocusable.OnFocusEnter() and IFocusable.OnFocusExit() callbacks in Interactible.cs. 当焦点 (通过注视或控制器) 进入并退出特定 GameObject 的碰撞器时,混合现实工具包的 InputManager 将调用这些方法。These are called by the Mixed Reality Toolkit's InputManager when focus (either by gaze or by controller pointing) enters and exits the specific GameObject's collider.
/* TODO: DEVELOPER CODING EXERCISE 2.d */

void IFocusable.OnFocusEnter()
{
    for (int i = 0; i < defaultMaterials.Length; i++)
    {
        // 2.d: Uncomment the below line to highlight the material when gaze enters.
        defaultMaterials[i].EnableKeyword("_ENVIRONMENT_COLORING");
    }
}

void IFocusable.OnFocusExit()
{
    for (int i = 0; i < defaultMaterials.Length; i++)
    {
        // 2.d: Uncomment the below line to remove highlight on material when gaze exits.
        defaultMaterials[i].DisableKeyword("_ENVIRONMENT_COLORING");
    }
}

备注

我们使用 EnableKeywordDisableKeyword 更高版本。We use EnableKeyword and DisableKeyword above. 若要在自己的应用程序中使用工具包的标准着色器来使用这些应用程序,需要遵循 Unity 指导原则,通过脚本访问材料In order to make use of these in your own app with the Toolkit's Standard shader, you'll need to follow the Unity guidelines for accessing materials via script. 在这种情况下,我们已在 "资源" 文件夹中包含了所需的 突出显示材料的三个变体 (查找姓名) 中突出显示的三个材料。In this case, we've already included the three variants of highlighted material needed in the Resources folder (look for the three materials with highlight in the name).

生成和部署Build and Deploy

  1. 与之前一样,生成项目并将其部署到 HoloLens。As before, build the project and deploy to the HoloLens.
  2. 观察注视的目标是对象时,发生的情况。Observe what happens when the gaze is aimed at an object and when it's not.

第3章-目标技术Chapter 3 - Targeting Techniques

目标Objectives

  • 更轻松地定位全息影像。Make it easier to target holograms.
  • 稳定的自然头运动。Stabilize natural head movements.

说明Instructions

  1. 在 " 层次结构 " 面板中,选择 " InputManager " 对象。In the Hierarchy panel, select the InputManager object.
  2. 在 " 检查器 " 面板中,找到 " 注视" 稳定 的脚本。In the Inspector panel, find the Gaze Stabilizer script. 如果要查看,请单击它以在 Visual Studio 中打开。Click it to open in Visual Studio, if you want to take a look.
    • 此脚本将循环访问 Raycast 数据的示例,并帮助使用户看得更稳定,以实现精确定位。This script iterates over samples of Raycast data and helps stabilize the user's gaze for precision targeting.
  3. 检查器 中,可以编辑 存储的稳定性示例 值。In the Inspector, you can edit the Stored Stability Samples value. 此值表示稳定程序为计算稳定值而迭代的样本数。This value represents the number of samples that the stabilizer iterates on to calculate the stabilized value.

第4章-方向指示器Chapter 4 - Directional indicator

目标Objectives

  • 在光标处添加方向指示器以帮助查找全息影像。Add a directional indicator on the cursor to help find holograms.

说明Instructions

我们将使用 DirectionIndicator.cs 文件,该文件将:We're going to use the DirectionIndicator.cs file which will:

  1. 如果用户不 gazing 在全息影像上,则显示方向指示器。Show the directional indicator if the user is not gazing at the holograms.
  2. 如果用户在全息影像上 gazing,则隐藏方向指示器。Hide the directional indicator if the user is gazing at the holograms.
  3. 更新方向指示器以指向全息影像。Update the directional indicator to point to the holograms.

让我们开始吧。Let's get started.

  1. 单击 "层次结构" 面板中的 AstroMan 对象,然后 单击箭头 将其展开。Click on the AstroMan object in the Hierarchy panel and click the arrow to expand it.
  2. 在 "层次结构" 面板中,选择 " AstroMan" 下的 DirectionalIndicator 对象。In the Hierarchy panel, select the DirectionalIndicator object under AstroMan.
  3. 检查器 面板中,单击 " 添加组件 " 按钮。In the Inspector panel, click the Add Component button.
  4. 在菜单中,键入 "搜索框 方向" 指示器In the menu, type in the search box Direction Indicator. 选择搜索结果。Select the search result.
  5. 在 "层次结构" 面板中,将 cursor 对象拖放到 检查器cursor 属性上。In the Hierarchy panel, drag and drop the Cursor object onto the Cursor property in the Inspector.
  6. 在 "项目" 面板中,将 " DirectionalIndicator " 资产 拖放到 检查器 的 "方向指示器" 属性中。In the Project panel, in the Holograms folder, drag and drop the DirectionalIndicator asset onto the Directional Indicator property in the Inspector.
  7. 生成并部署应用。Build and deploy the app.
  8. 观看方向指示器对象如何帮助您找到 astronaut。Watch how the directional indicator object helps you find the astronaut.

第5章-BillboardingChapter 5 - Billboarding

目标Objectives

  • 使用 billboarding,让全息影像始终面向你。Use billboarding to have holograms always face towards you.

我们将使用 Billboard.cs 文件来保持 GameObject,使其始终面向用户。We will be using the Billboard.cs file to keep a GameObject oriented such that it is facing the user at all times.

  1. 在 " 层次结构 " 面板中,选择 " AstroMan " 对象。In the Hierarchy panel, select the AstroMan object.
  2. 检查器 面板中,单击 " 添加组件 " 按钮。In the Inspector panel, click the Add Component button.
  3. 在菜单中,在 "搜索" 框中键入 " 布告栏"。In the menu, type in the search box Billboard. 选择搜索结果。Select the search result.
  4. 检查器 中,将 透视轴 设置为 YIn the Inspector set the Pivot Axis to Y.
  5. 试试看!Try it! 像以前一样构建并部署应用程序。Build and deploy the app as before.
  6. 了解布告栏对象如何面对您如何更改视点。See how the Billboard object faces you no matter how you change the viewpoint.
  7. 立即从 AstroMan 中删除脚本。Delete the script from the AstroMan for now.

第6章-Tag-AlongChapter 6 - Tag-Along

目标Objectives

  • 使用 Tag-Along 让我们的全息影像围绕空间。Use Tag-Along to have our holograms follow us around the room.

当我们处理此问题时,我们将通过以下设计约束指导:As we work on this issue, we'll be guided by the following design constraints:

  • 内容应始终一目了然。Content should always be a glance away.
  • 内容不应采用的方式。Content should not be in the way.
  • 打印头锁定内容不舒服。Head-locking content is uncomfortable.

此处使用的解决方案是使用 "标记式" 方法。The solution used here is to use a "tag-along" approach.

标记靠对象从不完全离开用户的视图。A tag-along object never fully leaves the user's view. 可以将标记作为附加到用户 head 的对象(通过橡胶带)。You can think of the a tag-along as being an object attached to the user's head by rubber bands. 随着用户的移动,内容将一直滑入视图边缘,无需完全离开。As the user moves, the content will stay within an easy glance by sliding towards the edge of the view without completely leaving. 当用户 gazes 沿标记的对象时,它会更全面地进入视图。When the user gazes towards the tag-along object, it comes more fully into view.

我们将使用 SimpleTagalong.cs 文件,该文件将:We're going to use the SimpleTagalong.cs file which will:

  1. 确定 Tag-Along 对象是否处于相机边界内。Determine if the Tag-Along object is within the camera bounds.
  2. 如果不在视图的 "被" Tag-Along 截If not within the view frustum, position the Tag-Along to partially within the view frustum.
  3. 否则,将 Tag-Along 定位到用户的默认距离。Otherwise, position the Tag-Along to a default distance from the user.

为此,我们必须先更改 Interactible.cs 脚本,以便调用 TagalongActionTo do this, we first must change the Interactible.cs script to call the TagalongAction.

  1. 完成编码练习6,编辑 Interactible.cs 。 (取消注释行84到 87) 。Edit Interactible.cs by completing coding exercise 6.a (uncommenting lines 84 to 87).
/* TODO: DEVELOPER CODING EXERCISE 6.a */
// 6.a: Uncomment the lines below to perform a Tagalong action.
if (interactibleAction != null)
{
    interactibleAction.PerformAction();
}

当点击全息影像时,与 Interactible.cs 配对的 InteractibleAction.cs 脚本将执行自定义操作。The InteractibleAction.cs script, paired with Interactible.cs performs custom actions when you tap on holograms. 在这种情况下,我们将专门用于标记。In this case, we'll use one specifically for tag-along.

  • 在 " 脚本 " 文件夹中,单击 " TagalongAction.cs 资产" 以在 Visual Studio 中打开。In the Scripts folder click on TagalongAction.cs asset to open in Visual Studio.
  • 完成编码练习,或将其更改为:Complete the coding exercise or change it to this:
    • 层次结构 的顶部,在搜索栏中键入 " ChestButton_Center ",然后选择结果。At the top of the Hierarchy, in the search bar type ChestButton_Center and select the result.
    • 检查器 面板中,单击 " 添加组件 " 按钮。In the Inspector panel, click the Add Component button.
    • 在菜单中,在 "搜索" 框中键入 " Tagalong" 操作In the menu, type in the search box Tagalong Action. 选择搜索结果。Select the search result.
    • 全息影像 文件夹中查找 Tagalong 资产。In Holograms folder find the Tagalong asset.
    • 选择 层次结构 中的 ChestButton_Center 对象。Select the ChestButton_Center object in the Hierarchy. TagAlong 对象从 " 项目 " 面板中拖放到 " 要 TagAlong 的对象 " 属性中。Drag and drop the TagAlong object from the Project panel onto the Inspector into the Object To Tagalong property.
    • Tagalong 操作 对象从 检查器 拖到 Interactible 脚本的 " Interactible 操作" 字段。Drag the Tagalong Action object from the Inspector into the Interactible Action field on the Interactible script.
  • 双击 TagalongAction 脚本,在 Visual Studio 中将其打开。Double click the TagalongAction script to open it in Visual Studio.

Interactible 设置

我们需要添加以下内容:We need to add the following:

  • 将功能添加到 TagalongAction 脚本中的 PerformAction 函数 (从 InteractibleAction) 继承。Add functionality to the PerformAction function in the TagalongAction script (inherited from InteractibleAction).
  • 将 billboarding 添加到 gazed 对象,并将透视轴设置为 XY。Add billboarding to the gazed-upon object, and set the pivot axis to XY.
  • 然后,将简单的 Tag-Along 添加到对象。Then add simple Tag-Along to the object.

下面是 TagalongAction.cs 的解决方案:Here's our solution, from TagalongAction.cs:

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using HoloToolkit.Unity;
using UnityEngine;

public class TagalongAction : InteractibleAction
{
    [SerializeField]
    [Tooltip("Drag the Tagalong prefab asset you want to display.")]
    private GameObject objectToTagalong;

    private void Awake()
    {
        if (objectToTagalong != null)
        {
            objectToTagalong = Instantiate(objectToTagalong);
            objectToTagalong.SetActive(false);

            /* TODO: DEVELOPER CODING EXERCISE 6.b */

            // 6.b: AddComponent Billboard to objectToTagAlong,
            // so it's always facing the user as they move.
            Billboard billboard = objectToTagalong.AddComponent<Billboard>();

            // 6.b: AddComponent SimpleTagalong to objectToTagAlong,
            // so it's always following the user as they move.
            objectToTagalong.AddComponent<SimpleTagalong>();

            // 6.b: Set any public properties you wish to experiment with.
            billboard.PivotAxis = PivotAxis.XY; // Already the default, but provided in case you want to edit
        }
    }

    public override void PerformAction()
    {
        // Recommend having only one tagalong.
        if (objectToTagalong == null || objectToTagalong.activeSelf)
        {
            return;
        }

        objectToTagalong.SetActive(true);
    }
}
  • 试试看!Try it! 生成并部署应用。Build and deploy the app.
  • 观察内容如何沿着注视点的中心,但不会不断地进行阻止。Watch how the content follows the center of the gaze point, but not continuously and without blocking it.