Unity接入Huawei AR Engine

使用Unity进行AR开发的开发者基本都会遇到华为手机的坎:由于谷歌的制裁,ARCore并不能覆盖华为的新机型导致新的机型已经不能使用ARFoundation方案;使用第三方通用的ARsdk也并不能完美兼容常用的华为机型,毕竟官方原生的sdk才是最适配自身机型的。最近我也在学习接入HuaweiAREngine与ARFoundation兼容使用的项目,搜集了一些解决方案并进行了总结,由于unity相关的方案官方迟迟还没有更新(官方也关闭了旧的2.0版本的下载入口 ),下面提供的sdk版本也是测试版本,仅供学习使用。

一、简介

Huawei AR Engine 是⼀个在 Android 智能⼿机上构建增强现实应用程序的平台。目前支持的功能包括:

运动跟踪、平面检测、光照估计和命中测试、手势识别和骨骼跟踪、人体骨骼跟踪、人体蒙版、图像跟踪、场景网格、面部表情

二、Huawei AR Engine的unitypackage介绍

目前华为官网上是找不到unity相关的sdk,但目前华为论坛上还是存在内测版的Huawei AR Engine的unitypackage,我这边有两个unitypackage的版本,分别对应不同的unity版本使用

其中:

1.arenginesdk-sample-unity-2.0.0.5
环境要求:

手机EMUI版本9.0以上
在应用市场下载并安装AREngine
Unity 2017.4LTS 以上 Unity2019.4LTS以下的版本
2.arenginesdk-sample-unity-3.0.0.11
环境要求:

手机EMUI版本9.0以上
在应用市场下载并安装AREngine
Unity2020LTS以上的版本
因为Huawei AREngine需要用到一个ARBackgroundRenderer的类来渲染AR场景,这个类在2020以上的版本已经被其他的类取代,但AREngine找不到引用所以导致报错。

所以建议Unity2019LTS以下的版本使用2.0的版本,Unity2020以上的版本只能使用3.0以上的版本,开发者需要根据自己的unity版本来使用sdk。

三、SDK的接入与使用

因为2.0与3.0除了一些细节部分外使用的流程基本一致,所以这里就不分开直接一起说明。导入sdk后,在Example文件夹可以查看官方提供的所有demo

每个demo文件夹下有对应的功能配置,每个场景都需要加载对应的配置才能正常使用, 这里只挑一个demo作简单介绍。 

 WorldARSample这个demo主要是演示AR模型识别真实环境的地面并放置

配置文件的属性介绍如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**\if english
 * Lighting mode of an configuration object. Default value is AMBIENT_INTENSITY。
 * \else
 * 配置项的光照模式。默认启动环境光。
 * \endif
 */
public ARConfigLightingMode LightingMode = ARConfigLightingMode.AMBIENT_INTENSITY;
 
/**\if english
 * Update mode of an configuration object. Default value is BLOCKING。
 * \else
 * 配置项的更新模式。默认阻塞模式。
 * \endif
 */
public ARConfigUpdateMode UpdateMode = ARConfigUpdateMode.BLOCKING;
 
/**\if english
 * Power mode of an configuration object. Default value is NORMAL。
 * \else
 * 配置项的功耗模式。默认不使用低功耗。
 * \endif
 */
public ARConfigPowerMode PowerMode = ARConfigPowerMode.NORMAL;
 
/**\if english
 * Item of depth switch of an configuration object. Default value is \c true。
 * \else
 * 配置项的深度开关。默认打开深度流。
 * \endif
 */
public bool EnableDepth = true;
/**\if english
 * Item of mask switch an configuration object. Default value is \c false.
 * \else
 * 配置项的遮罩开关。默认关闭遮罩功能。
 * \endif
 */
public bool EnableMask = false;
/**\if english
 * Item of scenemesh switch an configuration object. Default value is \c false.
 * \else
 * 配置项的环境Mesh开关。默认关闭环境Mesh。
 * \endif
 */
