XRTrackReference.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Collections;
  5. namespace Ximmerse.XR.Tag
  6. {
  7. /// <summary>
  8. /// Track reference. Anchor set transform to tracked marker.
  9. /// </summary>
  10. //[AddComponentMenu("Ximmerse XR/Track reference")]
  11. public class XRTrackReference : MonoBehaviour
  12. {
  13. public int referenceID = 0;
  14. private void OnEnable()
  15. {
  16. XRManager.OnTrackUpdate += XRManager_OnTrackUpdate;
  17. }
  18. private void XRManager_OnTrackUpdate(NativeArray<TrackingResult> tracked, NativeArray<TrackingResult> unTracked, NativeArray<TrackingResult> added, NativeArray<TrackingResult> lost)
  19. {
  20. for (int i = 0; i < tracked.Length; i++)
  21. {
  22. TrackingResult t = tracked[i];
  23. if (t.id == this.referenceID)
  24. {
  25. transform.SetPositionAndRotation(t.worldPose.position, t.worldPose.rotation);
  26. break;
  27. }
  28. }
  29. }
  30. private void OnDisable()
  31. {
  32. XRManager.OnTrackUpdate -= XRManager_OnTrackUpdate;
  33. }
  34. }
  35. }