使用 Visual Studio 來部署和偵錯Using Visual Studio to deploy and debug

無論您是使用 DirectX 或 Unity 來開發混合實境應用程式,Visual Studio 都是進行偵錯和部署的必備工具。Whether you're using DirectX or Unity to develop your mixed reality app, Visual Studio is your go-to tool for debugging and deployment. 在本節中,您將了解如何:In this section, you will learn how to:

  • 透過 Visual Studio 將應用程式部署到 HoloLens 或 Windows Mixed Reality 沉浸式頭戴裝置。Deploy applications to your HoloLens or Windows Mixed Reality immersive headset through Visual Studio.
  • 使用內建至 Visual Studio 的 HoloLens 模擬器。Use the HoloLens emulator built in to Visual Studio.
  • 針對混合實境應用程式進行偵錯。Debug mixed reality apps.

必要條件Prerequisites

  1. 如需安裝指示,請參閱安裝工具See Install the Tools for installation instructions.
  2. 在 Visual Studio 中建立新的通用 Windows app 專案。Create a new Universal Windows app project in Visual Studio. 對於 HoloLens (第 1 代),使用 Visual Studio 2017 或更新版本。For HoloLens (1st gen), use Visual Studio 2017 or newer. 對於 HoloLens 2,使用 Visual Studio 2019 16.2 或更新版本。For HoloLens 2, use Visual Studio 2019 16.2 or newer. 支援 C# 和 C++。C# and C++ are supported. (或者遵循指示,在 Unity 中建立應用程式。)(Or follow the instructions to create an app in Unity.)

啟用開發人員模式Enabling Developer Mode

從在您的裝置上啟用 開發人員模式 開始,讓 Visual Studio 可以與它連線。Start by enabling Developer Mode on your device, so Visual Studio can connect to it.

HoloLensHoloLens

  1. 開啟您的 HoloLens 並將裝置戴上。Turn on your HoloLens and put on the device.
  2. 使用開始手勢來啟動主功能表。Use the start gesture to launch the main menu.
  3. 選取 [設定] 磚,在您的環境中啟動應用程式。Select the Settings tile to launch the app in your environment.
  4. 選取 [更新] 功能表項目。Select the Update menu item.
  5. 選取 [適用於開發人員] 功能表項目。Select the For developers menu item.
  6. 啟用 [ 使用開發人員功能 ],將應用程式從 Visual Studio 部署到 HoloLens。Enable Use developer features to deploy apps from Visual Studio to your HoloLens. 如果您的裝置正在執行 Windows 全像21H1 版或更新版本,也可以啟用 裝置探索If your device is running Windows Holographic version 21H1 or newer, also enable Device discovery.
  7. 選擇性:向下捲動而且啟用 [裝置入口網站],可讓您從網頁瀏覽器連線到您 HoloLens 上的 Windows 裝置入口網站Optional: Scroll down and also enable Device Portal, which lets you connect to the Windows Device Portal on your HoloLens from a web browser.

Windows PCWindows PC

如果您使用的是連線到電腦的 Windows Mixed Reality 頭戴式裝置,就必須在電腦上啟用 開發人員模式If you're working with a Windows Mixed Reality headset connected to your PC, you must enable Developer Mode on the PC.

  1. 移至 [設定] Go to Settings
  2. 選取 [更新與安全性] Select Update and Security
  3. 選取 [適用於開發人員] Select For developers
  4. 啟用 [開發人員模式],閱讀您所選設定的免責聲明,然後選取 [是] 來接受變更。Enable Developer Mode, read the disclaimer for the setting you chose, then select Yes to accept the change.

透過 Wi-Fi 部署 HoloLens 應用程式Deploying a HoloLens app over Wi-Fi

使用下列屬性來設定您的 Visual Studio 專案:Configure your Visual Studio project with the following properties:

  1. 選取您的應用程式編譯選項Select your apps compilation options
    • 針對 Unity 專案,選擇 [發行] 或 [主要]For Unity projects, choose either Release or Master
    • 針對所有其他專案,選擇 [發行]For all other projects, choose Release

