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

相关推荐
向宇it29 分钟前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦6 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl8 小时前
Mysql测试题
android·adb
游戏开发爱好者811 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号11 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
快乐觉主吖11 小时前
Unity网络通信的插件分享,及TCP粘包分包问题处理
tcp/ip·unity·游戏引擎
星辰也为你祝福h12 小时前
Android原生Dialog
android
梁同学与Android13 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android
Misha韩14 小时前
React Native 基础tabBar和自定义tabBar - bottom-tabs
android·react native