using System; using System.Collections.Generic; using UnityEngine.XR.ARSubsystems; namespace UnityEngine.XR.ARFoundation { /// /// Manages reference points. /// /// /// Use this component to programmatically add, remove, or query for /// reference points. Reference points are `Pose`s in the world /// which will be periodically updated by an AR device as its understanding /// of the world changes. /// Subscribe to changes (added, updated, and removed) via the /// event. /// /// [DisallowMultipleComponent] [RequireComponent(typeof(ARSessionOrigin))] [HelpURL(HelpUrls.ApiWithNamespace + nameof(ARReferencePointManager) + ".html")] [Obsolete("ARReferencePointManager has been deprecated. Use ARAnchorManager instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager", true)] public sealed class ARReferencePointManager : ARTrackableManager< XRReferencePointSubsystem, XRReferencePointSubsystemDescriptor, XRReferencePointSubsystem.Provider, XRReferencePoint, ARReferencePoint> { [SerializeField] [Tooltip("If not null, instantiates this prefab for each instantiated reference point.")] GameObject m_ReferencePointPrefab; /// /// This Prefab will be instantiated for each . Can be `null`. /// [Obsolete("ARReferencePointManger.referencePointPrefab has been renamed. Use ARAnchorManager.anchorPrefab instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager.anchorPrefab", true)] public GameObject referencePointPrefab { get => m_ReferencePointPrefab; set => m_ReferencePointPrefab = value; } /// /// Invoked once per frame to communicate changes to reference points, including /// new reference points, the update of existing reference points, and the removal /// of previously existing reference points. /// [Obsolete("ARReferencePointManger.referencePointsChanged has been renamed. Use ARAnchorManager.anchorsChanged instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager.anchorsChanged", true)] public event Action referencePointsChanged; /// /// Attempts to add an with the given Pose. /// /// /// If /// is not null, a new instance of that Prefab will be instantiated. Otherwise, a /// new GameObject will be created. In either case, the resulting /// GameObject will have an component on it. /// /// The pose, in Unity world space, of the . /// A new if successful, otherwise null. [Obsolete("ARReferencePointManger.AddReferencePoint() has been deprecated. Use ARAnchorManager.AddAnchor() instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager.AddAnchor(*)", true)] public ARReferencePoint AddReferencePoint(Pose pose) { if (!enabled) throw new InvalidOperationException("Cannot create a reference point from a disabled reference point manager."); if (subsystem == null) throw new InvalidOperationException("Reference point manager has no subsystem. Enable the manager first."); var sessionRelativePose = sessionOrigin.trackablesParent.InverseTransformPose(pose); // Add the reference point to the XRReferencePointSubsystem XRReferencePoint sessionRelativeData; if (subsystem.TryAddReferencePoint(sessionRelativePose, out sessionRelativeData)) return CreateTrackableImmediate(sessionRelativeData); return null; } /// /// Attempts to create a new reference point that is attached to an existing . /// /// The to attach to. /// The initial Pose, in Unity world space, of the reference point. /// A new if successful, otherwise null. [Obsolete("ARReferencePointManger.AttachReferencePoint() has been deprecated. Use ARAnchorManager.AttachAnchor() instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager.AttachAnchor(*)", true)] public ARReferencePoint AttachReferencePoint(ARPlane plane, Pose pose) { if (!enabled) throw new InvalidOperationException("Cannot create a reference point from a disabled reference point manager."); if (subsystem == null) throw new InvalidOperationException("Reference point manager has no subsystem. Enable the manager first."); if (plane == null) throw new ArgumentNullException("plane"); var sessionRelativePose = sessionOrigin.trackablesParent.InverseTransformPose(pose); XRReferencePoint sessionRelativeData; if (subsystem.TryAttachReferencePoint(plane.trackableId, sessionRelativePose, out sessionRelativeData)) return CreateTrackableImmediate(sessionRelativeData); return null; } /// /// Attempts to remove an . /// /// The reference point you wish to remove. /// /// True if the reference point was successfully removed. /// False usually means the reference point is not longer tracked by the system. /// [Obsolete("ARReferencePointManger.RemoveReferencePoint() has been deprecated. Use ARAnchorManager.RemoveAnchor() instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager.RemoveAnchor(*)", true)] public bool RemoveReferencePoint(ARReferencePoint referencePoint) { if (!enabled) throw new InvalidOperationException("Cannot create a reference point from a disabled reference point manager."); if (subsystem == null) throw new InvalidOperationException("Reference point manager has no subsystem. Enable the manager first."); if (referencePoint == null) throw new ArgumentNullException("referencePoint"); if (subsystem.TryRemoveReferencePoint(referencePoint.trackableId)) { DestroyPendingTrackable(referencePoint.trackableId); return true; } return false; } /// /// Gets the with given , /// or null if it does not exist. /// /// The of the to retrieve. /// The with or null if it does not exist. [Obsolete("ARReferencePointManger.GetReferencePoint() has been deprecated. Use ARAnchorManager.GetAnchor() instead (UnityUpgradable) -> UnityEngine.XR.ARFoundation.ARAnchorManager.GetAnchor(*)", true)] public ARReferencePoint GetReferencePoint(TrackableId trackableId) { ARReferencePoint referencePoint; if (m_Trackables.TryGetValue(trackableId, out referencePoint)) return referencePoint; return null; } /// /// Gets the Prefab that will be instantiated for each . /// /// The Prefab that will be instantiated for each . protected override GameObject GetPrefab() => m_ReferencePointPrefab; /// /// The name given to each `GameObject` associated with each . /// protected override string gameObjectName => "ReferencePoint"; /// /// Invoked when the base class detects trackable changes. /// /// The list of added s. /// The list of updated s. /// The list of removed s. protected override void OnTrackablesChanged( List added, List updated, List removed) { if (referencePointsChanged != null) { using (new ScopedProfiler("OnReferencePointsChanged")) referencePointsChanged( new ARReferencePointsChangedEventArgs( added, updated, removed)); } } } }