注意

您可以在 匯出和建立 Visual Studio 方案中,找到每個編譯選項的完整定義。You can find complete definitions for each compilation option in exporting and building Visual Studio solutions.

  1. 根據您的裝置選取組建設定Select your build configuration based on your device

選擇 ARMARM64 configuration 選項Choose the ARM or ARM64 configuration option

Visual Studio 中的 ARM64 組建設定

  1. 在 [部署目標] 下拉式功能表中,選取 [遠端電腦] Select Remote Machine in the deployment target drop-down menu

Visual Studio 中的遠端電腦部署目標

接下來,您需要設定遠端連線。Next, you need to set your remote connection. 針對 C++ 和 JavaScript 專案,請移至 [專案 > 屬性 > 設定屬性 > 偵錯] 。For C++ and JavaScript projects, go to Project > Properties > Configuration Properties > Debugging. 如果您是在 c # 專案中工作,對話方塊應該會自動出現。If you're working in a C# project, a dialog should automatically appear.

注意

如果 [遠端連線] 對話方塊未出現在 c # 專案中,您可以從 [ 屬性] > Debug] 手動開啟。If the remote connection dialog doesn't appear in your C# project, you can open it manually from Properties > Debug.

  1. 在 [位址] 或 [電腦名稱] 欄位中,輸入您裝置的 IP 位址。Enter the IP address of your device in the Address or Machine Name field.

    • 您可以在 HoloLens 下的 [設定] > Network & Internet > Advanced Options 中找到 IP 位址You can find the IP address on your HoloLens under Settings > Network & Internet > Advanced Options
    • 我們一律建議以手動方式輸入您的 IP 位址,而不是根據自動偵測到的功能。We always recommend manually entering your IP address rather than depending on the Auto Detected feature
  2. 驗證模式 設定為 通用 (未加密的通訊協定)Set the Authentication Mode to Universal (Unencrypted protocol)

Visual Studio 中的遠端連線對話方塊

  1. 根據您的需求來建立、部署及偵測您的應用程式Build, deploy, and debug your app based on your needs
    • 選取 [偵錯 > 開始偵錯] 來部署您的應用程式並開始偵錯Select Debug > Start debugging to deploy your app and start debugging
    • 選取 組建 > 部署 以在不進行調試的情況下進行組建和部署Select Build > Deploy to build and deploy without debugging

在 Visual Studio 中開始但不進行偵錯

  1. 當您第一次從電腦將應用程式部署到 HoloLens 時,系統會提示您輸入 PIN 碼。The first time you deploy an app to your HoloLens from your PC, you'll be prompted for a PIN. 遵循以下的 配對您的裝置 指示。Follow the Pairing your device instructions below.

透過 USB 部署 HoloLens 應用程式Deploying a HoloLens app over USB


  1. 選取您的應用程式編譯選項Select your apps compilation options
    • 針對 Unity 專案,選擇 [發行] 或 [主要]For Unity projects, choose either Release or Master
    • 針對所有其他專案,選擇 [發行]For all other projects, choose Release

注意

您可以在 匯出和建立 Visual Studio 方案中,找到每個編譯選項的完整定義。You can find complete definitions for each compilation option in exporting and building Visual Studio solutions.

  1. 根據您的裝置選取組建設定Select your build configuration based on your device

選擇 ARMARM64 configuration 選項Choose the ARM or ARM64 configuration option

Visual Studio 中的 ARM64 組建設定

  1. 在 [部署目標] 下拉式功能表中,選取 [裝置] Select Device in the deployment target drop-down menu

Visual Studio 中的裝置部署

  1. 根據您的需求來建立、部署及偵測您的應用程式Build, deploy, and debug your app based on your needs
    • 選取 [偵錯 > 開始偵錯] 來部署您的應用程式並開始偵錯Select Debug > Start debugging to deploy your app and start debugging
    • 選取 組建 > 部署 以在不進行調試的情況下進行組建和部署Select Build > Deploy to build and deploy without debugging