public bool EnableMesh = false;
 
/**\if english
 * Enable semantic plane mode. Default value is \c false.
 * \else
 * 配置使能语义识别平面模式。默认关闭。
 * \endif
 */
public bool SemanticPlaneMode = false;
 
/**\if english
 * The way the configuration item is opened by the camera. The camera is turned on internally by default.
 * \else
 * 配置项的相机打开方式。默认内部打开相机。
 * \endif
 */
public int ImageInputMode = 0;
/**
 * \if english
 * Focus mode of this configuratioin. Default is FIXED_FOCUS.
 * \else
 * 对焦模式,默认是锁定对焦到无穷远。
 * \endif
 */
public ARConfigFocusMode FocusMode = ARConfigFocusMode.FIXED_FOCUS;
 
/**
 * \if english
 * Select the behavior of the plane detection subsystem. Default is Enable.
 * \else
 * 设置平面检测的行为,默认使能。
 * \endif
 */
public ARConfigPlaneFindingMode PlaneFindingMode =ARConfigPlaneFindingMode.ENABLE;
 
///@cond ContainImageAR
/**
 * \if english
 * Set the database of image tracking. Default is null.
 * \else
 * 设置图像跟踪的数据库。
 * \endif
 */
public ARAugmentedImageDatabase AugmentedImageDatabase = null;

  

开发者可以根据自己需要的功能对配置表进行配置。

使用Huawei AREngine sdk开发时有些需要注意的细节:

1.判断设备是否可用AR,可以查阅sdk中的AREnginesSelector.cs类

1
2
3
4
5
AREnginesAvaliblity aREnginesAvaliblity = AREnginesSelector.Instance.CheckDeviceExecuteAbility(); 
if ((AREnginesAvaliblity.HUAWEI_AR_ENGINE&&aREnginesAvaliblity) != 0) 
{   
    AREnginesSelector.Instance.SetAREngine(AREnginesType.HUAWEI_AR_ENGINE); 
}}

2.检测设置是否已经安装AR的依赖(AREngine.apk),可以查阅sdk中的AREnginesApk.cs类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
        /**
         * \if english
         * @brief Request to insatll the <em>HUAWEI AR Engine.apk</em> synchronously.
         *
         * We recommand you to call this method in
         * <a href="https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html">OnApplictionPause(bool)</a>.
         * When the application starts, set \c userRequestedInstall=true.
         * If <em>HUAWEI AR Engine.apk</em> is installed and compatiable, this method will return
         * \link ARInstallStatus.INSTALLED\endlink immediately.
         * Otherwise, this function will firstly check current device availability. If the device is supported, this function
         * show a window to prompt user. If user agree, it will jump to huawei application store. And then this function returns
         * \link ARInstallStatus.INSTALL_REQUESTED\endlink.
         *
         * When your application resume, you should call this method again with \c userRequestedInstall=false.
         * This will either return INSTALLED or throw an exception indicating the reason that installation could not be completed.
         * @param userRequestedInstall If set \c true, override the previous installation failure message and perform the installation again.
         * @return The Install status of HUAWEI AR Engine.
         * \else
         * @brief 同步请求安装<em>HAUWEI AR Engine.apk</em>。
         *
         * 推荐在<a href="https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html">OnApplictionPause(bool)</a>
         * 中调用该方法。
         *
         * 当应用启动时,设置 \c userRequestedInstall=true。如果已经安装<em>HUAWEI AR Engine.apk</em>并且与SDK版本兼容,该方法将直接
         * 返回\link ARInstallStatus.INSTALLED\endlink。否则,该方法将首先检查设备的兼容性。如果设备支持,该方法将弹出一个提示框,提示用户跳转
         * 到华为应用市场下载。
         *
         * 当下载完成后,应用恢复,应用应该用\c userRequestedInstall=false 调用该方法。该方法将返回INSATLLED或者抛出异常。
         * @param userRequestedInstall 如果为\c true,将清除之前请求安装的错误信息,重新请求。
         * @return HUAWEI AR Engine的安装状态。
         * \endif
         * @exception ARUnavailableDeviceNotCompatibleException \copybrief ARUnavailableDeviceNotCompatibleException
         * @exception ARUnavailableEmuiNotCompatibleException \copybrief ARUnavailableEmuiNotCompatibleException
         * @exception ARUnavailableUserDeclinedInstallationException \copybrief ARUnavailableUserDeclinedInstallationException
         * @exception ARUnavailableConnectServerTimeOutException \copybrief ARUnavailableConnectServerTimeOutException
         */
