using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems; namespace UnityEngine.XR.ARFoundation.Samples { [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(); m_AnchorManager = GetComponent(); m_Anchors = new List(); } 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 s_Hits = new List(); List m_Anchors; ARRaycastManager m_RaycastManager; ARAnchorManager m_AnchorManager; } }