在 Visual Studio 中開始但不進行偵錯

  1. 當您第一次從電腦將應用程式部署到 HoloLens 時,系統會提示您輸入 PIN 碼。The first time you deploy an app to your HoloLens from your PC, you'll be prompted for a PIN. 遵循以下的 配對您的裝置 指示。Follow the Pairing your device instructions below.

注意

如果您在透過 USB 部署應用程式時看到相當長的延遲時間,建議您在上一節中使用 遠端電腦的指示If you're seeing considerable lag time with your apps deployment over USB, we recommend using the remote machine instructions in the previous section.

將應用程式部署到 HoloLens 模擬器Deploying an app to the HoloLens Emulator

  1. 請確定您已 安裝 HoloLens 2 或 HoloLens (第1代) 模擬器Make sure you've installed either the HoloLens 2 or HoloLens (1st gen) Emulator
  2. 根據您的裝置選取組建設定和模擬器Select your build configuration and emulator based on your device

選擇 ARMARM64 configuration 選項Choose the ARM or ARM64 configuration option

Visual Studio 中的 ARM64 組建設定

  1. 根據您的需求來建立、部署及偵測您的應用程式Build, deploy, and debug your app based on your needs
    • 選取 [偵錯 > 開始偵錯] 來部署您的應用程式並開始偵錯Select Debug > Start debugging to deploy your app and start debugging
    • 選取 組建 > 部署 至不 debuggingg 的組建和部署Select Build > Deploy to build and deploy without debuggingg

在 Visual Studio 中開始但不進行偵錯

將 VR 應用程式部署到您的本機電腦Deploying a VR app to your Local PC

若要使用可連線到您的電腦或混合實境模擬器的 Windows Mixed Reality 沉浸式頭戴裝置:To use a Windows Mixed Reality immersive headset that connects to your PC or the Mixed Reality simulator:

  1. 為您的應用程式選取 x86x64 組建設定Select an x86 or x64 build configuration for your app
  2. 在 [部署目標] 下拉式功能表中,選取 [本機電腦] Select Local Machine in the deployment target drop-down menu
  3. 根據您的需求來建立、部署及偵測您的應用程式Build, deploy, and debug your app based on your needs
    • 選取 [偵錯 > 開始偵錯] 來部署您的應用程式並開始偵錯Select Debug > Start debugging to deploy your app and start debugging
    • 選取 組建 > 部署 以在不進行調試的情況下進行組建和部署Select Build > Deploy to build and deploy without debugging

配對您的裝置Pairing your device

當您第一次從 Visual Studio 將應用程式部署到 HoloLens 時,系統會提示您輸入 PIN 碼。The first time you deploy an app from Visual Studio to your HoloLens, you'll be prompted for a PIN. 在 HoloLens 上,移至 [更新 > 適用於開發人員],並點選 [配對],藉由啟動 [設定] 應用程式來產生 PIN。On the HoloLens, generate a PIN by launching the Settings app, go to Update > For Developers, and tap on Pair. 當您的 HoloLens 上顯示 PIN 時,請將其輸入 Visual Studio 中。When the PIN is displayed on your HoloLens, type it into Visual Studio. 配對完成之後,點選 HoloLens 上的 [完成] 以關閉對話方塊。After pairing is complete, tap Done on your HoloLens to dismiss the dialog. 這部電腦現在已與 HoloLens 配對,您可以自動部署應用程式。This PC is now paired with the HoloLens and you can deploy apps automatically. 針對用來將應用程式部署至 HoloLens 的每部電腦重複這些步驟。Repeat these steps for every PC that's used to deploy apps to your HoloLens.

若要解除配對您的 HoloLens 與所有配對的電腦:To unpair your HoloLens from all paired computers:

  • 啟動 [設定] 應用程式,移至 [更新] > [適用於開發人員],然後點選 [清除]。Launch the Settings app, go to Update > For Developers, and tap on Clear.

HoloLens (第 1 代) 的圖形偵錯工具Graphics Debugger for HoloLens (1st gen)

