Transforms.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FirstGearGames.Utilities.Objects
  5. {
  6. public static class Transforms
  7. {
  8. /// <summary>
  9. /// Destroys all children under the specified transform.
  10. /// </summary>
  11. /// <param name="t"></param>
  12. public static void DestroyChildren(this Transform t, bool destroyImmediately = false)
  13. {
  14. foreach (Transform child in t)
  15. {
  16. if (destroyImmediately)
  17. MonoBehaviour.DestroyImmediate(child.gameObject);
  18. else
  19. MonoBehaviour.Destroy(child.gameObject);
  20. }
  21. }
  22. /// <summary>
  23. /// Gets components in children and optionally parent.
  24. /// </summary>
  25. /// <typeparam name="T"></typeparam>
  26. /// <param name="results"></param>
  27. /// <param name="parent"></param>
  28. /// <param name="includeParent"></param>
  29. /// <param name="includeInactive"></param>
  30. public static void GetComponentsInChildren<T>(Transform parent, List<T> results, bool includeParent = true, bool includeInactive = false) where T : Component
  31. {
  32. if (!includeParent)
  33. {
  34. List<T> current = new List<T>();
  35. for (int i = 0; i < parent.childCount; i++)
  36. {
  37. parent.GetChild(i).GetComponentsInChildren(includeInactive, current);
  38. results.AddRange(current);
  39. }
  40. }
  41. else
  42. {
  43. parent.GetComponentsInChildren(includeInactive, results);
  44. }
  45. }
  46. /// <summary>
  47. /// Returns the position of this transform.
  48. /// </summary>
  49. public static Vector3 GetPosition(this Transform t, bool localSpace)
  50. {
  51. return (localSpace) ? t.localPosition : t.position;
  52. }
  53. /// <summary>
  54. /// Returns the rotation of this transform.
  55. /// </summary>
  56. public static Quaternion GetRotation(this Transform t, bool localSpace)
  57. {
  58. return (localSpace) ? t.localRotation : t.rotation;
  59. }
  60. /// <summary>
  61. /// Returns the scale of this transform.
  62. /// </summary>
  63. public static Vector3 GetScale(this Transform t)
  64. {
  65. return t.localScale;
  66. }
  67. /// <summary>
  68. /// Sets the position of this transform.
  69. /// </summary>
  70. /// <param name="t"></param>
  71. /// <param name="localSpace"></param>
  72. public static void SetPosition(this Transform t, bool localSpace, Vector3 pos)
  73. {
  74. if (localSpace)
  75. t.localPosition = pos;
  76. else
  77. t.position = pos;
  78. }
  79. /// <summary>
  80. /// Sets the position of this transform.
  81. /// </summary>
  82. /// <param name="t"></param>
  83. /// <param name="localSpace"></param>
  84. public static void SetRotation(this Transform t, bool localSpace, Quaternion rot)
  85. {
  86. if (localSpace)
  87. t.localRotation = rot;
  88. else
  89. t.rotation = rot;
  90. }
  91. /// <summary>
  92. /// Sets the position of this transform.
  93. /// </summary>
  94. /// <param name="t"></param>
  95. /// <param name="localSpace"></param>
  96. public static void SetScale(this Transform t, Vector3 scale)
  97. {
  98. t.localScale = scale;
  99. }
  100. }
  101. }