The road to HoloLens2 - Unity2019.4+MRTK2.6 configuration

Copyright statement: Davidwang's original article is strictly prohibited from being used in any commercial way and may be reproduced only with authorization.

  This section mainly describes the project creation and configuration process using Unity2019.4LTS + MRTK2.6. The next section describes the project configuration process using Unity2020.3LTS + MRTK2.7. Unity2021.3LTS has not yet been released. MRTK will only adapt to the TLS version and will be improved according to the situation.

  The HoloLens2 device is a portable and wearable mobile device. Its application development and deployment process is somewhat different from traditional applications. Below we will experience the entire process of developing MR from importing MRTK, configuring engineering projects, and exporting WMR (Windows Mixed Reality) applications. application process.

(1). Create project

  Through Unity Hub, create a new project, select the 3D template, fill in the project name, and select the project storage path. It is recommended to use English for the project name and project storage path to prevent character recognition problems, as shown in Figure 1.
Insert image description here

Figure 1 Newly created Unity project

  After the project is created, in the Unity menu, select File > Build Settings (or use the shortcut key Ctrl+Shift+B) to open the build settings window, select "Universal Windows Platform", and then click the Switch Platform button to switch the working platform to UWP platform (Universal Windows Platform, Universal Windows Platform), as shown in Figure 2.
Insert image description here

Figure 2 Switch to UWP working platform

  After switching the working platform to the UWP platform, you can import the MRTK toolkit. MRTK supports importing into the Unity project in two ways. In actual use, any of these methods can be used, which are introduced below.

(2) Directly import the MRTK toolkit

  In the Unity menu, select Assets > Import Package > Custom Package… to open the resource import window, select the downloaded Microsoft.MixedReality.Toolkit.Unity.Foundation.2.6.1.unitypackage package, and import it into the project. After the toolkit is imported, the MRTK project settings window will pop up, which can be set as needed. Here we directly click the Apply button to use the default settings, as shown in Figure 3.

  The Foundation tool package is a required package for using the MRTK development kit, while the Examples, Extensions, Tools, and TestUtilities packages are optional packages and can be imported as needed (readers are recommended to import them all, so that they can view examples and use various tools provided by MRTK at any time , later parts of this book also require the use of various toolkits).
Insert image description here

Figure 3 Configuration dialog interface

  It is most intuitive and simple to import the MRTK toolkit in this way, but the problem is that it is difficult to upgrade the toolkit, and it can only be imported all at once, and specified functions cannot be imported as needed. Therefore, we can also use the package management provided by Unity. Import the MRTK tool package through Unity Package Manager (UPM for short).

(3) Use UPM to import the MRTK toolkit

  Since the MRTK toolkit is not currently an official component package provided by Unity, you first need to add the package source address to the manifest file (manifest.json) and register it in Unity. Open the Unity project file path in Windows File Manager, use Visual Studio 2019 to open the project file Packages/manifest.json file, and add the service registration configuration shown in Code Listing 1 after the first "{" in the file.
  Code Listing 1

