• [AR Foundation] AR Foundation学习之路(持续记录)


    1.利用手机USB调试,打包安卓项目

    值得注意的是:

    • 手机 开发者模式打开
    • 手机 USB调试打开
    • 连接时为 传送文件

    2.2021版本unity 打包设置示意图:

    在这里插入图片描述

    3.Unity事件函数

    Reset()函数:

    调用时间:当脚本第一次挂载到物体对象上时或者就是在我们的物体的Inspector面板中,对脚本test进行Reset时也会触发。

    作用:来初始化脚本的各个属性,Reset最常用于在检测面板中提供良好的默认值。

    Awake():

    调用时间:

    a.在加载场景资源时,初始化包含脚本的激活状态下的GameObject

    b.GameObject从非激活状态转变为激活状态

    c.在初始化使用Instantiate创建的GameObject之后

    每个游戏物体上的Awke以随机的顺序被调用。

    Awake总是在Start之前被调用。

    Awake像构造函数一样只被调用一次

    OnEnable()

    当对象变为可用或该组件被激活时此函数被调用。

    4.锚点的使用

    在这里插入图片描述
    射线以及锚点添加的实践

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR.ARFoundation;
    using UnityEngine.XR.ARSubsystems;
    
    public class ARPlaneHologram : MonoBehaviour
    {
        // The Prefab to instantiate on touch
        [SerializeField]
        private GameObject _prefabtoPlace;
    
        //Cache ARRaycastMananager GameObject from ARCoreSession
        private ARRaycastManager _aRRaycastManager;
    
        //List for raycast hits is re-used by raycast manager
        private static readonly List<ARRaycastHit> hits = new List<ARRaycastHit>();
    
    
        //Cache ARAnchorMananager GameObject from ARCoreSession
        private ARAnchorManager _aRAnchorManager;
    
        //Cache ARPlaneMananager GameObject from ARCoreSession
        private ARPlaneManager _aRPlaneManager;
        private void Awake()
        {
            _aRRaycastManager = GetComponent<ARRaycastManager>();
            _aRAnchorManager = GetComponent<ARAnchorManager>();
        }
        private void Update()
        {
            // Only consider single-finger touches that are beginning 
            //Touch touch;
            if (Input.touchCount < 1 || Input.GetTouch(0).phase != TouchPhase.Began) return;
    
            //Perform AR raycast to any kind of trackable
            if (_aRRaycastManager.Raycast(Input.GetTouch(0).position ,hits, TrackableType.AllTypes))
            {
                // Raycast hits are sorted by distance, so the first one will be the closest hit
                var hitpose = hits[0].pose;
    
                //Instantiate the prefab at the given position 
                //Instantiate(_prefabtoPlace, hitpose.position, hitpose.rotation);
                ARAnchor anchor;  
                anchor = CreateAnchor(hits[0]);
            }
        }
    
        ARAnchor CreateAnchor(in ARRaycastHit hit)
        {
            ARAnchor anchor;
            
            //if we hit a plane, try to "attach" the anchor to the plane
            if (hit.trackable is ARPlane plane)
            {
                var planeManager = GetComponent<ARPlaneManager>();
    
                if (planeManager)
                {
                    //get original prefab of ARAnchorManager
                    var oldprefab = _aRAnchorManager.anchorPrefab;
    
                    //replace into the prefab of this Script Component
                    //Also,Any prefab is ok if you want
                    _aRAnchorManager.anchorPrefab = _prefabtoPlace;
    
                    //将锚点锚定到平面,当前hit.pose创建锚点
                    anchor = _aRAnchorManager.AttachAnchor(plane, hit.pose);
    
                    //Restore original prefab
    
                    _aRAnchorManager.anchorPrefab = oldprefab;
                    return anchor;
                }
            }
            var instantiateObject = Instantiate(_prefabtoPlace, hit.pose.position, hit.pose.rotation);
    
            //make sure the new GameObject has an ARanchor component
            anchor = instantiateObject.GetComponent<ARAnchor>();
    
            if (anchor == null)
            {
                anchor = instantiateObject.AddComponent<ARAnchor>();
            }
            return anchor;
        }
    
    }
    
    • 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

    输出信息到UI端

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    
    using UnityEngine.XR.ARFoundation;
    
    public class PointCloudInfo : MonoBehaviour
    {
        //
        private ARPointCloud _aRPointCloud;
    
    
        //Reference to logging UI element in the canvas
        public TMP_Text Log;
        private void OnEnable()
        {
            //Subscribe to the event when point cloud changed 
            _aRPointCloud = GetComponent<ARPointCloud>();
            _aRPointCloud.updated += OnPointedChanged;
        }
        private void OnDisable()
        {
            //Unsubscribe event when this element is disabled
            _aRPointCloud.updated -= OnPointedChanged;
        }
    
        private void OnPointedChanged(ARPointCloudUpdatedEventArgs obj)
        {
            if (!_aRPointCloud.positions.HasValue || !_aRPointCloud.identifiers.HasValue || !_aRPointCloud.confidenceValues.HasValue) return;
            var _positions = _aRPointCloud.positions.Value;
            var _identifiers = _aRPointCloud.identifiers.Value;
            var _confidence = _aRPointCloud.confidenceValues.Value;
            if (_positions.Length == 0) return;
            var logText = "Number of points: " + _positions.Length + "\nPoint info: x = "
                      + _positions[0].x + ", y = " + _positions[0].y + ", z = " + _positions[0].z
                      + ",\n Identifier = " + _identifiers[0] + ", Confidence = " + _confidence[0];
            if (Log)
            {
                
                Log.text = logText;
            }
            else
            {
                Debug.Log(logText);
            }
        }
    }
    
    
    • 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

    5.一个比较全面的入门教学

    涉及到平面检测/点云/锚点/事件等知识

  • 相关阅读:
    力扣每日一题 找出数组的第 K 大和 小根堆 逆向思维(TODO:二分+暴搜)
    【C++ 程序】函数积分(使用 std::function)
    【无标题】
    Linux权限
    关键点检测 HRNet网络详解笔记
    element 树组件 tree 横向纵向滚动条
    Scala 05 —— 函数式编程底层逻辑
    vue监听路由变化
    【菜鸡学艺--Vue2--002】[基础指令&[条件与循环]
    SpringMvc 源码分析 (如何自定义视图 + 如何自定义异常) (十四)
  • 原文地址:https://blog.csdn.net/qq_51533157/article/details/126314937