by Max Huddleston
由马克斯·哈德斯顿(Max Huddleston)
如何使用Unity创建第一个HoloLens应用 (How to create your first HoloLens app with Unity)
Microsoft’s HoloLens is pretty freaking awesome. I was introduced to it at my internship this summer, and I’ve had a blast developing for it.
微软的HoloLens非常棒。 今年夏天,我在实习期间向我介绍了它,为此我得到了极大的发展。
HoloLens apps are created using either DirectX with C++ or Unity with C#. I found that it is generally quicker and easier to get an app up and running with Unity.
使用C ++的DirectX或C#的Unity创建HoloLens应用。 我发现,使用Unity启动和运行应用程序通常更快,更容易。
In this article, we’ll set up Unity for HoloLens development, create an interactive cube, and spin up the HoloLens emulator so you can see your creation. This article is meant for total beginners to Unity, so if you have experience with the editor, I’d recommend going through Microsoft’s tutorials.
在本文中,我们将为Unity开发HoloLens,创建一个交互式多维数据集,并启动HoloLens仿真器,以便您看到自己的创建。 本文适用于Unity的所有初学者,因此,如果您有使用该编辑器的经验,建议您阅读Microsoft的教程 。
先决条件 (Prerequisites)
- A Windows 10 PC with the April 2018 update 具有2018年4月更新的Windows 10 PC
The HoloLens Emulator which you can download here
您可以在此处下载HoloLens模拟器
Visual Studio 2017 — Community edition is fine
Visual Studio 2017 —社区版很好
Unity — Make sure you add the Windows .NET scripting backend component during installation
Unity —确保在安装过程中添加Windows .NET脚本后端组件
The HoloToolKit
You can find the full source code here.
您可以在此处找到完整的源代码。
建立 (Set up)
Open up Unity. If this is your first time using the editor, you will be prompted to sign in or sign up. I’d recommend Unity Personal unless you plan on monetizing your app.
打开Unity。 如果这是您第一次使用编辑器,将提示您登录或注册。 除非您打算通过应用获利,否则我建议您使用Unity Personal。
After you sign in, create a new project and enter in a name. You should be greeted with this screen after the project completes the initial setup.
登录后,创建一个新项目并输入名称。 项目完成初始设置后,您应该会看到此屏幕。
Right click on the Assets folder in the bottom left corner. Navigate to Import Package -> Custom Package, and open the HoloToolKit unity file we downloaded earlier. A Unity popup will appear, click All and import the assets.
右键单击左下角的Assets文件夹。 导航到导入包->自定义包,然后打开我们之前下载的HoloToolKit统一文件。 将显示一个Unity弹出窗口,单击“全部”并导入资产。
Next we’ll create our scene. Select File -> New Scene and save it as Main.
接下来,我们将创建场景。 选择“文件”->“新场景”并将其另存为“主要”。
Unity needs a special configuration in order to build a project for HoloLens. Luckily, the HoloToolKit has utility scripts which do this configuration for us. On the top toolbar, select Mixed Reality Toolkit -> Configure -> Apply Mixed Reality Project Settings. Use the default values. After the configuration is finished, apply the Mixed Reality Scene Settings. Delete the Directional Light GameObject.
Unity需要特殊的配置才能为HoloLens构建项目。 幸运的是,HoloToolKit具有实用程序脚本,可以为我们执行此配置。 在顶部工具栏上,选择“混合现实工具包”->“配置”->“应用混合现实项目设置”。 使用默认值。 配置完成后,应用混合现实场景设置。 删除Directional Light GameObject。
Awesome! So far we’ve imported the HoloToolKit and set up Unity for HoloLens development. In the next section I’ll introduce you to the basics of Unity, and after that we’ll get a cube to render on screen.
太棒了! 到目前为止,我们已经导入了HoloToolKit,并为HoloLens开发设置了Unity。 在下一节中,我将向您介绍Unity的基础知识,然后,我们将获得一个要在屏幕上呈现的多维数据集。
熟悉编辑器 (Familiarizing yourself with the editor)
This is what the editor should look like at this point. Notice the three boxed-in areas:
这是编辑器此时的外观。 请注意三个方框中的区域:
The red area to the left is the scene hierarchy panel. Here we can add new GameObjects, and get a high-level overview of the current application. A GameObject is a base class for all Unity entities. They can be a physical object like a cube, or a collection of scripts like the Input Manager in the scene.
左侧的红色区域是场景层次面板。 在这里,我们可以添加新的GameObjects ,并获得当前应用程序的高级概述。 GameObject是所有Unity实体的基类。 它们可以是诸如多维数据集的物理对象,也可以是场景中“输入管理器”之类的脚本集合。
The bottom blue area contains the assets menu and console. The assets menu is exactly what it sounds like — it contains the C# scripts, materials, prefabs, and meshes required to run your app.
底部的蓝色区域包含资产菜单和控制台。 资产菜单确实听起来很像-它包含运行您的应用程序所需的C#脚本,材料,预制件和网格。
The right green area is the inspector menu. Here is where you can add and modify the Components of your GameObjects. Components are what make up the behavior of your GameObjects. They determine the look, interactivity, and physics of their parent.
右边的绿色区域是检查器菜单。 您可以在此处添加和修改GameObjects的组件 。 组件是构成GameObjects行为的要素。 他们确定其父母的外观,互动性和物理性质。
创建一个立方体 (Creating a cube)
Now we’ll get our first GameObject to render on screen. In the scene hierarchy panel, right click and select 3D Object -> Cube. A cube should appear in the editor.
现在,我们将第一个GameObject渲染到屏幕上。 在场景层次面板中,右键单击并选择3D对象->多维数据集。 多维数据集应出现在编辑器中。
Double click Cube in the in hierarchy panel, and in the right inspector panel change the position to (0,0,2). Set the scale to be (.25, .25, .25). Your inspector panel should look like this:
双击“层次结构”面板中的“多维数据集”,然后在右侧的检查器面板中将位置更改为(0,0,2)。 将比例设置为(.25,.25,.25)。 您的检查器面板应如下所示:
Nice! Let’s go through the build steps so we can see our cube in the HoloLens emulator.
真好! 让我们完成构建步骤,以便可以在HoloLens仿真器中看到我们的多维数据集。
In Unity, select File->Build Settings. The build popup should appear, so hit the build button. In the folder popup, create a new folder called App and choose it as the build destination.
在Unity中,选择文件->构建设置。 构建弹出窗口应该出现,因此点击构建按钮。 在文件夹弹出窗口中,创建一个名为App的新文件夹,然后选择它作为构建目标。
After the build is completed, open the Visual Studio solution in the App folder. Change the debug options to Release, x86, and target the HoloLens emulator. Your options bar should look like this:
构建完成后,在App文件夹中打开Visual Studio解决方案。 将调试选项更改为Release,x86,并以HoloLens仿真器为目标。 您的选项栏应如下所示:
Run the solution, and after the emulator starts up and your application loads, you should see your cube on screen.
运行解决方案,并且在模拟器启动并加载应用程序之后,您应该在屏幕上看到多维数据集。
使多维数据集具有交互性 (Making the cube interactive)
Now that we’ve figured out how to get a cube into our application, let’s make the cube actually do something. When we gaze at the cube, it will rotate, and when we click the cube, it will increase in size.
现在,我们已经找到了如何将多维数据集放入我们的应用程序,让我们让多维数据集实际执行某些操作。 当我们凝视立方体时,它将旋转,而当我们单击立方体时,它将增大。
Navigate back to Unity and create a new script in the assets panel called InteractiveCube. To create a new script, right click on the assets panel and select Create -> C# script.
浏览回到Unity并在资产面板中创建一个名为InteractiveCube的新脚本。 要创建新脚本,请右键单击资产面板,然后选择创建-> C#脚本。
To add the script to the cube, make sure the cube is selected, and drag and drop the script onto the inspector panel. It should look like this:
要将脚本添加到多维数据集,请确保已选择多维数据集,然后将脚本拖放到检查器面板上。 它看起来应该像这样:
Double click on the script in the assets tab and a Visual Studio instance should appear.
双击资产选项卡中的脚本,应会出现一个Visual Studio实例。
Import HoloToolkit.Unity.InputModule at the top of our script, and make InteractiveCube extend IFocusable and IInputClickHandler. Our code should look like this:
在脚本顶部导入HoloToolkit.Unity.InputModule,并使InteractiveCube扩展IFocusable和IInputClickHandler。 我们的代码应如下所示:
using HoloToolkit.Unity.InputModule;
public class InteractiveCube : MonoBehaviour, IFocusable, IInputClickHandler {...}
By extending the IFocuable and IInputClickHandler interfaces, our script component allows the parent GameObject to subscribe to focus and click events.
通过扩展IFocuable和IInputClickHandler接口,我们的脚本组件允许父级GameObject订阅焦点和单击事件。
Let’s make the cube rotate when our gaze is on it. The IFocusable interface requires us to implement two public void methods: OnFocusEnter and OnFocusExit. Create a private boolean field and name it Rotating. When we focus the cube, set it to true, and when our focus exits, set it to false. Our code should look like this:
让我们在凝视时旋转立方体。 IFocusable接口要求我们实现两个公共无效方法:OnFocusEnter和OnFocusExit。 创建一个私有布尔字段,并将其命名为Rotating。 当我们关注多维数据集时,将其设置为true,当我们退出焦点时,将其设置为false。 我们的代码应如下所示:
public bool Rotating;
public void OnFocusEnter(){ Rotating = true;}
public void OnFocusExit(){ Rotating = false;}
We’ll do the actual rotation in Update(). Update() is a special Unity method that is called every frame. To control the speed of the rotation, add a public float field named RotationSpeed. Any public field in a component can be adjusted and initialized in the Unity editor.
我们将在Update()中进行实际的轮换。 Update()是一种特殊的Unity方法,每帧都会调用一次。 要控制旋转速度,请添加一个名为RotationSpeed的公共浮动字段。 组件中的任何公共字段都可以在Unity编辑器中进行调整和初始化。
public float RotationSpeed;
void Update() { if (Rotating) transform.Rotate(Vector3.Up * Time.deltaTime * RotationSpeed);}
In Unity, the transform is used to control the physical attributes such as size, rotation, and position of a GameObject. We are rotating the parent GameObject around the y-axis at one degree per second multiplied by the speed.
在Unity中,转换用于控制物理属性,例如GameObject的大小,旋转和位置。 我们将父GameObject绕y轴以每秒一度乘以速度旋转。
To make the cube respond to click events, add the OnInputClicked method required by IInputClickHandler. Create a public Vector3 variable called ScaleChange. In the OnInputClicked method, we’ll increase the cube’s scale by ScaleChange.
若要使多维数据集响应单击事件,请添加IInputClickHandler所需的OnInputClicked方法。 创建一个公共的Vector3变量,称为ScaleChange。 在OnInputClicked方法中,我们将通过ScaleChange增加多维数据集的比例。
public Vector3 ScaleChange;
public void OnInputClicked(InputClickedEventData eventData) { transform.localScale += ScaleChange;}
Now that we’re finished with the script, navigate back to Unity. Make sure the Cube object is selected, and set the Speed and ScaleChange variables to 50 and (.025, .025, .025) respectively. Feel free to experiment with different values! Our script should look like this:
现在我们已经完成了脚本,导航回到Unity。 确保选择了Cube对象,并将Speed和ScaleChange变量分别设置为50和(.025,.025,.025)。 随意尝试不同的值! 我们的脚本应如下所示:
Awesome! We’ve finished the InteractiveCube script. Build your app from the Unity editor, and run the solution from Visual Studio.
太棒了! 我们已经完成了InteractiveCube脚本。 从Unity编辑器构建应用,然后从Visual Studio运行解决方案。
摘要 (Summary)
In this walkthrough, you learned how to set up Unity for HoloLens development, create an interactive GameObject, and run your application in the emulator.
在本演练中,您学习了如何为HoloLens开发设置Unity,创建交互式GameObject以及在模拟器中运行应用程序。
If you enjoy HoloLens development, I encourage you to go through the Microsoft Academy tutorials. They go over the various HoloLens key concepts in detail and walk you through creating some pretty cool apps.
如果您喜欢HoloLens开发,我鼓励您阅读Microsoft Academy教程。 他们详细介绍了各种HoloLens关键概念,并引导您创建一些非常酷的应用程序。
If you liked the article or have feedback, leave a comment below!
如果您喜欢这篇文章或有反馈,请在下面发表评论!
翻译自: https://www.freecodecamp.org/news/how-to-create-your-first-hololens-app-with-unity-1afa364843d4/