AnchorSynchronization.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental.Persistence
  10. {
  11. using UnityEngine;
  12. [RequireComponent(typeof(ImageTrackingAnchorTool))]
  13. public class AnchorSynchronization : MonoBehaviour
  14. {
  15. // This is the object which you want to synchronize.
  16. public Transform m_SynchronousTransform;
  17. // This is an anchor load/add tool using image tracking.
  18. private ImageTrackingAnchorTool m_ImageTrackingAnchorTool;
  19. private NRWorldAnchor m_Anchor;
  20. private void Start()
  21. {
  22. m_ImageTrackingAnchorTool = gameObject.GetComponent<ImageTrackingAnchorTool>();
  23. m_ImageTrackingAnchorTool.OnAnchorLoaded += OnImageTrackingAnchorLoaded;
  24. }
  25. void Update()
  26. {
  27. if (m_Anchor != null && m_Anchor.GetTrackingState() == TrackingState.Tracking)
  28. {
  29. m_SynchronousTransform.position = m_Anchor.transform.position;
  30. m_SynchronousTransform.rotation = m_Anchor.transform.rotation;
  31. }
  32. }
  33. private void OnImageTrackingAnchorLoaded(string key, NRWorldAnchor anchor)
  34. {
  35. NRDebugger.Info("[AnchorSynchronization] The anchor:{0} is Loaded", key);
  36. this.m_Anchor = anchor;
  37. }
  38. }
  39. }