Unity ARFoundation 配置工程 (Android)

注意:
1、AR Core是Google的产品,因为谷歌制裁华为,所以 有些 华为机可能不支持AR Core的软件;
2、手机在设置里搜索Google Play,看看是否已经安装上了,如果没有装此服务,去商城里搜索Google Play,安装到手机上,如果商城里搜不到,就去网上下载一个Google Play装到手机上(有些设备即使装上也可能无法使用);

1、继承Packages

打开Package Manager,导入以下几个包;

2、工程切换到安卓平台

3、配置Player Settings

取消多线程渲染,没必要勾选

Min API Level 选择24;

Target API Level用本地最高的就ok,一般是在29及以上就可以,我本地用的是32

选择IL2CPP打包

ARCore不支持32位的,取消勾选ARMv7,选择ARM64;不然打出包来运行会闪退。

4、新建测试场景

新建场景,然后在Hierarchy面板删除Camera,添加下图中标注的AR Session Origin和AR Session

写个测试脚本(AppController.cs),我挂到了AR Session Origin物体上了。

csharp 复制代码
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARRaycastManager))]
public class AppController : MonoBehaviour
{
    public GameObject spawnPrefab;

    private List<ARRaycastHit> Hits;
    private ARRaycastManager mRaycastManager;
    private GameObject spawnedObject = null;


    private void Start()
    {
        Hits = new List<ARRaycastHit>();
        mRaycastManager = GetComponent<ARRaycastManager>();
    }

    void Update()
    {
        if (Input.touchCount == 0)
            return;
        var touch = Input.GetTouch(0);
        if (mRaycastManager.Raycast(touch.position, Hits, TrackableType.PlaneWithinPolygon | TrackableType.PlaneWithinBounds))
        {
            var hitPose = Hits[0].pose;
            if (spawnedObject == null)
            {
                spawnedObject = Instantiate(spawnPrefab, hitPose.position, hitPose.rotation);
            }
            else
            {
                spawnedObject.transform.position = hitPose.position;
            }
        }
    }
}

Spawn Prefab 就是一个小方块,目的是检测到平面后显示在平面上

然后保存场景,打包到真机上测试

相关网址:

1、AR Foundation的官方文档:点击跳转链接

2、谷歌ARCore 的官方例子(不是Unity的工程,可以用Android Studio打开,打包到真机上测试):点击跳转Git;可以下载谷歌官方例子,打包到真机上测试官方案例是否可以正常运行。

3、AR Foundation的例子工程,ARFoundation Samples:点击跳转Git;下载到本地后,用Unity打开,可能会报错,但是解决一下相应的错误就好,是一个很完整的例子工程。

相关推荐
阿巴斯甜12 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker13 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952714 小时前
Andorid Google 登录接入文档
android
黄林晴15 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_1 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android