MR 基础知识 100:Unity 入门MR Basics 100: Getting started with Unity

重要

混合现实学院教程在制作时考虑到了 HoloLens(第一代)和混合现实沉浸式头戴显示设备。The Mixed Reality Academy tutorials were designed with HoloLens (1st gen) and Mixed Reality Immersive Headsets in mind. 因此,对于仍在寻求这些设备的开发指导的开发人员而言,我们觉得很有必要保留这些教程。As such, we feel it is important to leave these tutorials in place for developers who are still looking for guidance in developing for those devices. 我们 不会 在这些教程中更新 HoloLens 2 所用的最新工具集或集成相关的内容。These tutorials will not be updated with the latest toolsets or interactions being used for HoloLens 2. 我们将维护这些教程,使之持续适用于支持的设备。They will be maintained to continue working on the supported devices. 已经为 HoloLens 2 发布了一系列新教程A new series of tutorials has been posted for HoloLens 2.

本教程将指导你创建使用 Unity 构建的基本混合现实应用。This tutorial will walk you through creating a basic mixed reality app built with Unity.

设备支持Device support

课程Course HoloLensHoloLens 沉浸式头戴显示设备Immersive headsets
MR 基础知识 100:Unity 入门MR Basics 100: Getting started with Unity ✔️✔️ ✔️✔️

先决条件Prerequisites

  • 配置了正确 工具的 WINDOWS 10 电脑。A Windows 10 PC configured with the correct tools installed.

第1章-创建新项目Chapter 1 - Create a New Project

若要使用 Unity 构建应用,首先需要创建一个项目。To build an app with Unity, you first need to create a project. 此项目组织为多个文件夹,其中最重要的文件夹是 "资产" 文件夹。This project is organized into a few folders, the most important of which is your Assets folder. 此文件夹包含从数字内容创建工具(如 Maya、最大电影院4D 或 Photoshop)中导入的所有资产、你用 Visual Studio 创建的所有代码或你最喜欢的代码编辑器,以及 Unity 在编辑器中编写场景、动画和其他 Unity 资产类型时创建的任意数量的内容文件。This is the folder that holds all assets you import from digital content creation tools such as Maya, Max Cinema 4D or Photoshop, all code you create with Visual Studio or your favorite code editor, and any number of content files that Unity creates as you compose scenes, animations and other Unity asset types in the editor.

若要生成和部署 UWP 应用,Unity 可将项目导出为 Visual Studio 解决方案,该解决方案将包含所有必需的资产和代码文件。To build and deploy UWP apps, Unity can export the project as a Visual Studio solution that will contain all necessary asset and code files.

  1. 启动 UnityStart Unity
  2. 选择“新建”Select New
  3. 输入项目名称 (例如 "MixedRealityIntroduction" ) Enter a project name (e.g. "MixedRealityIntroduction")
  4. 输入用于保存项目的位置Enter a location to save your project
  5. 确保选择了 3d 切换Ensure the 3D toggle is selected
  6. 选择 "创建项目"Select Create project

恭喜,你可以立即开始进行混合现实自定义。Congrats, you are all setup to get started with your mixed reality customizations now.

第2章-设置照相机Chapter 2 - Setup the Camera

Unity 摄像机处理头跟踪和 stereoscopic 呈现。The Unity Main Camera handles head tracking and stereoscopic rendering. 需要对主摄像机进行一些更改,以将其用于混合现实。There are a few changes to make to the Main Camera to use it with mixed reality.

  1. 选择文件 > 新建场景Select File > New Scene

首先,如果将用户的起始位置想像为 (X:0, Y:0, Z: 0) ,则可以更容易地设计应用程序。First, it will be easier to lay out your app if you imagine the starting position of the user as (X: 0, Y: 0, Z: 0). 由于摄像机正在跟踪用户的头移动,因此可以通过设置主摄像机的开始位置来设置用户的起始位置。Since the Main Camera is tracking movement of the user's head, the starting position of the user can be set by setting the starting position of the Main Camera.

  1. 选择 "层次结构" 面板中的 "主相机"Select Main Camera in the Hierarchy panel
  2. 在 " 检查器 " 面板中,找到 转换 组件,并将 位置 从 (X:0, Y:1, Z:-10) 更改为 (X:0, Y:0, z: 0) In the Inspector panel, find the Transform component and change the Position from (X: 0, Y: 1, Z: -10) to (X: 0, Y: 0, Z: 0)

