TrackablePlaneGenerator.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using System.Linq;
  8. public class TrackablePlaneGenerator : MonoBehaviour
  9. {
  10. public GameObject trackablePlanePrefab;
  11. private List<PlaneTrackable> newPlanes = new List<PlaneTrackable>();
  12. private List<PlaneTrackable> allPlanes = new List<PlaneTrackable>();
  13. public Text text;
  14. private List<TrackablePlaneMono> trackableCache = new List<TrackablePlaneMono>();
  15. #if UNITY_EDITOR
  16. public static bool twoPlane = false;
  17. #endif
  18. public void Update()
  19. {
  20. #if UNITY_EDITOR
  21. if (Input.GetKeyDown(KeyCode.O))
  22. {
  23. twoPlane = false;
  24. }
  25. if (Input.GetKeyDown(KeyCode.T))
  26. {
  27. twoPlane = true;
  28. }
  29. #endif
  30. //if (!GSXRManager.Instance.Initialized)
  31. //{
  32. // return;
  33. //}
  34. text.text = API_GSXR_Slam.GSXR_Get_PanelNum().ToString();
  35. TrackableManager.Instance.GetTrackables<PlaneTrackable>(newPlanes, TrackableQueryFilter.New);
  36. for (int i = 0; i < newPlanes.Count; i++)
  37. {
  38. GameObject trackablePlaneGameObject = Instantiate(trackablePlanePrefab);
  39. trackablePlaneGameObject.GetComponent<TrackablePlaneMono>().Init(newPlanes[i]);
  40. trackableCache.Add(trackablePlaneGameObject.GetComponent<TrackablePlaneMono>());
  41. }
  42. TrackableManager.Instance.GetTrackables<PlaneTrackable>(allPlanes, TrackableQueryFilter.All);
  43. List<TrackablePlaneMono> tobeRemovePlane = trackableCache.Where((item) =>
  44. {
  45. PlaneTrackable plane = allPlanes.Where((planeItem) =>
  46. {
  47. return planeItem.trackableId == item.GetPlaneId();
  48. }).FirstOrDefault();
  49. return plane == null;
  50. }).ToList();
  51. for (int i = 0; i < tobeRemovePlane.Count; i++)
  52. {
  53. trackableCache.Remove(tobeRemovePlane[i]);
  54. GameObject.Destroy(tobeRemovePlane[i].gameObject);
  55. }
  56. }
  57. }