123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
-
- namespace NRKernal
- {
- using System;
- using System.Collections.Generic;
- using UnityEngine;
-
- public partial class NRAnchor : MonoBehaviour
- {
-
- private static Dictionary<Int64, NRAnchor> m_AnchorDict = new Dictionary<Int64, NRAnchor>();
-
- public NRTrackable Trackable;
-
- private bool m_IsSessionDestroyed;
-
-
-
- public static NRAnchor Factory(NRTrackable trackable)
- {
- if (trackable == null)
- {
- return null;
- }
- NRAnchor result;
- if (m_AnchorDict.TryGetValue(trackable.GetDataBaseIndex(), out result))
- {
- return result;
- }
- NRAnchor anchor = (new GameObject()).AddComponent<NRAnchor>();
- anchor.gameObject.name = "Anchor";
- anchor.Trackable = trackable;
- m_AnchorDict.Add(trackable.GetDataBaseIndex(), anchor);
- return anchor;
- }
-
- private void Update()
- {
- if (Trackable == null)
- {
- NRDebugger.Error("NRAnchor components instantiated outside of NRInternel are not supported. " +
- "Please use a 'Create' method within NRInternel to instantiate anchors.");
- return;
- }
- if (IsSessionDestroyed())
- {
- return;
- }
- var pose = Trackable.GetCenterPose();
- transform.position = pose.position;
- transform.rotation = pose.rotation;
- }
-
- private void OnDestroy()
- {
- if (Trackable == null)
- {
- return;
- }
- m_AnchorDict.Remove(Trackable.GetDataBaseIndex());
- }
-
-
- private bool IsSessionDestroyed()
- {
- if (!m_IsSessionDestroyed)
- {
- var subsystem = NRSessionManager.Instance.TrackableFactory.TrackableSubsystem;
- if (subsystem != Trackable.TrackableSubsystem)
- {
- Debug.LogErrorFormat("The session which created this anchor has been destroyed. " +
- "The anchor on GameObject {0} can no longer update.",
- this.gameObject != null ? this.gameObject.name : "Unknown");
- m_IsSessionDestroyed = true;
- }
- }
- return m_IsSessionDestroyed;
- }
- }
- }
|