其次,默认相机背景需要一些想法。Second, the default Camera background needs some thought.

对于 HoloLens 应用程序,实际情况应该出现在照相机呈现的所有内容上,而不是 Skybox 的纹理。For HoloLens applications, the real world should appear behind everything the camera renders, not a Skybox texture.

  1. 在 "层次结构" 面板中,在 "层次结构" 面板中 选择 "相机 " 组件,并将 " 清除标志" 下拉列表 从 " Skybox " 更改为 "纯色"。With the Main Camera still selected in the Hierarchy panel, find the Camera component in the Inspector panel and change the Clear Flags dropdown from Skybox to Solid Color.
  2. 选择 背景 色选取器并将 RGBA 值更改为 (0,0,0,0) Select the Background color picker and change the RGBA values to (0, 0, 0, 0)

对于以沉浸式耳机为目标的混合现实应用程序,我们可以使用 Unity 提供的默认 Skybox 纹理。For mixed reality applications targeted to immersive headsets, we can use the default Skybox texture that Unity provides.

  1. 在 "层次结构" 面板中选择了 主相机 后,在 "检查器" 面板中查找 相机 组件,并将 "清除标志" 下拉列表保留为 " Skybox"。With the Main Camera still selected in the Hierarchy panel, find the Camera component in the Inspector panel and keep the Clear Flags dropdown to Skybox.

第三,让我们在 Unity 中考虑近剪裁平面,并防止用户在用户接近对象或对象时向用户眼睛呈现对象。Third, let us consider the near clip plane in Unity and prevent objects from being rendered too close to the users eyes as a user approaches an object or an object approaches a user.

对于 hololens 应用程序,near 剪辑平面可以设置为 HoloLens 建议 0.85 米。For HoloLens applications, the near clip plane can be set to the HoloLens recommended 0.85 meters.

  1. 在 "层次结构" 面板中,在 " 层次结构" 面板中 选择 "相机 " 组件, 并将 "附近的 剪辑平面" 字段从默认的0.3 更改为 " 0.85"。With the Main Camera still selected in the Hierarchy panel, find the Camera component in the Inspector panel and change the Near Clip Plane field from the default 0.3 to the HoloLens recommended 0.85.

对于以沉浸式耳机为目标的混合现实应用程序,可以使用 Unity 提供的默认设置。For mixed reality applications targeted to immersive headsets, we can use the default setting that Unity provides.

  1. 如果仍在 "层次结构" 面板中选择了 主相机,请在 "检查器" 面板中查找 相机 组件,并将 "近处剪裁飞机" 字段保留为默认值 0.3With the Main Camera still selected in the Hierarchy panel, find the Camera component in the Inspector panel and keep the Near Clip Plane field to the default 0.3.

最后,让我们来保存迄今为止的进度。Finally, let us save our progress so far. 若要保存场景更改,请选择 " 文件" > "将场景另存为",为场景 命名,然后选择 " 保存"。To save the scene changes, select File > Save Scene As, name the scene Main, and select Save.

第3章-设置项目设置Chapter 3 - Setup the Project Settings

在本章中,我们将设置一些 Unity 项目设置,这些设置可帮助我们面向 Windows 全息 SDK 进行开发。In this chapter, we will set some Unity project settings that help us target the Windows Holographic SDK for development. 我们还将设置应用程序的质量设置。We will also set some quality settings for our application. 最后,我们将确保生成目标设置为通用 Windows 平台。Finally, we will ensure our build targets are set to Universal Windows Platform.

Unity 性能和质量设置Unity performance and quality settings

HoloLens 的 Unity 质量设置Unity quality settings for HoloLens

HoloLens 的 Unity 质量设置

