2.開始使用 Azure Spatial Anchors2. Getting started with Azure Spatial Anchors

在此教學課程中,您將探索啟動和停止 Azure Spatial Anchors 工作階段,以及在單一裝置上建立、上傳和下載 Azure Spatial Anchors 所需的各種步驟。In this tutorial, you will explore the various steps required to start and stop an Azure Spatial Anchors session and to create, upload, and download Azure Spatial Anchors on a single device.

目標Objectives

  • 了解 HoloLens 2 的 Azure Spatial Anchors 開發基本概念Learn the fundamentals of developing with Azure Spatial Anchors for HoloLens 2
  • 了解如何建立空間錨點,並從 Azure 提取這些錨點Learn how to create spatial anchors and fetch them from Azure

建立和準備 Unity 專案Creating and preparing the Unity project

在本節中,您將建立新的 Unity 專案,並使該專案準備好進行 MRTK 開發。In this section, you will create a new Unity project and get it ready for MRTK development.

首先,請遵循初始化您的專案和部署第一個應用程式 (但不包括對您的裝置建置應用程式的指示),其中包括下列步驟:First, follow the Initializing your project and deploying your first application, excluding the Build your application to your device instructions, which includes the following steps:

  1. 建立 Unity 專案,並為其提供適當的名稱,例如「MRTK 教學課程」Creating the Unity project and give it a suitable name, for example, MRTK Tutorials
  2. 切換建置平台Switching the build platform
  3. 匯入 TextMeshPro 基本資源Importing the TextMeshPro Essential Resources
  4. 匯入混合實境工具組Importing the Mixed Reality Toolkit
  5. 設定 Unity 專案Configuring the Unity project
  6. 建立和設定場景並為場景提供適當的名稱,例如 AzureSpatialAnchorsCreating and configuring the scene and give the scene a suitable name, for example, AzureSpatialAnchors

然後遵循變更空間感知顯示選項的指示,以執行下列動作:Then follow the Changing the Spatial Awareness Display Option instructions to:

  1. MRTK 設定檔 變更為 DefaultHoloLens2ConfigurationProfileChange the MRTK configuration profile for to the DefaultHoloLens2ConfigurationProfile
  2. 空間感知網格顯示選項 變更為 遮蔽Change the spatial awareness mesh display options to Occlusion.

安裝內建的 Unity 套件Installing inbuilt Unity packages

在 Unity 功能表中,選取 [視窗] > [套件管理員] 以開啟 [套件管理員] 視窗,然後選取 [AR 基本概念],並按一下 [安裝] 按鈕以安裝套件:In the Unity menu, select Window > Package Manager to open the Package Manager window, then select AR Foundation and click the Install button to install the package:

已選取 AR Foundation 的 Unity 套件管理員

注意

您正在安裝 AR Foundation 套件,因為 Azure Spatial Anchors SDK 需要此套件,您將在下一節中匯入。You are installing the AR Foundation package because the Azure Spatial Anchors SDK requires it, which you will import in the next section.

匯入教學課程資產Importing the tutorial assets

下載並 依列出順序匯入 下列 Unity 自訂套件:Download and import the following Unity custom packages in the order they are listed:

匯入教學課程資產之後,您的專案視窗看起來應該會像這樣:After you have imported the tutorial assets your Project window should look similar to this:

匯入教學課程資產後的 Unity 階層、場景和專案視窗

注意

如果您看到任何有關於 'WorldAnchor.SetNativeSpatialAnchorPtr(IntPtr)' 已過時的 CS0618 警告,您可以忽略這些警告。If you see any CS0618 warnings regarding 'WorldAnchor.SetNativeSpatialAnchorPtr(IntPtr)' is obsolete, you can ignore these warnings.

提示

如需有關如何匯入 Unity 自訂套件的提示,您可以參閱匯入混合實境工具組 的指示。For a reminder on how to import a Unity custom package, you can refer to the Importing the Mixed Reality Toolkit instructions.

準備場景Preparing the scene

在本節中,您將藉由新增一些教學課程 Prefab 來準備場景。In this section, you will prepare the scene by adding some of the tutorial prefabs.

在 [專案] 視窗中,瀏覽至 [資產] > [MRTK.Tutorials.AzureSpatialAnchors] > [Prefabs] 資料夾,然後按一下下列 Prefab 並將其拖曳至 [階層] 視窗中,將其新增到您的場景中:In the Project window, navigate to the Assets > MRTK.Tutorials.AzureSpatialAnchors > Prefabs folder, then click-and-drag the following prefabs into the Hierarchy window to add them to your scene:

  • ButtonParent PrefabButtonParent prefabs
  • DebugWindow PrefabDebugWindow prefabs
  • Instructions PrefabInstructions prefabs
  • ParentAnchor PrefabParentAnchor prefabs

