EditorUtility.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Contains various utility methods used throughout the editor.
  8. /// </summary>
  9. public class EditorUtility
  10. {
  11. /// <summary>
  12. /// Calculates the axis aligned box that encapsulated the provided scene object and its components and children.
  13. /// Only certain components like <see cref="Renderable"/> will be included in bound calculations.
  14. /// </summary>
  15. /// <param name="so">Scene object to calculate the bounds for.</param>
  16. /// <returns>Axis aligned box encompassing all scene object elements, in world space.</returns>
  17. public static AABox CalculateBounds(SceneObject so)
  18. {
  19. AABox bounds;
  20. Internal_CalculateBounds(so, out bounds);
  21. return bounds;
  22. }
  23. /// <summary>
  24. /// Calculates the axis aligned box that encapsulated the provided scene objects and their components and children.
  25. /// Only certain components like <see cref="Renderable"/> will be included in bound calculations.
  26. /// </summary>
  27. /// <param name="objects">Scene objects to calculate the bounds for.</param>
  28. /// <returns>Axis aligned box encompassing all scene object elements, in world space.</returns>
  29. public static AABox CalculateBounds(SceneObject[] objects)
  30. {
  31. AABox bounds;
  32. Internal_CalculateBoundsArray(objects, out bounds);
  33. return bounds;
  34. }
  35. /// <summary>
  36. /// Converts a hierarchy of scene objects and their children into a flat array. Doesn't modify the scene object's
  37. /// themselves.
  38. /// </summary>
  39. /// <param name="so">Scene object whose hierarchy to flatten.</param>
  40. /// <returns>Array of flattened hierarchy in a depth-first manner.</returns>
  41. public static SceneObject[] FlattenHierarchy(SceneObject so)
  42. {
  43. Stack<SceneObject> todo = new Stack<SceneObject>();
  44. todo.Push(so);
  45. List<SceneObject> flattenedHierarchy = new List<SceneObject>();
  46. while (todo.Count > 0)
  47. {
  48. SceneObject cur = todo.Pop();
  49. flattenedHierarchy.Add(cur);
  50. int numChildren = cur.GetNumChildren();
  51. for (int i = 0; i < numChildren; i++)
  52. todo.Push(cur.GetChild(i));
  53. }
  54. return flattenedHierarchy.ToArray();
  55. }
  56. /// <summary>
  57. /// Find all resources that the provided resource depends on.
  58. /// </summary>
  59. /// <param name="resource">Resource whose dependencies to find.</param>
  60. /// <param name="recursive">Determines whether or not child objects will also be searched (if object has any
  61. /// children).</param>
  62. /// <returns>All resources the provided resource is dependant on (does not include itself).</returns>
  63. public static Resource[] FindDependencies(Resource resource, bool recursive = true)
  64. {
  65. return Internal_FindDependencies(resource, recursive);
  66. }
  67. [MethodImpl(MethodImplOptions.InternalCall)]
  68. private static extern void Internal_CalculateBounds(SceneObject so, out AABox bounds);
  69. [MethodImpl(MethodImplOptions.InternalCall)]
  70. private static extern void Internal_CalculateBoundsArray(SceneObject[] objects, out AABox bounds);
  71. [MethodImpl(MethodImplOptions.InternalCall)]
  72. private static extern Resource[] Internal_FindDependencies(Resource resource, bool recursive);
  73. }
  74. }