由于在 HoloLens 上维护高的帧率非常重要,因此我们希望优化质量设置以实现最高性能。Since maintaining high framerate on HoloLens is so important, we want the quality settings tuned for fastest performance. 有关性能的详细信息,请查看 Unity 的性能建议For more detailed performance information, Performance recommendations for Unity.

  1. 选择 "编辑 > 项目设置 > 质量"Select Edit > Project Settings > Quality
  2. 选择 通用 Windows 平台 徽标下的 下拉列表,并选择 "非常低"。Select the dropdown under the Universal Windows Platform logo and select Very Low. 当 "通用 Windows 平台" 列中的框为绿色 时,将 知道设置正确应用。You'll know the setting is applied correctly when the box in the Universal Windows Platform column and Very Low row is green.

对于目标为封闭像素的混合现实应用程序,你可以将质量设置保留为其默认值。For mixed reality applications targeted to occluded displays, you can leave the quality settings to its default values.

面向 Windows 10 SDKTarget Windows 10 SDK

面向 Windows 全息 SDKTarget Windows Holographic SDK

面向 Windows 全息 SDK

我们需要让 Unity 知道我们要导出的应用程序应创建 沉浸式视图 而不是2d 视图。We need to let Unity know that the app we are trying to export should create an immersive view instead of a 2D view. 为此,我们将在针对 Windows 10 SDK 的 Unity 上启用虚拟现实支持。We do this by enabling Virtual Reality support on Unity targeting the Windows 10 SDK.

  1. 请参阅 " 编辑 > 项目设置" > Player"。Go to Edit > Project Settings > Player.
  2. 在 "播放器设置" 的 检查器面板 中,选择 " 通用 Windows 平台 " 图标。In the Inspector Panel for Player Settings, select the Universal Windows Platform icon.
  3. 展开“XR 设置”组。Expand the XR Settings group.
  4. 在“呈现”部分,选中“支持虚拟现实”复选框,添加新“虚拟现实 SDK 的”列表 。In the Rendering section, check the Virtual Reality Supported checkbox to add a new Virtual Reality SDKs list.
  5. 验证列表中是否显示“Windows 混合现实”。Verify that Windows Mixed Reality appears in the list. 如果没有,请选择列表底部的“+”按钮,然后选择“Windows 混合现实” 。If not, select the + button at the bottom of the list and choose Windows Mixed Reality.

备注

如果看不到 " 通用 Windows 平台 " 图标,请仔细检查以确保在安装过程中选择通用 Windows 平台生成支持。If you do not see the Universal Windows Platform icon, double check to make sure you selected Universal Windows Platform Build Support during installation. 如果没有,可能需要使用正确的 Windows 安装重新安装 Unity。If not, you may need to reinstall Unity with the correct Windows installation.

获取应用了所有项目设置的出色作业。Awesome job on getting all the project settings applied. 接下来,让我们添加全息影像!Next, let us add a hologram!

第4章-创建多维数据集Chapter 4 - Create a cube

在 Unity 项目中创建多维数据集类似于在 Unity 中创建任何其他对象。Creating a cube in your Unity project is just like creating any other object in Unity. 将多维数据集放在用户前面非常简单,因为 Unity 的坐标系统会映射到现实世界-其中,Unity 中的一种指示器约为现实中的一个计量。Placing a cube in front of the user is easy because Unity's coordinate system is mapped to the real world - where one meter in Unity is approximately one meter in the real world.

  1. 层次结构 面板的左上角,选择 " 创建 " 下拉列表,然后选择 " 3D 对象 > 多维数据集"。In the top left corner of the Hierarchy panel, select the Create dropdown and choose 3D Object > Cube.
  2. 在 "层次结构" 面板中选择新创建的 多维数据集Select the newly created Cube in the Hierarchy panel
  3. 检查器 中, 找到转换 组件,并将 位置 更改为 (X:0, Y:0, Z: 2) 。In the Inspector find the Transform component and change Position to (X: 0, Y: 0, Z: 2). 这会将 cube 2 计量置于用户的起始位置之前。This positions the cube 2 meters in front of the user's starting position.
  4. 转换 组件中,将 旋转 更改为 (x:45, Y:45, Z: 45) 并将 缩放 更改为 (X:0.25, Y:0.25, Z: 0.25) 。In the Transform component, change Rotation to (X: 45, Y: 45, Z: 45) and change Scale to (X: 0.25, Y: 0.25, Z: 0.25). 这会将多维数据集缩放到0.25 米。This scales the cube to 0.25 meters.
  5. 若要保存场景更改,请选择 " 文件" > 保存场景"。To save the scene changes, select File > Save Scene.

