匯出和建置 Unity Visual Studio 解決方案Exporting and building a Unity Visual Studio solution

如果您的應用程式不需要系統鍵盤,我們的建議是使用 D3D ,讓您的應用程式使用較少的記憶體,並縮短啟動時間。If your app doesn't need the system keyboard, our recommendation is to use D3D so that your app uses slightly less memory and a faster launch time. 不過,如果您是透過 TouchScreenKeyboard API 使用系統鍵盤,您需要將專案匯出為 XAMLHowever, if you're using the system keyboard through the TouchScreenKeyboard API, you need to export the project as XAML.

如何從 Unity 匯出How to export from Unity

Unity 組建設定Unity build settings
Unity 編輯器中的組建設定Build settings in Unity editor

  1. 當您準備好從 Unity 匯出專案時,請 開啟 [檔案] 功能表,然後選取 [組建設定 ... ]。When you're ready to export your project from Unity, open the File menu and select Build Settings...
  2. 選取 [ 新增開啟的場景 ],將您的場景新增至組建。Select Add Open Scenes to add your scene to the build.
  3. 在 [ 組建設定 ] 對話方塊中,選擇下列選項以匯出 HoloLens:In the Build Settings dialog, choose the following options to export for HoloLens:
    • 平臺: 通用 Windows 平臺 並務必選取 [ 切換平臺 ],讓您的選擇生效。Platform: Universal Windows Platform and be sure to select Switch Platform for your selection to take effect.
    • SDK: 通用 10SDK: Universal 10.
    • UWP 組建類型: D3DUWP Build Type: D3D.
  4. 選用Unity c # 專案: 已核取。Optional: Unity C# Projects: Checked.

注意

勾選此方塊可讓您:Checking this box allows you to:

  • 在 Visual Studio 遠端偵錯程式中,為您的應用程式進行偵錯工具。Debug your app in the Visual Studio remote debugger.
  • 使用 IntelliSense 進行 WinRT Api 時,請編輯 Unity c # 專案中的腳本。Edit scripts in the Unity C# project while using IntelliSense for WinRT APIs.
  1. 從 [組建設定 ... ] 視窗中,開啟 [播放玩家設定]。From the Build Settings... window, open Player Settings...
  2. 選取 [ 通用 Windows 平臺 ] 索引標籤的設定。Select the Settings for Universal Windows Platform tab.
  3. 展開 [XR 設定] 群組。Expand the XR Settings group.
  4. 在 [ XR 設定 ] 區段中,核取 [ 支援虛擬事實 ] 核取方塊以新增 虛擬實境裝置 清單,並確認 [Windows Mixed Reality] 列為支援的裝置。In the XR Settings section, check the Virtual Reality Supported checkbox to add a new Virtual Reality Devices list and confirm "Windows Mixed Reality" is listed as a supported device.
  5. 返回 [ 組建設定 ] 對話方塊。Return to the Build Settings dialog.
  6. 選取 [組建] 。Select Build.
  7. 在出現的 [Windows 檔案總管] 對話方塊中,建立新的資料夾來保存 Unity 的組建輸出。In the Windows Explorer dialog that appears, create a new folder to hold Unity's build output. 一般來說,我們會將資料夾命名為 "App"。Generally, we name the folder "App".
  8. 選取新建立的資料夾,然後選取 [ 選取資料夾]。Select the newly created folder and select Select Folder.
  9. Unity 完成建立之後,會開啟專案根目錄的 Windows 檔案總管視窗。Once Unity has finished building, a Windows Explorer window will open to the project root directory. 流覽至新建立的資料夾。Navigate into the newly created folder.
  10. 開啟位於此資料夾內產生的 Visual Studio 方案檔。Open the generated Visual Studio solution file located inside this folder.

從 Unity 重新匯出的時機When to re-export from Unity

從 Unity 匯出您的應用程式時,檢查 c # 專案 核取方塊會建立包含所有 Unity 腳本檔案的 Visual Studio 解決方案。Checking the C# Projects checkbox when exporting your app from Unity creates a Visual Studio solution that includes all your Unity script files. 將您的所有腳本都放在一個位置,可讓您逐一查看,而不需要從 Unity 重新匯出。Having all your scripts in one place lets you iterate without re-exporting from Unity. 但是,如果您對專案進行變更,而不只是變更腳本的內容,您必須從 Unity 重新匯出。However, if you make changes to your project that aren't just changing the contents of scripts, you'll need to re-export from Unity. 您需要從 Unity 重新匯出的一些時間範例如下:Some examples of times you need to re-export from Unity are:

  • 您可以在 [專案] 索引標籤中新增或移除資產。You add or remove assets in the Project tab.
  • 您可以在 [偵測器] 索引標籤中變更任何值。You change any value in the Inspector tab.
  • 您可以從 [階層] 索引標籤新增或移除物件。You add or remove objects from the Hierarchy tab.
  • 您變更任何 Unity 專案設定You change any Unity project settings

建立及部署 Unity Visual Studio 解決方案Building and deploying a Unity Visual Studio solution

建立和部署應用程式的其餘部分會在 Visual Studio中進行。The remainder of building and deploying apps happens in Visual Studio. 您將需要指定 Unity 組建設定。You will need to specify a Unity build configuration. Unity 的命名慣例可能與您在 Visual Studio 中使用的不同:Unity's naming conventions may differ from what you're used to in Visual Studio:

組態Configuration 說明Explanation
偵錯Debug 關閉所有優化,並啟用分析工具。All optimizations off and the profiler is enabled. 用來對腳本進行調試。Used to debug scripts.
MasterMaster 所有優化都會開啟,並停用分析工具。All optimizations are turned on and the profiler is disabled. 用來將應用程式提交到存放區。Used to submit apps to the Store.
版本Release 所有優化都已開啟,並且已啟用分析工具。All optimizations are turned on and the profiler is enabled. 用來評估應用程式效能。Used to evaluate app performance.

請注意,上述清單是常見觸發程式的子集,這些觸發程式會導致 Visual Studio 專案需要產生。Note, the above list is a subset of the common triggers that will cause the Visual Studio project to need to be generated. 一般而言,從 Visual Studio 中編輯 .cs 檔案不需要從 Unity 內重新產生專案。In general, editing .cs files from within Visual Studio won't require the project to be regenerated from within Unity.

疑難排解Troubleshooting

如果您在 Visual Studio 專案中發現無法辨識 .cs 檔案的編輯,請確定當您從 Unity 的 [組建] 功能表產生 VS 專案時,會檢查 Unity c # 專案If you find that edits to your .cs files aren't being recognized in your Visual Studio project, ensure that Unity C# Projects is checked when you generate the VS project from Unity's Build menu.