ImageLibraryTest.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.XR.ARFoundation;
  6. public class ImageLibraryTest : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private Text text;
  10. [SerializeField]
  11. private Text text2;
  12. [SerializeField]
  13. private ARTrackedImageManager imagerManager;
  14. [SerializeField]
  15. private List<ARTrackedImage> listARTrackedImage;
  16. [SerializeField]
  17. private bool state = false;
  18. private ARTrackedImage oldTrackedImage;
  19. public void Start()
  20. {
  21. listARTrackedImage = new List<ARTrackedImage>();
  22. oldTrackedImage = null;
  23. text.text = "打开识别图管理 ";
  24. }
  25. private void Update()
  26. {
  27. text2.text = imagerManager.trackables.count.ToString();
  28. if (oldTrackedImage != null && oldTrackedImage.trackingState != UnityEngine.XR.ARSubsystems.TrackingState.Tracking)
  29. {
  30. // 离开识别图
  31. Debug.Log(" Lost " + oldTrackedImage.name);
  32. VuforialControl.Instance.TrackingLost(int.Parse(oldTrackedImage.referenceImage.name));
  33. oldTrackedImage = null;
  34. }
  35. foreach (var trackedImage in imagerManager.trackables)
  36. {
  37. Debug.Log($"Image: {trackedImage.referenceImage.name} is at " +
  38. $"{trackedImage.transform.position}" + $" {trackedImage.trackingState.ToString()} ");
  39. if (!listARTrackedImage.Contains(trackedImage))
  40. listARTrackedImage.Add(trackedImage);
  41. if (trackedImage.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.Tracking)
  42. {
  43. Debug.Log(" This is " + trackedImage.referenceImage.name);
  44. text.text = " This is " + trackedImage.referenceImage.name;
  45. if (oldTrackedImage != trackedImage)
  46. {
  47. oldTrackedImage = trackedImage;
  48. VuforialControl.Instance.TrackingFound(oldTrackedImage.transform, int.Parse(oldTrackedImage.referenceImage.name), 0);
  49. }
  50. }
  51. }
  52. if (listARTrackedImage.Count < 1)
  53. {
  54. return;
  55. }
  56. }
  57. }