已選取新增 Prefabs 的 Unity

提示

如果您發現場景中的大圖示 (例如大型邊框的 'T' 圖示) 會造成干擾,您可以藉由將 Gizmo 切換到關閉位置來將其隱藏,如上圖所示。If you find the large icons in your scene, for example, the large framed 'T' icons distracting, you can hide these by toggling the Gizmos to the off position, as shown in the image above.

設定按鈕以操作場景Configuring the buttons to operate the scene

在本節中,您將在場景中新增指令碼來建立一系列的按鈕事件,以示範本機錨點和 Azure Spatial Anchors 在應用程式中的行為基礎。In this section, you will add scripts to the scene to create a series of button events that demonstrate the fundamentals of how both local anchors and Azure Spatial Anchors behave in an app.

在 [階層] 視窗中,展開 ButtonParent 物件,並選取名為 StartAzureSession 的第一個子物件,然後在 [偵測器] 視窗中,設定 Button Config Helper (指令碼) 元件的 On Click () 事件,如下所示:In the Hierarchy window, expand the ButtonParent object and select the first child object named StartAzureSession, in the Inspector window, configure the Button Config Helper (Script) component's On Click () event as follows:

  • ParentAnchor 物件指派給 [無 (物件)] 欄位Assign the ParentAnchor object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [AnchorModuleScript] > [StartAzureSession ()],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select AnchorModuleScript > StartAzureSession () to set this function as the action to be executed when the event is triggered

已設定 StartAzureSession 按鈕 OnClick 事件的 Unity

在 [階層] 視窗中,選取名為 StopAzureSession 的下一個按鈕,然後在 [偵測器] 視窗中,設定 Button Config Helper (指令碼) 元件的 On Click () 事件,如下所示:In the Hierarchy window, select the next button named StopAzureSession, then in the Inspector window, configure the Button Config Helper (Script) component's On Click () event as follows:

  • ParentAnchor 物件指派給 [無 (物件)] 欄位Assign the ParentAnchor object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [AnchorModuleScript] > [StopAzureSession ()],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select AnchorModuleScript > StopAzureSession () to set this function as the action to be executed when the event is triggered

已設定 StopAzureSession 按鈕 OnClick 事件的 Unity

在 [階層] 視窗中,選取名為 CreateAzureAnchor 的下一個按鈕,然後在 [偵測器] 視窗中,設定 Button Config Helper (指令碼) 元件的 On Click () 事件,如下所示:In the Hierarchy window, select the next button named CreateAzureAnchor, then in the Inspector window, configure the Button Config Helper (Script) component's On Click () event as follows:

  • ParentAnchor 物件指派給 [無 (物件)] 欄位Assign the ParentAnchor object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [AnchorModuleScript] > [CreateAzureAnchor ()],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select AnchorModuleScript > CreateAzureAnchor () to set this function as the action to be executed when the event is triggered
  • ParentAnchor 物件指派給空的 [無 (遊戲物件)] 欄位,使其成為 CreateAzureAnchor () 函式的引數Assign the ParentAnchor object to the empty None (Game Object) field to make it the argument for the CreateAzureAnchor () function

已設定 CreateAzureAnchor 按鈕 OnClick 事件的 Unity

在 [階層] 視窗中,選取名為 RemoveLocalAnchor 的下一個按鈕,然後在 [偵測器] 視窗中,設定 Button Config Helper (指令碼) 元件的 On Click () 事件,如下所示:In the Hierarchy window, select the next button named RemoveLocalAnchor,then in the Inspector window, configure the Button Config Helper (Script) component's On Click () event as follows:

  • ParentAnchor 物件指派給 [無 (物件)] 欄位Assign the ParentAnchor object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [AnchorModuleScript] > [RemoveLocalAnchor ()],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select AnchorModuleScript > RemoveLocalAnchor () to set this function as the action to be executed when the event is triggered
  • ParentAnchor 物件指派給空的 [無 (遊戲物件)] 欄位,使其成為 RemoveLocalAnchor () 函式的引數Assign the ParentAnchor object to the empty None (Game Object) field to make it the argument for the RemoveLocalAnchor () function

