AnchorLoader.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.NRExamples
  10. {
  11. using NRKernal.Experimental.Persistence;
  12. using UnityEngine;
  13. [RequireComponent(typeof(ImageTrackingAnchorTool))]
  14. public class AnchorLoader : MonoBehaviour
  15. {
  16. // This is the object which you want to load
  17. public GameObject AnchorPrefab;
  18. // This is an anchor load/add tool using image tracking.
  19. private ImageTrackingAnchorTool m_ImageTrackingAnchorTool;
  20. private void Start()
  21. {
  22. m_ImageTrackingAnchorTool = gameObject.GetComponent<ImageTrackingAnchorTool>();
  23. m_ImageTrackingAnchorTool.OnAnchorLoaded += OnImageTrackingAnchorLoaded;
  24. }
  25. /// <summary>
  26. /// After local anchor been loaded
  27. /// </summary>
  28. /// <param name="key"></param>
  29. /// <param name="anchor"></param>
  30. private void OnImageTrackingAnchorLoaded(string key, NRWorldAnchor anchor)
  31. {
  32. // Load your prefab as a child of the anchor
  33. var go = Instantiate(AnchorPrefab);
  34. go.transform.SetParent(anchor.transform);
  35. go.transform.localPosition = Vector3.zero;
  36. go.transform.localRotation = Quaternion.identity;
  37. go.name = key;
  38. }
  39. }
  40. }