8.使用眼动跟踪8. Using eye-tracking

在本教程中,你将了解如何为 HoloLens 2 启用眼动跟踪,以及在用户查看对象时如何将眼动跟踪添加到对象以触发操作。In this tutorial, you will learn how to enable eye-tracking for HoloLens 2 and add eye-tracking to objects to trigger actions when the user looks at the objects.

备注

如果你也要将此项目部署到 HoloLens(第一代),请注意,只有 HoloLens 2 支持眼动跟踪。If you are also deploying this project to HoloLens (1st generation), note that eye-tracking is only supported on HoloLens 2.

目标Objectives

  • 了解如何为 HoleLens 2 启用眼动跟踪Learn how to enable eye-tracking for HoleLens 2
  • 了解如何使用眼动跟踪触发操作Learn how to use eye-tracking to trigger action

确保启用了“眼睛凝视输入”功能Ensuring the Eye Gaze Input capability is enabled

在 Unity 菜单中,选择“混合现实工具包”>“实用工具”>“配置 Unity 项目”,打开“MRTK 项目配置器”窗口,然后在“UWP 功能”部分中,验证“启用眼睛凝视输入功能”是否灰显 :In the Unity menu, select Mixed Reality Toolkit > Utilities > Configure Unity Project to open the MRTK Project Configurator window, then in the UWP Capabilities section, verify that Enable Eye Gaze Input Capability is greyed out:

Unity“MRTK 项目配置器”窗口

备注

当你在本系列教程开头配置 Unity 项目时,应用 MRTK 项目配置器设置指令期间应该已经启用了眼睛凝视输入功能。The Gaze Input capability should have been enabled during the Apply the MRTK Project Configurator settings instructions when you configured the Unity project at the beginning of this tutorial series. 但如果未启用此功能,请确保立即启用。However, if it is not enabled, make sure you enable it now.

在凝视提供者中启用基于眼睛的凝视Enabling eye based gaze in the gaze provider

在“层次结构”窗口中选择“MixedRealityToolkit”对象,然后在“检查器”窗口中选择“MixedRealityToolkit”>“输入”选项卡,并执行以下步骤 :In the Hierarchy window, select the MixedRealityToolkit object, then in the Inspector window, select the MixedRealityToolkit > Input tab and take the following steps:

  • 克隆 DefaultHoloLens2InputSystemProfile 并为其指定合适的名称,例如 GettingStarted_HoloLens2InputSystemProfileClone the DefaultHoloLens2InputSystemProfile and give it a suitable name, for example, GettingStarted_HoloLens2InputSystemProfile
  • 展开“指针”部分Expand the Pointers section
  • 克隆 DefaultMixedRealityPointerProfile 并为其指定合适的名称,例如 GettingStarted_MixedRealityPointerProfileClone the DefaultMixedRealityPointerProfile and give it a suitable name, for example, GettingStarted_MixedRealityPointerProfile
  • 找到“凝视设置”部分,并选中“是否启用了眼动跟踪”复选框 Locate the Gaze Settings section and check the Is Eye Tracking Enabled checkbox

应用了新创建的配置文件并启用了眼动跟踪的 Unity MixedRealityToolkit 组件

提示

有关如何克隆 MRTK 配置文件的提示,可参阅配置 MRTK 配置文件中的说明。For a reminder on how to clone MRTK profiles, you can refer to the Configuring the MRTK profiles instructions.

为 Unity 编辑器启用模拟的眼动跟踪Enabling simulated eye-tracking for the Unity editor

在“层次结构”窗口中,选择“MixedRealityToolkit”对象,然后在“检查器”窗口中,导航到“输入”选项卡,然后执行以下操作 :In the Hierarchy window, select the MixedRealityToolkit object, then in the Inspector window, navigate to the Input tab, then:

  • 展开“输入数据提供者” > “输入模拟服务”部分 Expand the Input Data Providers > Input Simulation Service section
  • 克隆 DefaultMixedRealityInputSimulationProfile 并为其指定合适的名称,例如 GettingStarted_MixedRealityInputSimulationProfileClone the DefaultMixedRealityInputSimulationProfile and give it a suitable name, for example, GettingStarted_MixedRealityInputSimulationProfile
  • 找到“眼动模拟”部分,然后选中“模拟眼睛位置”复选框 Locate the Eye Simulation section and check the Simulate Eye Position checkbox

