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

1.利用手机USB调试,打包安卓项目 值得注意的是:

  • 手机 开发者模式打开
  • 手机 USB调试打开
  • 连接时为 传送文件
2.2021版本unity 打包设置示意图: AR|[AR Foundation] AR Foundation学习之路(持续记录)
文章图片

3.Unity事件函数 Reset()函数:
调用时间:当脚本第一次挂载到物体对象上时或者就是在我们的物体的Inspector面板中,对脚本test进行Reset时也会触发。
作用:来初始化脚本的各个属性,Reset最常用于在检测面板中提供良好的默认值。
Awake():
调用时间:
a.在加载场景资源时,初始化包含脚本的激活状态下的GameObject
b.GameObject从非激活状态转变为激活状态
c.在初始化使用Instantiate创建的GameObject之后
每个游戏物体上的Awke以随机的顺序被调用。
Awake总是在Start之前被调用。
Awake像构造函数一样只被调用一次
OnEnable()
当对象变为可用或该组件被激活时此函数被调用。
4.锚点的使用 AR|[AR Foundation] AR Foundation学习之路(持续记录)
文章图片

射线以及锚点添加的实践
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 hits = new List(); //Cache ARAnchorMananager GameObject from ARCoreSession private ARAnchorManager _aRAnchorManager; //Cache ARPlaneMananager GameObject from ARCoreSession private ARPlaneManager _aRPlaneManager; private void Awake() { _aRRaycastManager = GetComponent(); _aRAnchorManager = GetComponent(); } 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(); 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(); if (anchor == null) { anchor = instantiateObject.AddComponent(); } return anchor; }}

输出信息到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.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); } } }

5.一个比较全面的入门教学 【AR|[AR Foundation] AR Foundation学习之路(持续记录)】涉及到平面检测/点云/锚点/事件等知识

    推荐阅读