bool installRequested = false;
switch (AREnginesApk.Instance.RequestInstall(!installRequested))
{   
    case ARInstallStatus.INSTALL_REQUESTED:     
        installRequested = true;   
        return;   
    case ARInstallStatus.INSTALLED:   
        break;
}

3.AR功能的启用,可以查阅ARSession.cs类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * \if english
 * @brief Create a new ARSession.
 *
 * Before calling this method, application must firstly confirm that HUAWEI AR Engine is installed
 * and compatiable on current device. Otherwise, exceptions may throwed.
 * \else
 * @brief 创建一个新的会话。
 *
 * 调用该方法前,应用应该首先保证引擎已经安装并且兼容,否则,将抛出异常。
 * \endif
 * @exception ARUnavailableServiceNotInstalledException \copybrief ARUnavailableServiceNotInstalledException
 * @exception ARUnavailableServiceApkTooOldException \copybrief ARUnavailableServiceApkTooOldException
 * @exception ARUnavailableDeviceNotCompatibleException \copybrief ARUnavailableDeviceNotCompatibleException
 * @exception ARUnavailableEmuiNotCompatibleException \copybrief ARUnavailableEmuiNotCompatibleException
 */
public static void CreateSession()
{
    ARSessionManager.Instance.CreateSession();
}

  

四、应用的导出与使用

输出版本是建议是选择android 24以上的版本

在使用2.0版本的sdk时,输出sdk前需要在project setting->huawei AR ,把Huawei AR Required勾选上,不然输出的版本可能会黑屏

 如果设置了上面的选项还是出现黑屏的情况,那还需要检查是否有开启Camera的权限,可以修改工程的AndroidManifest.xml来增加camera权限,或者直接在脚本上增加权限检测的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    public void CheckAndroidPermission()
    {
  
#if UNITY_ANDROID && !UNITY_EDITOR
        Debug.Log("====申请安卓的摄像机权限");
        permissionList.Add(Permission.Camera);
        foreach (string permission in permissionList)
        {
            if (Permission.HasUserAuthorizedPermission(permission))
            {
  
            }
            else
            {
                Permission.RequestUserPermission(permission);
            }
        }
#endif
    }

如果以上都设置了还是使用不了AR功能的话,就需要检查下机型是否支持AR,是否已经安装AREngine的apk等等。

五、小结

华为AREngine sdk输出的工程能在华为的机型(本人测试的设备是mate pad)下流畅运行,基本与同等硬件的ARCore机型的效果差不多,甚至会更好一点。而且作为兼容方案与ARFoundation一起输出也并不会冲突,可以用作华为机型与ARCore机型的解决方案(启动前先做设备支持性的检测),期待官方正式版的发布!

最后附上两个版本的Huawei Engine SDK

2.0版本:arenginesdk-sample-unity-2.0.0.5.zip

链接:https://pan.baidu.com/s/1mi2-dASmvfgxIQ-ahb8wHQ
提取码:6fgl

3.0版本:arenginesdk-sample-unity-3.0.0.11.zip

链接:https://pan.baidu.com/s/1xIHjwnzloS2LcZYCeGhXHQ
提取码:h6su

posted @   多见多闻  阅读(1182)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示