第5章-从 Unity 编辑器验证设备Chapter 5 - Verify on device from Unity editor

现在,我们已经创建了多维数据集,可以在设备中快速完成检查了。Now that we have created our cube, it is time to do a quick check in device. 可以直接从 Unity 编辑器中执行此操作。You can do this directly from within the Unity editor.

初始设置Initial setup

  1. 在开发 PC 的 Unity 中,打开 " 文件 > 生成设置 " 窗口。On your development PC, in Unity, open File > Build Settings window.
  2. 平台 更改为 "通用 Windows 平台",然后单击 "切换平台"Change Platform to Universal Windows Platform and click Switch Platform

对于 HoloLens,使用 Unity 远程处理For HoloLens use Unity Remoting

  1. 在 HoloLens 上,安装并运行 Windows 应用商店提供的 全息远程处理播放器On your HoloLens, install and run the Holographic Remoting Player, available from the Windows Store. 在设备上启动应用程序,该应用程序将进入等待状态并显示设备的 IP 地址。Launch the application on the device, and it will enter a waiting state and show the IP address of the device. 记下 IP。Note down the IP.
  2. > 全息仿真打开 > XR 的窗口Open Window > XR > Holographic Emulation.
  3. 仿真模式None 更改为 远程到设备Change Emulation Mode from None to Remote to Device.
  4. 远程计算机 上,输入你之前记下的 HOLOLENS 的 IP 地址。In Remote Machine, enter the IP address of your HoloLens noted earlier.
  5. 单击“连接” 。Click Connect.
  6. 确保 连接状态 更改为 " 已连接 绿色"。Ensure the Connection Status changes to green Connected.
  7. 现在,你可以在 Unity 编辑器中单击 " 播放 "。Now you can now click Play in the Unity editor.

现在,可以在设备和编辑器中查看多维数据集。You will now be able to see the cube in device and in the editor. 您可以暂停、检查对象和调试,就像您在编辑器中运行应用一样,因为这实质上是发生的事情,但使用视频、音频和设备输入通过网络在主机和设备之间来回传输。You can pause, inspect objects, and debug just like you are running an app in the editor, because that's essentially what's happening, but with video, audio, and device input transmitted back and forth across the network between the host machine and the device.

适用于其他混合现实支持的耳机For other mixed reality supported headsets

  1. 使用 USB 电缆和 HDMI 或显示器端口电缆将耳机连接到开发 PC。Connect the headset to your development PC using the USB cable and the HDMI or display port cable.
  2. 启动 混合现实门户 并确保已完成首次运行体验。Launch the Mixed Reality Portal and ensure you have completed the first run experience.
  3. 现在,可以从 Unity 按下 "播放" 按钮。From Unity, you can now press the Play button.

现在,你将能够在混合现实耳机和编辑器中看到多维数据集呈现。You will now be able to see the cube rendering in your mixed reality headset and in the editor.

第6章-从 Visual Studio 生成并部署到设备Chapter 6 - Build and deploy to device from Visual Studio

现在,我们已准备好将项目编译到 Visual Studio 并部署到我们的目标设备。We are now ready to compile our project to Visual Studio and deploy to our target device.

