# 构建您第一个XR应用

本教程帮助您在 Unity 中构建您的第一个 XR 应用程序。这是一个基本的应用程序，它介绍了主要的 Unity 概念，例如 3D 对象、组件和构建设置。

#### **添加 XR Plugin 预制体**

1\.  新建一个场景,将里面的 Main Camera

Directional Light 删除

![](https://testopen.rayneo.cn/media/build-1.png)

2\.  进入 Packages/Ffalcon SDK/Runtime/Prefab 目录

XR Plguin 预制体拖入场景中

![](https://testopen.rayneo.cn/media/build-2.png)

![](https://testopen.rayneo.cn/media/build-3.png)

XR Plugin 包含以下元素：

![](https://testopen.rayneo.cn/media/build-4.png)

| 元素           | 说明                           |
| ------------ | ---------------------------- |
| XR Plugin    | 主物体                          |
| LaserBeam    | 手机 3dof 对应射线控制单元             |
| BeamGraphic  | 射线绘制器，绘制射线在空间中的位置，并能让用户感知到   |
| LaserBeamDot | 射线与物体的交互点，当射线碰撞到物体时，会在碰撞点展示。 |
| Head         | 眼镜 3dof 对应相机控制单元             |

####

#### **添加按钮和输入框**

1\.  在 Hierarchy 窗口中，右键（或者+）>UI>Button

![](https://testopen.rayneo.cn/media/build-5.png)

创建好后，场景中会有如下 prefab 被生成

![](https://testopen.rayneo.cn/media/build-6.png)

2\.  添加 Input Field。在 Canvas 上右键>UI>Input Field

![](https://testopen.rayneo.cn/media/build-7.png)

3\.  配置 Canvas，选中 Canvas，并选择 Render Mode 为 World Space，将 RectTransform 组件 Pos 属性设置为（0,0,60），scale 属性设置为（0.024,0.024,0.024）

![](https://testopen.rayneo.cn/media/build-8.png)

4\.  并且删除 Graphic Raycaster 组件,添加 XR Graphic Raycaster

![](https://testopen.rayneo.cn/media/build-9.png)

5\.  在 Button 的 Rect Transform 组件处，Pos 属性设置为（0，0，0）

并将 Button 中的 Text 内容改为 Reset Space

![](https://testopen.rayneo.cn/media/build-10.png)

6\.  在 InputField 的 Rect Transform 组件处，Pos 属性设置为（0，60，0）

![](https://testopen.rayneo.cn/media/build-11.png)

#### **脚本按钮点击事件绑定**

在 Canvas 上，点击 Add Component，输入 SampleInput，点击 New script>Create and Add 创建并挂载脚本。

![](https://testopen.rayneo.cn/media/build-12.png)

双击 SampleInput 文件，进行编辑

![](https://testopen.rayneo.cn/media/build-13.png)

复制如下代码到 SampleInput 中：

> using FfalconXR.Native;\
> using UnityEngine;\
> using UnityEngine.UI;\
> public class SampleInput : MonoBehaviour\
> {\
> &#x20;  public InputField m\_input;\
> &#x20;  public void ResetInput()\
> &#x20;  {\
> &#x20;      NativeModule.Instance.ResetGlassQuat();\
> &#x20;      NativeModule.Instance.ResetMobileQuat();\
> &#x20;      m\_input.text = "";//清空inputField\
> &#x20;  }\
> }

回到 Unity 中，绑定 InputField，按住 InputField,拖动到 Sample Input 中的 Input 选项中

![](https://testopen.rayneo.cn/media/build-14.png)

绑定按钮点击事件，选中 Button，点击加号，并将 Canvas 拖动到图示区域。选择 Function 为 ResetInput（）

![](https://testopen.rayneo.cn/media/build-15.png)

![](https://testopen.rayneo.cn/media/build-16.png)

![](https://testopen.rayneo.cn/media/build-17.png)

#### **打包验证**

将 Android 手机连接 PC，并开启调试模式

菜单栏 File>Build And Run 将 app 编译安装。

待手机的应用被拉起时，插入雷鸟 Air，转动头，寻找到按钮和 InputField， 点击按钮可以初始化空间位置为当前，点击 InputField 可以输入文字。

(运行时,需要遵守启动流程: 插上雷鸟 XR 眼镜，先运行雷鸟 XR 眼镜 App，等 app 开启且切换 3D 模式成功后（需要在后台运行，不能杀掉），之后再运行您的 App 即可。)

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rayneo.gitbook.io/rayneo-devdoc/air-xi-lie/unity-kai-fa/kuai-su-kai-shi/gou-jian-nin-di-yi-ge-xr-ying-yong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
