3.保存、检索和共享 Azure 空间定位点3. Saving, retrieving, and sharing Azure Spatial Anchors

本教程将介绍如何通过将定位点 ID 保存到 HoloLens 2 的存储,来保存多个应用会话中的 Azure 空间定位点。In this tutorial, you will learn how to save Azure Spatial Anchors across multiple app sessions by saving the anchor ID to the HoloLens 2's storage. 此外,介绍如何与其他设备共享此定位点 ID,以便在多个设备上对齐定位点。You will also learn how to share this anchor ID to other devices for a multi-device anchor alignment.

目标Objectives

  • 了解如何在多个应用会话之间实现空间对齐。Learn how to achieve spatial alignment across multiple app sessions.
  • 了解如何在多台设备之间实现空间对齐。Learn how to achieve spatial alignment between multiple devices.

准备场景Preparing the scene

在“层次结构”窗口中,展开“ButtonParent”对象。In the Hierarchy window, expand the ButtonParent object. 选择最后四个子按钮对象。Select the last four child button objects. 在“检查器”窗口中,选中“名称”字段旁的复选框以使所有对象处于活动状态。In the Inspector window, check the checkbox next to the name field to make all the objects active.

选中并激活了先前处于非活动状态的按钮对象的 Unity

在“层次结构”窗口中,选择“ButtonParent”对象。In the Hierarchy window, select the ButtonParent objects. 然后在“检查器”窗口中,找到 GridObjectCollection 组件并单击“更新集合”按钮,以更新所有 ButtonParent 对象的子对象的位置 。Then in the Inspector window, locate the GridObjectCollection component and click the Update Collection button to update the position of all the ButtonParent object's child objects.

更新了 GridObjectCollection 组件的 Unity

在应用会话之间保留 Azure 空间定位点Persisting Azure Spatial Anchors between app sessions

本部分介绍如何在 HoloLens 2 本地磁盘中保存以及从中检索 Azure 空间定位点 ID。In this section, you will learn how to save and retrieve the Azure Anchor ID to and from the HoloLens' local disk. 通过这些操作,你可以在 Azure 中查询不同应用会话中的同一定位点 ID。This will allow you to query Azure for the same anchor ID between different app sessions. 它使定位的全息影像能够位于上一个应用会话中的相同位置。It will enable the anchored holograms to be positioned at the same location as in the previous app session.

在“层次结构”窗口中,展开“ButtonParent”对象并找到名为 SaveAzureAnchorIdToDisk 和 GetAzureAnchorIdFromDisk 的两个按钮 :In the Hierarchy window, expand the ButtonParent object and locate the two buttons named SaveAzureAnchorIdToDisk and GetAzureAnchorIdFromDisk:

选中了 SaveAzureAnchorIdToDisk 和 GetAzureAnchorIdFromDisk 按钮对象的 Unity

遵循上一篇教程的配置按钮以操作场景说明中的相同步骤,在这两个按钮中的每个按钮上配置“可交互(脚本)”组件:Follow the same steps as in the configuring the buttons to operate the scene instructions from the previous tutorial to configure the Interactable (Script) component on each of the two buttons:

  • 对于“SaveAzureAnchorIdToDisk”按钮对象,请分配“AnchorModuleScript”>“SaveAzureAnchorIdToDisk ()”函数。 For the SaveAzureAnchorIdToDisk button object, assign the AnchorModuleScript > SaveAzureAnchorIdToDisk () function.
  • 对于“GetAzureAnchorIdFromDisk”按钮对象,请分配“AnchorModuleScript”>“GetAzureAnchorIdFromDisk ()”函数。 For the GetAzureAnchorIdFromDisk button object, assign the AnchorModuleScript > GetAzureAnchorIdFromDisk () function.

如果在 HoloLens 中生成更新的应用,则现在可以通过保存 Azure 定位点 ID 在不同的应用会话中保留 Azure 空间定位点。If you build the updated app to your HoloLens, you can now persist Azure Spatial Anchors between app sessions by saving the Azure Anchor ID. 若要进行测试,可执行以下步骤:To test it out, you can follow these steps:

  1. 将“漫游者探测器”移到所需位置Move the Rover Explorer to the desired location
  2. 启动 Azure 会话Start Azure Session
  3. 创建 Azure 定位点(在“漫游者探测器”位置创建定位点)Create Azure Anchor (creates anchors at the location of the Rover Explorer)
  4. 将 Azure 定位点 ID 保存到磁盘Save Azure Anchor ID to Disk
  5. 重启应用Restart the app
  6. 从磁盘中获取 Azure 定位点(加载刚刚保存的定位点 ID)Get Azure Anchor from Disk (loads the anchor ID you just saved)
  7. 启动 Azure 会话Start Azure Session
  8. 查找 Azure 定位点(将“漫游者探测器”定位到步骤 3 所述的位置)Find Azure Anchor (positions the Rover Explorer at the location from step 3)

备注

若要完全重启应用,在退出沉浸式应用视图之后,需要先关闭混合现实主页中的应用窗口,然后从“开始”菜单重新启动应用。To fully restart the app, after exiting the immersive app view, the app window in the mixed reality home needs to be closed before relaunching it from the Start menu. 如需更多详细信息,可以参阅在 HoloLens 上使用应用文档。For additional details, you can refer to the Using apps on HoloLens documentation.

在设备之间共享 Azure 空间定位点Sharing Azure Spatial Anchors between devices