{
    
      //manifest.json文件原首行
  "scopedRegistries": [
    {
    
    
      "name": "Microsoft Mixed Reality",
      "url": "https://pkgs.dev.azure.com/aipmr/MixedReality-Unity-Packages/_packaging/Unity-packages/npm/registry/",
      "scopes": [
        "com.microsoft.mixedreality",
        "com.microsoft.spatialaudio"
      ]
    }
  ],

  After adding the above configuration, Unity can correctly parse and locate the service used, but we did not specify the required toolkit, so we also need to add the toolkit name used to the dependencies (dependencies), such as the code list As shown in 2, we add the Foundation, Tools, and Examples tool packages to the dependencies.
  Code Listing 2

"dependencies": {
    
      //manifest.json文件原依赖项声明行
  "com.microsoft.mixedreality.toolkit.foundation": "2.6.1",
  "com.microsoft.mixedreality.toolkit.tools": "2.6.1",
  "com.microsoft.mixedreality.toolkit.examples": "2.6.1",

  After completing all configurations, save the file and return to the Unity software. Unity will automatically load the MRTK Foundation tool package. Select Window > Package Manager in the menu to open the package manager window. You can upgrade, uninstall, and manage Foundation in this manager. package (named Mixed Reality Toolkit Foundation). Unlike the Foundation package, which is automatically loaded, the Examples and Tools packages need to be loaded by the developer themselves. If you need to use a demonstration case, select "Mixed Reality Toolkit Examples" in the UPM window and select the required case list in the right panel. Case, click the subsequent Import into Project button to complete the import of the case, as shown in Figure 4.
Insert image description here

Figure 4 Using UPM to manage MRTK toolkit

  The advantage of using UPM to manage the MRTK toolkit is that it has high flexibility and the toolkit version can be upgraded at any time. However, it should be noted that if the toolkit is uninstalled during use, the corresponding package name will not appear in UPM. In this case, it needs to be re-installed. Edit the manifest.json file to make Unity re-import the package.

(4) Use MRFT to import the MRTK toolkit

  Starting from MRTK2.6, Microsoft has provided the Mixed Reality Feature Tool (MRFT) tool, which manages the MRTK toolkit in a visual way and can greatly simplify the management of the MRTK toolkit [Using MRFT requires installation. NET 5 runtime, download address: https://dotnet.microsoft.com/download/dotnet/5.0]. MRFT is an independent tool program. Its essence is to guide Unity to load the MRTK corresponding tool package by modifying the manifest.json file. It is very simple to use this tool program to import the MRTK tool package. First, specify the Unity project path to be processed, and then select the corresponding tool package. Toolkit to complete the operation, as shown in Figure 5, when returning to the Unity editor, Unity will automatically detect and load the corresponding toolkit.
Insert image description here

Figure 5 Using MRFT to manage MRTK toolkit

  The biggest benefit of using MRFT to manage MRTK toolkits is that you can intuitively see the collection of toolkits required for MR application development, and you can choose to load a specified version.

(5) Import the TextMeshPro toolkit

  TextMeshPro is a text rendering toolkit. Compared with other text rendering plug-ins, it uses a Signed Distance Field (SDF) to render text and supports UI and 3D text rendering at different resolutions and different positions. It can render clear text, and it has better performance by using different simple and efficient shaders to achieve stroke, shadow, glow and other effects. MRTK uses TextMeshPro to render text. This toolkit can be imported in advance. If it is not imported in advance, an import prompt will pop up when text rendering is used. We import the toolkit in advance. In the Unity menu, select Window>TextMeshPro>Import TMP Essential Resources, and click the Import button in the subsequently opened resource import window to complete the import, as shown in Figure 6.
Insert image description here

Figure 6 Import TextMeshPro toolkit

(6) Configure Unity project

  Now that we have imported various required toolkits into the project, in the Unity menu, select Edit > Project Settings to open the project configuration window and configure the project. Select the Player module in the opened project configuration window, fill in the Company Name and Product Name correctly in the right functional area, and keep the defaults for the rest [The project configuration window is the place to set and manage the properties and functional features of various projects, and is related to the project All functional features and release-related configurations are set here. When developing actual projects, you need to carefully fill in the properties of the project configuration window in detail so that the released application can meet expectations and optimize performance. ].
  Since MR applications are classified as XR applications in Unity, if you only perform the above configuration, the final generated and published application will be a 2D application displayed in the form of a window after running. Therefore, Unity's XR function also needs to be activated. There are two ways. Make settings.

(1). Use XR Plugin Management

Select the XR Plug-in Management module in the project configuration window, click Install XR Plugin Management in the right panel to install the XR plug-in. After the installation is complete, check the "Initialize XR on Startup" and "Windows Mixed Reality" attribute check boxes , then select the newly added "Windows Mixed Reality" sub-module under the XR Plug-in Management module on the left, check the functions as needed, and keep the default settings here for the time being, as shown in Figure 7.
Insert image description here

Figure 7 Using XR Plugin Management to set up XR functions

(2). Use Legacy XR

  Select the Player module in the project configuration window, expand the XR Settings rollout under the UWP platform, check the "Virtual Reality Supported" attribute multi-check box, click the "+" icon under Virtual Reality SDKs, and select "Windows Mixed Reality" to add the Windows Mixed Reality SDK.
Then in the panel that appears, set the Depth Format property to 16-bit Depth, check the Enable Depth Buffer Sharing check box, the Stereo Rendering Mode property to Single Pass Instance, and check the WSA Holographic Remoting Supported property as needed, as shown in Figure 8 .

Insert image description here

Figure 8 Setting up XR functionality using Legacy XR

  We recommend using the first method to set up the XR function. The second method has been removed in the Unity 2020 version. In order to maintain the integrity of the narrative, it is introduced here [Part of the content later in this book relies on the Legacy XR setting method. , there will be special tips in the corresponding content section. ].

(7) Create and set up scenes

  In the Unity menu, select Mixed Reality Toolkit > Add to Scene and Configure. MRTK will automatically add the necessary game objects to the current scene and configure the properties of the Main Camera object for MR use.
In the Hierarchy window, select the MixedRealityToolkit game object, and then in the Inspector window, select the main configuration file under the MixedRealityToolkit component as DefaultHoloLens2ConfigurationProfile, as shown in Figure 9, use the default HoloLens2 configuration file.
Insert image description here

Figure 9 Using the default HoloLens2 configuration file

  As a demonstration, we create a new cube object in the scene, adjust the side length of the cube to 0.2, and adjust the position and angle of the cube appropriately so that it can be seen when the MR application is started. Keep the rest as default.

(8) Export Unity project

  In order to better control the generation process, we first need to export the Unity project to a Visual Studio project. In the Unity menu, select File > Build Settings to open the build settings window, as shown in Figure 10. Add the scene in the build settings window, set the Target Device property to Any Device, the Architecture property to X64, and set other properties as needed. After the settings are completed, click the "Build" button to build it into an empty target folder.
Insert image description here

Figure 10 Unity project export settings

Guess you like

Origin blog.csdn.net/yolon3000/article/details/121457528