已設定 RemoveLocalAnchor 按鈕 OnClick 事件的 Unity

在 [階層] 視窗中,選取名為 FindAzureAnchor 的下一個按鈕,然後在 [偵測器] 視窗中,設定 Button Config Helper (指令碼) 元件的 On Click () 事件,如下所示:In the Hierarchy window, select the next button named FindAzureAnchor,then in the Inspector window, configure the Button Config Helper (Script) component's On Click () event as follows:

  • ParentAnchor 物件指派給 [無 (物件)] 欄位Assign the ParentAnchor object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [AnchorModuleScript] > [FindAzureAnchor ()],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select AnchorModuleScript > FindAzureAnchor () to set this function as the action to be executed when the event is triggered

已設定 FindAzureAnchor 按鈕 OnClick 事件的 Unity

在 [階層] 視窗中,選取名為 DeleteAzureAnchor 的下一個按鈕,然後在 [偵測器] 視窗中,設定 Button Config Helper (指令碼) 元件的 On Click () 事件,如下所示:In the Hierarchy window, select the next button named DeleteAzureAnchor, then in the Inspector window, configure the Button Config Helper (Script) component's On Click () event as follows:

  • DeleteAzureAnchor 物件指派給 [無 (物件)] 欄位Assign the DeleteAzureAnchor object to the None (Object) field
  • 從 [沒有函式] 下拉式清單中,選取 [AnchorModuleScript] > [DeleteAzureAnchor ()],以將此函式設定為觸發事件時所要執行的動作From the No Function dropdown, select AnchorModuleScript > DeleteAzureAnchor () to set this function as the action to be executed when the event is triggered

已設定 DeleteAzureAnchor 按鈕 OnClick 事件的 Unity

將場景連線至 Azure 資源Connecting the scene to the Azure resource

在 [階層] 視窗中,選取 ParentAnchor 物件,然後在 [偵測器] 視窗中尋找 Spatial Anchor Manager (指令碼) 元件。In the Hierarchy window, select the ParentAnchor object, then in the Inspector window, locate the Spatial Anchor Manager (Script) component. 使用 Azure Spatial Anchors 帳戶中的認證來設定 [認證] 區段,該帳戶會在本教學課程系列的必要條件中建立:Configure the Credentials section with the credentials from the Azure Spatial Anchors account created as part of the Prerequisites for this tutorial series:

  • 在 [Spatial Anchors Account 識別碼] 欄位中,貼上 Azure Spatial Anchors 帳戶中的 帳戶識別碼In the Spatial Anchors Account ID field, paste the Account ID from your Azure Spatial Anchors account
  • 在 [Spatial Anchors 帳戶金鑰] 欄位中,貼上 Azure Spatial Anchors 帳戶中的主要或次要 存取金鑰In the Spatial Anchors Account Key field, paste the primary or secondary Access Key from your Azure Spatial Anchors account

已設定 Spatial Anchor Manager 的 Unity

嘗試 Azure Spatial Anchors 的基本行為Trying the basic behaviors of Azure Spatial Anchors

Azure Spatial Anchors 無法在 Unity 中執行,因此若要測試 Azure Spatial Anchors 功能,您必須在裝置中建置專案並部署應用程式。Azure Spatial Anchors can not run in Unity, so to test the Azure Spatial Anchors functionality, you need to build the project and deploy the app to your device.

提示