应用了新创建的配置文件并启用了眼动模拟的 Unity MixedRealityToolkit 组件

将眼动跟踪添加到对象Adding eye-tracking to objects

在“层次结构”窗口中,展开“RoverExplorer”>“按钮”对象,然后为三个子按钮对象中的每一个对象展开并选择“SeeItSayItLabel”>“TextMeshPro”对象 :In the Hierarchy window, expand the RoverExplorer > Buttons object, then for each of the three child button objects, expand and select the SeeItSayItLabel > TextMeshPro object:

选中了 TextMeshPro 对象的 Unity

当“检查器”窗口中的三个“TextMeshPro”对象仍处于选中状态时,请使用“添加组件”按钮将以下组件添加到所有选定对象:With the three TextMeshPro objects still selected, in the Inspector window, use the Add Component button to add the following components to all the selected objects:

  • 盒碰撞体组件Box Collider component
  • EyeTrackingTarget 组件EyeTrackingTarget component

选中了 TextMeshPro 对象并添加了组件的 Unity

在“层次结构”窗口中,选择“提示”>“SeeItSayItLabel”>“TextMeshPro”对象,然后按以下方式配置“EyeTrackingTarget”组件 :In the Hierarchy window, select the Hints > SeeItSayItLabel > TextMeshPro object, then configure the EyeTrackingTarget component as follows:

  • 在“On Look At Start ()”事件部分In the On Look At Start () event section
    • 单击小 + 图标以添加另一个事件Click the small + icon to add another event
    • 将对象本身(即相同的 TextMeshPro 对象)分配给“无(对象)”字段 Assign the object itself, i.e. the same TextMeshPro object, to the None (Object) field
    • 在“无函数”下拉列表中选择“TextMeshPro” > “float fontSize”,以便在触发事件时更新此属性 From the No Function dropdown, select TextMeshPro > float fontSize to update this property value when the event is triggered
    • 将参数设置为 0.06,以将 0.04 的当前字号增加 50%Set the argument to 0.06 to increase the current font size of 0.04 by 50%
  • 在“On Look Away ()”事件部分In the On Look Away () event section
    • 单击小 + 图标以添加另一个事件Click the small + icon to add another event
    • 将对象本身(即相同的 TextMeshPro 对象)分配给“无(对象)”字段 Assign the object itself, i.e. the same TextMeshPro object, to the None (Object) field
    • 在“无函数”下拉列表中选择“TextMeshPro” > “float fontSize”,以便在触发事件时更新此属性 From the No Function dropdown, select TextMeshPro > float fontSize to update this property value when the event is triggered
    • 将参数设置为 0.04 以将字号重置为 0.04Set the argument to 0.04 to reset the font size back to 0.04

选中了“提示 TextMeshPro 对象”并配置了 EyeTrackingTarget 组件的 Unity

对“分解”>“SeeItSayItLabel”>“TextMeshPro”对象和“重置”>“SeeItSayItLabel”>“TextMeshPro”对象重复此步骤 。Repeat this step for the Explode > SeeItSayItLabel > TextMeshPro object and the Reset > SeeItSayItLabel > TextMeshPro object.

如果你现在进入游戏模式,然后按住鼠标右键,同时移动鼠标,直到凝视命中的某个标签出现,你将看到字号增加了 50%,而鼠标移走时字号将重置回其原始大小:If you now enter Game mode and then press-and-hold the right mouse button while moving your mouse until the gaze hit's one of the labels, you will see the font size increase by 50% and reset back to its original size when looking away:

视线对准眼动跟踪目标“分解”按钮标签的 Unity“播放”模式分屏视图

祝贺Congratulations

在本教程中,你了解了如何为 HoloLens 2 启用眼动跟踪,以及在用户查看对象时如何将眼动跟踪添加到对象以触发操作。In this tutorial, you learned how to enable eye-tracking for HoloLens 2 and how to add eye-tracking to objects to trigger actions when the user looks at the objects.