本部分介绍如何在多个设备之间共享 Azure 定位点 ID。In this section, you will learn how to share the Azure Anchor ID between multiple devices. 这样,多个设备便可以在 Azure 中查询同一个定位点 ID,从而可在空间上对齐定位点定的全息影像。This will allow multiple devices to query Azure for the same anchor ID, allowing the anchored holograms to be spatially aligned. 空间对齐(即,在多个设备之间的同一物理位置查看相同的全息影像)是 HoloLens 2 中本地共享体验的关键所在。Spatial alignment, i.e., seeing the same holograms in the same physical location between multiple devices, is key to local shared experiences in the HoloLens 2.

可以通过多种方法在设备之间转移 Azure 定位点 ID,包括多用户功能教程系列中所述的方法。There are many ways to transfer Azure Anchor IDs between devices, including methods outlined in the Multi-user capabilities tutorials series. 此示例将使用一个简单的 Web 服务在设备之间上传和下载定位点 ID。In this example, you will use a simple web service to upload and download anchor IDs between devices.

在“层次结构”窗口中,展开“ButtonParent”对象。In the Hierarchy window, expand the ButtonParent object. 找到名为 ShareAzureAnchorIdToNetwork 和 GetAzureAnchorIdFromNetwork 的两个按钮 :Locate the two buttons named ShareAzureAnchorIdToNetwork and GetAzureAnchorIdFromNetwork:

选中了 ShareAzureAnchorIdToNetwork 和 GetAzureAnchorIdFromNetwork 按钮对象的 Unity

遵循上一篇教程的配置按钮以操作场景说明中的相同步骤,在这两个按钮中的每个按钮上配置“可交互(脚本)”组件:Follow the same steps as in the configuring the buttons to operate the scene instructions from the previous tutorial to configure the Interactable (Script) component on each of the two buttons:

  • 对于“ShareAzureAnchorIdToNetwork”对象,请分配“AnchorModuleScript”>“ShareAzureAnchorIdToNetwork ()”函数。 For the ShareAzureAnchorIdToNetwork object, assign the AnchorModuleScript > ShareAzureAnchorIdToNetwork () function.
  • 对于“GetAzureAnchorIdFromNetwork”对象,请分配“AnchorModuleScript”>“GetAzureAnchorIdFromNetwork ()”函数。 For the GetAzureAnchorIdFromNetwork object, assign the AnchorModuleScript > GetAzureAnchorIdFromNetwork () function.

如果在两个 HoloLens 设备上生成更新的应用,则现在可以通过共享 Azure 定位点 ID 来实现空间对齐。If you build the updated app to two HoloLens devices, you can now achieve spatial alignment by sharing the Azure Anchor ID. 若要进行测试,可执行以下步骤:To test it out, you can follow these steps:

  1. 在 HoloLens 设备 1 上:将“漫游者探测器”移到所需位置。On HoloLens device 1: Move the Rover Explorer to the desired location.
  2. 在 HoloLens 设备 1 上:启动 Azure 会话。On HoloLens device 1: Start Azure Session.
  3. 在 HoloLens 设备 1 上:创建 Azure 定位点(在“漫游者探测器”位置创建定位点)。On HoloLens device 1: Create Azure Anchor (creates anchors at the location of the Rover Explorer).
  4. 在 HoloLens 设备 1 上:在网络中共享 Azure 定位点 ID。On HoloLens device 1: Share Azure Anchor ID to Network.
  5. 在 HoloLens 设备 2 上:启动应用。On HoloLens device 2: Start the app.
  6. 在 HoloLens 设备 2 上:从网络中获取共享的定位点 ID(提取刚刚从 HoloLens 设备 1 共享的定位点 ID)。On HoloLens device 2: Get Shared Anchor ID from Network (fetches the anchor ID just shared from HoloLens device 1).
  7. 在 HoloLens 设备 2 上:启动 Azure 会话。On HoloLens device 2: Start Azure Session.
  8. 在 HoloLens 设备 2 上:查找 Azure 定位点(将“漫游者探测器”定位到步骤 3 所述的位置)。On HoloLens device 2: Find Azure Anchor (positions the Rover Explorer at the location from step 3).

提示

如果只有一个 HoloLens 设备,仍可以通过重启应用来测试该功能,而无需使用另一个 HoloLens 设备。If you only have one HoloLens, you can still test the functionality by restarting the app instead of using a second HoloLens device.

祝贺Congratulations

在本教程中,你已了解如何通过将 Azure 空间定位点 ID 保存到 HoloLens 上的本地磁盘,在不同的应用会话中以及每次重启应用后保留 Azure 空间定位点。In this tutorial, you learned how to persist Azure Spatial Anchors between app sessions and app restarts by saving the Azure Spatial Anchor ID to the local disk on HoloLens. 此外,你还了解了如何在基本多用户静态全息影像共享体验中的多个设备之间共享 Azure 空间定位点。You also learned how to share Azure Spatial Anchors between multiple devices for a basic multi-user, static hologram shared experience.

下一篇教程将会介绍如何为用户提供实时反馈。In the next tutorial, you will learn how to provide users with real-time feedback. 此反馈包括有关定位点的创建、环境质量识别以及 Azure 会话状态的信息。This feedback will include information about Anchor creation, the quality of environment understanding, and the Azure session's state. 如果没有反馈,用户可能不知道某个定位点是否已成功上传到 Azure,而不管环境质量是足以创建定位点还是处于当前状态。Without feedback, users may not know whether an anchor has successfully been uploaded to Azure, whether the quality of the environment is sufficient for anchor creation, or the current state.