如需有關如何建立 Unity 專案並將其部署至 HoloLens 2 的提醒,您可以參閱 [對您的 HoloLens 2 建置應用程式] (mr-learning-base-02.md#building-your-application-to-your-hololens-2) 的指示。For a reminder on how to build and deploy your Unity project to HoloLens 2, you can refer to the [Building your application to your HoloLens 2]((mr-learning-base-02.md#building-your-application-to-your-hololens-2) instructions.

當應用程式在您的裝置上執行時,請遵循 Azure Spatial Anchor 教學課程指示面板上顯示的螢幕指示:When the app runs on your device, follow the on-screen instructions displayed on the Azure Spatial Anchor Tutorial Instructions panel:

  1. 將立方體移至不同的位置Move the cube to a different location
  2. 啟動 Azure 工作階段Start Azure Session
  3. 建立 Azure 錨點 (在立方體的位置上建立錨點)。Create Azure Anchor (creates an anchor at the location of the cube).
  4. 停止 Azure 工作階段Stop Azure Session
  5. 移除區域錨點 (允許使用者移動立方體)Remove Local Anchor (allows the user to move the cube)
  6. 將立方體移至其他地方Move the cube somewhere else
  7. 啟動 Azure 工作階段Start Azure Session
  8. 尋找 Azure 錨點 (將立方體置於步驟 3 的位置)Find Azure Anchor (positions the cube at the location from step 3)
  9. 刪除 Azure 錨點Delete Azure Anchor
  10. 停止 Azure 工作階段Stop Azure session

已選取指示物件的 Unity

警告

Azure Spatial Anchors 會使用網際網路來儲存和載入錨點資料,因此請確定您的裝置已連線到網際網路。Azure Spatial Anchors uses the internet to save and load the anchor data, so make sure your device is connected to the internet.

錨點體驗Anchoring an experience

在前面幾節中,您已了解 Azure Spatial Anchors 的基本概念。In the previous sections, you learned the fundamentals of Azure Spatial Anchors. 我們使用了立方體和連結的錨點來呈現及視覺化父遊戲物件。We used a cube to represent and visualize the parent game object with the attached anchor. 在本節中,您將了解如何藉由將整個體驗放在 ParentAnchor 物件的子系,來為其建立錨點。In this section, you will learn how to anchor an entire experience by placing it as a child of the ParentAnchor object.

在 [階層] 視窗中,選取 ParentAnchor 物件,然後在 [偵測器] 視窗中設定 變形 元件,如下所示:In the Hierarchy window, select the ParentAnchor object, then in the Inspector window, configure the Transform components as follows:

  • 將 [X 軸縮放] 變更為 1.1Change Scale X to 1.1
  • 將 [Z 軸縮放] 變更為 1.1Change Scale Z to 1.1

已選取、置放及縮放 ParentAnchor 物件的 Unity

在 [專案] 視窗中,瀏覽至 [資產] > [MRTK.Tutorials.GettingStarted] > [Prefabs] > [Rover] 資料夾,然後按一下 RoverExplorer_Complete Prefab,並將其拖曳至 [階層] 視窗中來新增到您的場景中:In the Project window, navigate to the Assets > MRTK.Tutorials.GettingStarted > Prefabs > Rover folder, then click-and-drag the RoverExplorer_Complete prefab into the Hierarchy window to add it to the scene:

已選取新增 RoverExplorer_Complete Prefab 的 Unity

在 [階層] 視窗中,繼續選取新增的 RoverModule_Complete 物件,並將其拖曳至 ParentAnchor 物件,使其成為 ParentAnchor 物件的子系:With the newly added RoverModule_Complete object still selected in the Hierarchy window, drag it onto the ParentAnchor object to make it a child of the ParentAnchor object:

RoverExplorer_Complete 物件設定為 ParentAnchor 子系的 Unity

如果您現在重建專案,並將應用程式部署到您的裝置,您現在就可以藉由移動已調整大小的立方體來重新放置整個 Rover Explorer 體驗。If you now rebuild the project and deploy the app to your device, you can now reposition the entire Rover Explorer experience by moving the resized cube.

提示

有各種不同的使用者體驗流程可用於重新置放體驗,包括使用重新置放物件 (例如本教學課程中使用的立方體)、使用按鈕來切換環繞體驗的周框方塊、使用位置和旋轉 gizmo 等等。A variety of user experience flows for repositioning experiences, including the use of a repositioning object (such as the cube used in this tutorial), the use of a button to toggle a bounding box that surrounds the experience, the use of position and rotation gizmos, and more.

恭喜!Congratulations

在本教學課程中,您已了解 Azure Spatial Anchors 的基本概念。In this tutorial, you learned the fundamentals of Azure Spatial Anchors. 本教學課程提供了您幾個按鈕,讓您探索啟動和停止 Azure Spatial Anchors 工作階段所需的各種步驟。This tutorial provided you with several buttons to let you explore the various steps required to start and stop an Azure Spatial Anchors session. 此外,也可以在單一裝置上建立、上傳和下載 Azure Spatial Anchors。Also, to create, upload, and download Azure Spatial Anchors on a single device.

在下一個教學課程中,您將了解如何將 Azure 錨點識別碼儲存至 HoloLens 2 以便擷取 (即使在重新啟動應用程式之後),以及如何在多個裝置之間傳輸錨點識別碼以達到空間對齊。In the next tutorial, you will learn how to save Azure anchor IDs to your HoloLens 2 for retrieval, even after the app is restarted, and how to transfer anchor IDs between multiple devices to achieve spatial alignment.