ARFoundation快速入门-10添加锚点

一、添加平面检测和平面可视化

请参考 ARFounnation快速入门-08平面检测 案例

二、添加射线检测

1.在 AR Session Origin 组件上添加 ARRaycastManager 脚本

三、添加锚点管理 

1.新建一个 Cube 将大小调整为0.1f,做成Prefab后删除掉

2.在 AR Session Origin 对象上添加 ARAnchorManager 脚本,并将 Cube 拖动到 Anchor Prefab 

四、创建锚点

1.新建一个脚本命名为 AnchorCreator 代码如下

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARAnchorManager))]
[RequireComponent(typeof(ARRaycastManager))]
public class AnchorCreator : MonoBehaviour
{
    public void RemoveAllAnchors()
    {
        foreach (var anchor in m_Anchors)
        {
            m_AnchorManager.RemoveAnchor(anchor);
        }
        m_Anchors.Clear();
    }

    void Awake()
    {
        m_RaycastManager = GetComponent<ARRaycastManager>();
        m_AnchorManager = GetComponent<ARAnchorManager>();
        m_Anchors = new List<ARAnchor>();
    }

    void Update()
    {
        if (Input.touchCount == 0)
            return;

        var touch = Input.GetTouch(0);
        if (touch.phase != TouchPhase.Began)
            return;

        if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.FeaturePoint))
        {
            // Raycast hits are sorted by distance, so the first one
            // will be the closest hit.
            var hitPose = s_Hits[0].pose;
            var anchor = m_AnchorManager.AddAnchor(hitPose);
            if (anchor == null)
            {
                Logger.Log("Error creating anchor");
            }
            else
            {
                m_Anchors.Add(anchor);
            }
        }
    }

    static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();

    List<ARAnchor> m_Anchors;

    ARRaycastManager m_RaycastManager;

    ARAnchorManager m_AnchorManager;
}

2.最后将 AnchorCreator 脚本挂载在 AR Session Origin 对象上 ,大功告成!

 

猜你喜欢

转载自blog.csdn.net/a451319296/article/details/106060731