# 平面检测（beta）

## **简单介绍**

平面检测是利用传感器等设备，对物体表面或空间环境中的平面区域进行识别和提取的过程。在检测时，选择纹理丰富的平面很重要。因为纹理多的平面，其特征点更明显，能让检测设备更精准地捕捉到平面的边界和形状，从而显著提升平面识别的准确性和效率。

## **API介绍**

> 运行Sample中的PlaneDetection即可查看平面检测sample code. 在编辑器中,使用宏编写了测试数据.

[头控模式请选择SLAM模式](/rayneo-devdoc/x-xi-lie/unity-kai-fa/kuai-su-kai-shi/dao-ru-ardk-ji-pei-zhi.md#tou-kong-mo-shi-xuan-ze)

Algorithm 脚本，参考 TestPlaneDetection 使用

<table data-header-hidden><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>调用方法</td><td>使用说明</td><td>示例代码</td></tr><tr><td>开启平面检测Algorithm.EnablePlaneDetection()<br></td><td><strong>参数与返回值细节：函数无参数和返回</strong><br><strong>异常场景说明：</strong>注意拥有相机权限<br><strong>调用时机建议：在需要平面检测的时进行开启</strong></td><td><pre class="language-c#"><code class="lang-c#">Algorithm.EnablePlaneDetection();
</code></pre><p><br></p></td></tr><tr><td>获取当前平面检测结果.创建XRPlaneInfo并维护.Algorithm.GetPlaneInfo(m_infoArrays)</td><td><strong>参数与返回值细节：函数无参数和返回</strong>/// &#x3C;param name="info">输出平面信息&#x3C;/param>/// &#x3C;returns>平面个数&#x3C;/returns><br><strong>异常场景说明：</strong>注意拥有相机权限<br><strong>调用时机建议：在需要平面检测的时进行开启</strong></td><td><pre class="language-c#"><code class="lang-c#">int res = Algorithm.GetPlaneInfo(m_infoArrays);
</code></pre></td></tr><tr><td>关闭平面检测Algorithm.DisablePlaneDetection()</td><td><strong>参数与返回值细节：函数无参数和返回</strong><br><strong>异常场景说明：</strong>注意拥有相机权限<br><strong>调用时机建议：在不需要平面检测的时进行开启</strong></td><td><pre class="language-c#"><code class="lang-c#">Algorithm.DisablePlaneDetection();
</code></pre></td></tr></tbody></table>

## **构建场景**

搭建好基础环境，并且导入 Sample 功能能力 Demo 。

![](/files/ce2e7cf2b7e1982f6298fe139c637b2dfea87715)

接下来我们参见 Sample 中 PlaneDetection,进行实现 该案例

### **1. 新建场景**

新建Unity3D 工程，取名 Plane\_Detection

![](/files/8ed97775264499f433cfe71227c22270ca5e4bc7)

### **2. 替换 MainCamera 为 XR Plugin**

打开场景，删除默认的MainCamera

![](/files/563d0068556f13a47b25a0f7ad782c15cfd5417c)

在 packages 目录下找到 RayNeo OPenXR ARDK - SDK - Runtime - Resources - Prefab 文件夹下 的 XR Plugin 预制体

![](/files/7e30dbe962059eef8756a5035568173b512ee37f)

把 XR Plugin 预制体 拖到场景中

![](/files/dfe854edfaa6eaf7d8a31f4a48fde75343719efb)

展开场景中 XR Plugin ，找到 Head ，右键进行 Copy 一份

![](/files/94fd98a20579a0e1c4470e0a84a62336c0b764fe)

把 Head 粘贴 Paste 到场景中，删除场景中 XR Plugin

![](/files/1723716e455bc0145b7434dd9e07182e96d86a90)

然后再把 Head 下的 LaserBeam 子组件也删掉

![](/files/e6854c1cce3345ed4fc9d874c6424c742d8a8eb4)

![](/files/398f974439d9c3ca2ed780bac7a560c515dfdfbf)

### **3. 在场景中添加资源**

鼠标右键，在场景中添加一个 Canvas

![](/files/cc6d216e8268febeb10073542d4ff13ea12df0da)

（如果场景中自动添加了 Event System ，可以手动删除）

在 Canvas 添加两个 Text 如下

![](/files/8de371e06cc3e92c840a72f2683944dcc998147d)

Text 的一些设置如图

![](/files/1dc7ab117f25ca4334cb82ef7950c822a6bdaff1)

![](/files/85ebb786068a349ce207950af6f58577ff96cd98)

### **4. 添加一个 TestPlaneDetection 测试平面检测功能**

首先把 Canvas 的 RenderMode 设置为 ScrennSapce-Camera，添加 Render Camera 为 Head 上的 Camera

![](/files/3e71fc28f31c665ec4bc8564e6f6be068c4fc75f)

在 Canvas 物体上挂载脚本 SlamDemoCtrl ，并把场景中的 GreenMat ，以及对应的Text 对应赋值到脚本中，脚本其他设置如下

![](/files/f632a16bc20aab3fa5020388cb9ea65a07f4d59c)

![](/files/997ed090f3cc5bc7daff4dc88c3d8882ea28e22b)

### **5. 添加双击 TP 操控板退出应用功能**

在 Scripts 文件夹（没有自行创建即可）下，创建一个 QuitApp 脚本

![](/files/214bba26b8f1369dffd2953cd37169b7753aa368)

双击脚本，打开编辑器，编写双击 TP 退出代码

```c#
using RayNeo;
using UnityEngine;

public class QuitApp : MonoBehaviour
{
    /// <summary>
    /// 退出应用
    /// </summary>
    public void ToQuitApp()
    {
        Application.Quit();
    }

    void Start()
    {
        // 添加双击事件
        SimpleTouchForLite.Instance.OnDoubleTap.AddListener(ToQuitApp);
    }

    private void OnDestroy()
    {
        if (SimpleTouchForLite.SingletonExist)
        {
            // 移除双击事件
            SimpleTouchForLite.Instance.OnDoubleTap.RemoveListener(ToQuitApp);
        }

    }
}
```

把 QuitApp 挂载到 Canvas 物体上

![](/files/7c81b44ef7fcc3f192c0fd2cfb8051e04e0551e4)

## **编译运行场景**

### **1. 编译打包 APK**

把 Plane\_Detection 场景 添加到 Scene In Build 中，点击 Build 进行 打包 APK

![](/files/156dae961965ad09f4703cc0d74754b1125050e6)

### **2. 连接设备**

X3pro 眼镜通过设备线，连接电脑，（其中，电脑 ADB 环境参见手机已经配置好环境），使用 adb devices ，查看连接的设备

![](/files/03bd079d5b6c9800cf3a20fa5a2863e03503c3d2)

### **3. 安装应用**

之前编译打包好的应用，使用 adb install xxx.apk 进行安装

![](/files/3d690b65fb880d61bbb10474db3c4f1bde14d1bf)

### **4. 查看运行应用**

可以在眼镜上看到刚刚安装的应用，如下图

![](/files/c150ea7ef5f43c847f509407c9291db8fc423a5a)

单击 TP 操作板，运行应用，简单效果如下

<figure><img src="/files/Va8pDKponcyeSpLthzDt" 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/ar-neng-li-api/ping-mian-jian-ce-beta.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.
