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打开,可能会报错,但是解决一下相应的错误就好,是一个很完整的例子工程。

相关推荐
安卓与AI研习社几秒前
主线程明明是空闲的,为什么仍然 ANR?3 个真实 Trace 的证据链复盘
android
恋猫de小郭1 小时前
Dart 3.13 大更新,感觉比 Flutter 更带劲
android·前端·flutter
大锅盖12 小时前
HarmonyOS 6.1.1 ArkWeb:交付门户下载资料前-为什么必须先登记下载代理与来源字段
android·华为·harmonyos
峥嵘life10 小时前
Android16 311Y3 EAP-TLS 网络连接失败分析与修复总结
android·开发语言·人工智能·php
Kapaseker11 小时前
破坏性更新 - 解读 Jetpack Compose 1.12
android·kotlin
雨白11 小时前
我的 UML 学习笔记:结合 Android 实例看懂 4 种常用图表
android·架构
怣疯knight12 小时前
快速判断apk有没有支持16kb页面标准
android
风流 少年13 小时前
Spring AI 2.0:Advisor
android·人工智能·spring
YM52e14 小时前
分页查询的基石:ArkTS 为鸿蒙商品列表设计 LIMIT/OFFSET 的表
android·学习·华为·harmonyos
安卓与AI研习社16 小时前
Android ANR 到底怎么定位?从触发原理到日志判断的完整方法
android