ObjectQueries.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace O3DWB
  7. {
  8. public static class ObjectQueries
  9. {
  10. #region Public Static Functions
  11. public static bool IntersectsAnyObjectsInScene(OrientedBox orientedBox, bool ignoreFaceTouch)
  12. {
  13. return GetIntersectingObjects(orientedBox, ignoreFaceTouch).Count != 0;
  14. }
  15. public static bool IntersectsAnyObjectsInScene(OrientedBox orientedBox, List<GameObject> ignoreObjects, bool ignoreFaceTouch)
  16. {
  17. return GetIntersectingObjects(orientedBox, ignoreObjects, ignoreFaceTouch).Count != 0;
  18. }
  19. public static List<GameObject> GetIntersectingObjects(OrientedBox orientedBox, bool ignoreFaceTouch)
  20. {
  21. List<GameObject> intersectingObjects = Octave3DScene.Get().OverlapBox(orientedBox);
  22. if (ignoreFaceTouch) intersectingObjects.RemoveAll(item => orientedBox.AreAllBoxPointsOnOrInFrontOfAnyFacePlane(item.GetWorldOrientedBox()));
  23. return intersectingObjects;
  24. }
  25. public static List<GameObject> GetIntersectingObjects(OrientedBox orientedBox, List<GameObject> ignoreObjects, bool ignoreFaceTouch)
  26. {
  27. List<GameObject> intersectedObjects = GetIntersectingObjects(orientedBox, ignoreFaceTouch);
  28. intersectedObjects.RemoveAll(item => ignoreObjects.Contains(item));
  29. return intersectedObjects;
  30. }
  31. public static Dictionary<GameObject, GameObject> GetPrefabToObjectConnectionMappings(List<GameObject> gameObjects)
  32. {
  33. if (gameObjects.Count == 0) return new Dictionary<GameObject, GameObject>();
  34. var prefabMappings = new Dictionary<GameObject, GameObject>(gameObjects.Count);
  35. foreach(GameObject gameObject in gameObjects)
  36. {
  37. GameObject connectedPrefab = gameObject.GetSourcePrefab();
  38. if (connectedPrefab != null && !prefabMappings.ContainsKey(connectedPrefab)) prefabMappings.Add(connectedPrefab, gameObject);
  39. }
  40. return prefabMappings;
  41. }
  42. public static bool IsGameObjectEmpty(GameObject gameObject)
  43. {
  44. if (gameObject.HasMesh()) return false;
  45. if (gameObject.HasTerrain()) return false;
  46. if (gameObject.HasLight()) return false;
  47. if (gameObject.HasParticleSystem()) return false;
  48. if (gameObject.HasSpriteRenderer()) return false;
  49. return true;
  50. }
  51. public static bool IsGameObjectHierarchyEmpty(GameObject rootObject)
  52. {
  53. List<GameObject> allObjectsInHierarchy = rootObject.GetAllChildrenIncludingSelf();
  54. foreach(GameObject gameObject in allObjectsInHierarchy)
  55. {
  56. if (!ObjectQueries.IsGameObjectEmpty(gameObject)) return false;
  57. }
  58. return true;
  59. }
  60. public static bool CanGameObjectBePickedByCursor(GameObject gameObject)
  61. {
  62. if (gameObject == null) return false;
  63. if (IsGameObjectEmpty(gameObject)) return false;
  64. return Octave3DWorldBuilder.ActiveInstance.IsWorkingObject(gameObject);
  65. }
  66. public static bool CanGameObjectBeInteractedWith(GameObject gameObject)
  67. {
  68. if (gameObject == null) return false;
  69. if (Octave3DWorldBuilder.ActiveInstance.IsPivotWorkingObject(gameObject)) return false;
  70. return Octave3DWorldBuilder.ActiveInstance.IsWorkingObject(gameObject);
  71. }
  72. public static bool IsGameObjectPartOfPlacementGuideHierarchy(GameObject gameObject)
  73. {
  74. return ObjectPlacementGuide.ExistsInScene && (ObjectPlacementGuide.Equals(gameObject) || ObjectPlacementGuide.ContainsChild(gameObject.transform));
  75. }
  76. public static bool ObjectHasOnlyEmptyChildren(GameObject rootObject)
  77. {
  78. List<GameObject> allChildren = rootObject.GetAllChildren();
  79. foreach (GameObject gameObject in allChildren)
  80. {
  81. if (!ObjectQueries.IsGameObjectEmpty(gameObject)) return false;
  82. }
  83. return true;
  84. }
  85. #endregion
  86. }
  87. }
  88. #endif