导出到 Visual Studio 解决方案Export to the Visual Studio solution

  1. 打开 文件 > 生成设置 "窗口。Open File > Build Settings window.
  2. 单击 " 添加打开的场景 " 添加场景。Click Add Open Scenes to add the scene.
  3. 平台 更改为 " 通用 Windows 平台 ",然后单击 " 切换平台"。Change Platform to Universal Windows Platform and click Switch Platform.
  4. 通用 Windows 平台 设置 "中,确保 SDK通用 10In Universal Windows Platform settings, ensure SDK is Universal 10.
  5. 对于 "目标设备",请将封闭像素的 任何设备 保留为 "显示" 或 "切换到 HoloLens"。For Target device, leave to Any Device for occluded displays or switch to HoloLens.
  6. UWP 生成类型 应为 D3DUWP Build Type should be D3D.
  7. UWP SDK 可以保持 最新安装的版本UWP SDK could be left at Latest installed.
  8. 单击“生成”。Click Build.
  9. 在文件资源管理器中,单击 " 新建文件夹 ",然后将文件夹命名为 "App"In the file explorer, click New Folder and name the folder "App".
  10. 选择 应用 文件夹后,单击 " 选择文件夹 " 按钮。With the App folder selected, click the Select Folder button.
  11. 当 Unity 完成生成后,将显示一个 Windows 文件资源管理器窗口。When Unity is done building, a Windows File Explorer window will appear.
  12. 在文件资源管理器中打开 应用程序 文件夹。Open the App folder in file explorer.
  13. 在此示例中打开生成的 Visual Studio 解决方案 (MixedRealityIntroduction)) Open the generated Visual Studio solution (MixedRealityIntroduction.sln in this example)

编译 Visual Studio 解决方案Compile the Visual Studio solution

最后,我们将编译导出的 Visual Studio 解决方案,对其进行部署,然后在设备上试用。Finally, we will compile the exported Visual Studio solution, deploy it, and try it out on the device.

  1. 使用 Visual Studio 中的顶部工具栏,将目标从 " 调试 " 更改为 " 发布 ",将 "从 ARM " 更改为 " X86"。Using the top toolbar in Visual Studio, change the target from Debug to Release and from ARM to X86.

有关部署到设备与模拟器的说明有所不同。The instructions differ for deploying to a device versus the emulator. 按照与你的设置相匹配的说明进行操作。Follow the instructions that match your setup.

通过 Wi-Fi 部署到混合现实设备Deploy to mixed reality device over Wi-Fi

  1. 单击 " 本地计算机 " 按钮旁边的箭头,将部署目标更改为 " 远程计算机"。Click on the arrow next to the Local Machine button, and change the deployment target to Remote Machine.
  2. 输入混合现实设备的 IP 地址,并将 " 身份验证模式 " 更改为 "通用 (未加密的协议") 用于 HoloLens,并将 Windows 用于其他设备。Enter the IP address of your mixed reality device and change Authentication Mode to Universal (Unencrypted Protocol) for HoloLens and Windows for other devices.
  3. 单击 " 调试" > "开始(不调试)"。Click Debug > Start without debugging.

对于 HoloLens,如果这是首次部署到设备,则需要 使用 Visual Studio进行配对。For HoloLens, If this is the first time deploying to your device, you will need to pair using Visual Studio.

通过 USB 部署到混合现实设备Deploy to mixed reality device over USB

确保设备通过 USB 电缆接通电源。Ensure you device is plugged in via the USB cable.

  1. 对于 HoloLens,单击 " 本地计算机 " 按钮旁边的箭头,然后将 "部署目标" 更改为 " 设备"。For HoloLens, click on the arrow next to the Local Machine button, and change the deployment target to Device.
  2. 对于连接到电脑的封闭像素设备,请将设置保留为 "本地计算机"。For targeting occluded devices attached to your PC, keep the setting to Local Machine. 确保已运行 混合现实门户Ensure you have the Mixed Reality Portal running.
  3. 单击 " 调试" > "开始(不调试)"。Click Debug > Start without debugging.

部署到模拟器Deploy to Emulator

  1. 单击 " 设备 " 按钮旁边的箭头,并从下拉菜单中选择 " HoloLens 模拟器"。Click on the arrow next to the Device button, and from drop down select HoloLens Emulator.
  2. 单击 " 调试" > "开始(不调试)"。Click Debug > Start without debugging.

试用你的应用Try out your app

部署你的应用后,请尝试四处移动该多维数据集,并观察它是否在世界各地。Now that your app is deployed, try moving all around the cube and observe that it stays in the world in front of you.

另请参阅See also