# 构建第一个XR应用

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

## 添加XR Plugin预制体

1. 新建场景将里面的Main Camera，Directional Light删除

<figure><img src="/files/b27BvOTn8M8r1p7hAbus" alt=""><figcaption></figcaption></figure>

2. 进入Packages/RayNeo OpenXR ARDK/SDK/Runtime/Resources/Prefab目录,将XR Plguin预制体拖入场景中

<figure><img src="/files/4SBl8nVZ9MSHieXLojjt" alt=""><figcaption></figcaption></figure>

3. XR Plugin包含以下元素：

<figure><img src="/files/v08yZ8vSAZnqCWhFMOYm" alt=""><figcaption></figcaption></figure>

<table data-header-hidden><thead><tr><th width="170"></th><th></th></tr></thead><tbody><tr><td>元素</td><td>说明</td></tr><tr><td>XR Plugin</td><td>主物体</td></tr><tr><td>LaserBeam</td><td>Camera对应射线控制单元</td></tr><tr><td>BeamGraphic<br></td><td>射线绘制器，绘制射线在空间中的位置，并能让用户感知到，可关闭。</td></tr><tr><td>LaserBeamDot</td><td>射线与物体的交互点，当射线碰撞到物体时，会在碰撞点展示。</td></tr><tr><td>Head</td><td>眼镜3DoF对应相机控制单元</td></tr></tbody></table>

4. 设置Game视图分辨率:在Game视图中,新增一个分辨率640X480(请根据实际设备设置分辨率)

<figure><img src="/files/D6iAk7phsEBGFursK6WJ" alt=""><figcaption></figcaption></figure>

## 添加按钮和输入框

### 1.创建按钮

在Hierarchy窗口中，右键>UI>Legacy>Button

<figure><img src="/files/b2pBnYNlhq2XxRhL77BO" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/nCUagb7I1x746l7G6hqS" alt=""><figcaption></figcaption></figure>

### 2.配置Canvas

1. 选中Canvas，并选择Render Mode为World Space，将RectTransform组件Pos属性设置为（0,0,1000） .同时修改按钮的属性如下图

<figure><img src="/files/6SEOSamMnyK38uMo2XsP" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/bE2AOAkidrevfW4miTah" alt=""><figcaption></figcaption></figure>

2. 需要删除canvas下原有Graphic Raycaster,并添加我们的XRGraphicRaycaster

<figure><img src="/files/ms3D513sLpXMIibZ3eXJ" alt=""><figcaption></figcaption></figure>

### 3.打包并运行

1. 将当前场景加入Scenes In Build中,并点击Build And Run等待进度条完成即可.

<figure><img src="/files/3YTNmzFljdv2KAfCDa4W" alt=""><figcaption></figcaption></figure>

2. 我们在scrcpy中看到双目成功渲染,并且3DoF头控也表现正常.单机镜腿,Button按钮有事件响应.

<figure><img src="/files/u6um5BcjKN6RYoi56vb0" alt=""><figcaption></figcaption></figure>


---

# 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/x-xi-lie/unity-kai-fa/kuai-su-kai-shi/gou-jian-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.