撰寫和最佳化全像攝影應用程式時,Visual Studio 圖形診斷工具很有幫助。The Visual Studio Graphics Diagnostics tools are helpful when writing and optimizing a Holographic app. 如需完整詳細資料,請參閱 MSDN 上的 Visual Studio 圖形診斷See Visual Studio Graphics Diagnostics on MSDN for full details.

啟動圖形偵錯工具To Start the Graphics Debugger

  1. 遵循上述指示,以裝置或模擬器為目標Follow the instructions above to target a device or emulator
  2. 移至 [偵錯 > 圖形 > 開始診斷] Go to Debug > Graphics > Start Diagnostics
  3. 第一次使用 HoloLens 開始診斷時,您可能會遇到「拒絕存取」錯誤。The first time you start diagnostics with a HoloLens, you may get an "access denied" error. 將 HoloLens 重新開機,讓更新的權限生效,然後再試一次。Reboot your HoloLens to let the updated permissions take effect and try again.

程式碼剖析Profiling

Visual Studio 程式碼剖析工具可讓您分析應用程式的效能和資源使用。The Visual Studio profiling tools allow you to analyze your app's performance and resource use. 包括將 CPU、記憶體、圖形和網路使用最佳化的工具。This includes tools to optimize CPU, memory, graphics, and network use. 如需完整詳細資料,請參閱 MSDN 上的執行診斷工具而不進行偵錯See Run diagnostic tools without debugging on MSDN for full details.

使用 HoloLens 啟動程式碼剖析工具To Start the Profiling Tools with HoloLens

  1. 遵循上述指示,以裝置或模擬器為目標Follow the instructions above to target a device or emulator
  2. 移至 [偵錯 > 啟動診斷工具但不偵錯...] 。Go to Debug > Start Diagnostic Tools Without Debugging...
  3. 選取您想要使用的工具Select the tools you want to use
  4. 選取 [啟動]Select Start
  5. 第一次使用 HoloLens 開始診斷但不進行偵錯時,您可能會遇到「拒絕存取」錯誤。The first time you start diagnostics without debugging with a HoloLens, you may get an "access denied" error. 將 HoloLens 重新開機,讓更新的權限生效,然後再試一次。Reboot your HoloLens to let the updated permissions take effect and try again.

針對已安裝或執行中的應用程式進行偵錯Debugging an installed or running app

您可以使用 Visual Studio 針對已安裝但未從 Visual Studio 專案部署的通用 Windows 應用程式進行偵錯。You can use Visual Studio to debug an installed Universal Windows app without deploying from a Visual Studio project. 如果您想要對已安裝的應用程式套件進行偵錯,或對已經在執行中的應用程式進行偵錯,這會很有用。This is useful if you want to debug an installed app package or debug an app that's already running.

  1. 移至 [偵錯 -> 其他偵錯目標 -> 針對已安裝的應用程式套件進行偵錯] Go to Debug -> Other Debug Targets -> Debug Installed App Package
  2. 針對 HoloLens 選取 [遠端電腦] 目標,或針對沉浸式頭戴裝置選取 [本機電腦] 。Select the Remote Machine target for HoloLens or Local Machine for immersive headsets.
  3. 輸入您裝置的 IP 位址Enter your device’s IP address
  4. 選擇 [通用] 驗證模式Choose the Universal Authentication Mode
  5. 此視窗會顯示執行中和非使用中的應用程式。The window shows both running and inactive apps. 挑選您想要進行偵錯的應用程式。Pick the one what you’d like to debug.
  6. 選擇要進行偵錯的程式碼類型 (受控、原生、混合)Choose the type of code to debug (Managed, Native, Mixed)
  7. 選取 [連結] 或 [啟動]Select Attach or Start

下一個開發檢查點Next Development Checkpoint

依循我們配置的 Unity 開發檢查點旅程,此時您會進入部署階段。If you're following the Unity development checkpoint journey we've laid out, you're in the midst of the deployment stage. 接下來,您可以繼續進行下一個主題:From here, you can continue to the next topic:

或者,直接跳到新增進階服務的主題:Or jump directly to adding advanced services:

您可以隨時回到 Unity 開發檢查點You can always go back to the Unity development checkpoints at any time.

另請參閱See also