1.下載 MixedRealityToolkit-Unity (混合現實工具包)
- 什么是 MixedRealityToolkit-Unity ?
- 一個腳本和組件的集合,加速針對微軟全息和Windows混合現實的應用程序的開發。
GitHub : https://github.com/Microsoft/MixedRealityToolkit-Unity
下載完成后解壓可獲得以下腳本組件.
\MixedRealityToolkit-Unity-master\Assets\
2.創建項目
- 運行Unity並創建一個項目。
- 點擊 Create project 后 , 完成項目創建。
3.設置 Camera
- 將下載好的 MixedRealityToolkit 打開,把文件夾 Assets/HoloToolkit 和 Assets/HoloToolkit-Examples 拖到 Unity 的 Assets 下,由於文件較大拖動后會出現假死情況,耐心等待即可。
- 在 Unity 界面,Hierarchy 面板中刪除默認創建的 Main Camera,將 Assets 面板中 HoloToolkit -> Input -> Prefabs -> HoloLensCamera 拖入 Hierarchy 面板的根級。
1. 2.
4.創建全息模型
- 在 Hierarchy 面板中右鍵選擇 3D Object -> Cube
- 在 Hierarchy 面板中選中這個 Cube
- Unity 右側出現 Inspector 面板找到 Transform 組件
- 設置 Position -> (X : 0 , Y : 0 , Z : 2)
- 設置 Rotation -> (X : 45 , Y : 45 , Z : 45)
- 設置 Scale -> (X : 0.25 , Y : 0.25 , Z : 0.25)
5.Build Settings
- 在 Unity 主菜單選擇 File -> Build Settings
- 點擊 Add Open Scenes 添加當前場景
- 在 Platform 里點擊 Universal Windows Platform
- 點擊 Switch Platform 切換發布平台到 Universal Windows Platform
- 設置 Target Device 為 HoloLens
- 設置 SDK 為最新版本號
- 點擊 Player Settings ,Unity 右側出現設置 Player Settings 的 Inspector 面板
- 點擊 Other Settings 選項卡
- 設置 Scripting Runtime Version 為 .NET 4.x Equivalent 或更高版本后,提示重啟Unity,點擊重啟
- 設置 Scripting Backend 為 .NET
- 點擊 XR Settings 選項卡並勾選 Virtual Reality Supported
- Virtual Reality Supported 下 Virtual Reality SDKs 為 Windows Mixed Reality 即可
- 回到 Build Settings 界面,勾選 Unity C# Projects
- 點擊 Build ,在彈出框里創建 APP 文件夾 並選擇它
- 在 MixedRealityToolkit 的當前版本有個bug,編譯時報錯
- 提示 Assets/HoloToolkit-Examples/SpatialMapping/Scripts/PlaneFindingTest.cs(114,135): error CS0619: `UnityEngine.EventType.ignore' is obsolete: `Use Ignore instead (UnityUpgradable) -> Ignore'
- 就是這個 PlaneFindingTest.cs 里這行代碼 UnityEditor.Handles.ArrowHandleCap(0, center, Quaternion.FromToRotation(Vector3.forward, normal), 0.4f, EventType.ignore); 的 EventType.ignore 枚舉已經過時棄用了
- 修改為 EventType.Ignore 即可修復
- 再重新 Build 等待進度條完成
6.編譯和發布
- 上一步的 Build 完成后會在 APP 文件下生成一個 Visual Studio 的解決方案
- 打開 APP\Hololens_HelloWorld.sln 的解決方案
- 在 Hololens_HelloWorld 項目上右鍵點擊 生成
- 生成完成后,修改 解決方案平台 為 x86 ,運行環境選擇 HoleLens Emulator
- 點擊 HoleLens Emulator 將項目運行起來
- HoleLens Emulator 就會啟動並自動將項目發布到 HoleLens Emulator 中
- 最終效果