Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public static class TransformExtensions
  8. {
  9.     # region Cached
  10.     /*
  11.         Usage example:
  12.        
  13.         Rigidbody rb;
  14.         Rigidbody Rb => transform.CachedComponent(ref rb);
  15.      */
  16.     public static T CachedComponent<T>(this Transform t, ref T backingField)
  17.         where T : Component
  18.     {
  19.         if (backingField == null)
  20.             backingField = t.GetComponent<T>();
  21.         return backingField;
  22.     }
  23.  
  24.     public static T CachedComponentInChildren<T>(this Transform t, ref T backingField)
  25.         where T : Component
  26.     {
  27.         if (backingField == null)
  28.             backingField = t.GetComponentInChildren<T>();
  29.         return backingField;
  30.     }
  31.  
  32.     public static T CachedComponentOrInChildren<T>(this Transform t, ref T backingField)
  33.         where T : Component
  34.     {
  35.         if (backingField == null)
  36.             backingField = t.GetComponentOrInChildren<T>();
  37.         return backingField;
  38.     }
  39.  
  40.     public static T[] CachedComponentsInChildren<T>(this Transform t, ref T[] backingField)
  41.         where T : Component
  42.     {
  43.         if (backingField == null)
  44.             backingField = t.GetComponentsInChildren<T>();
  45.         return backingField;
  46.     }
  47.  
  48.     public static List<T> CachedComponentsInChildrenList<T>(this Transform t, ref List<T> backingField)
  49.         where T : Component
  50.     {
  51.         if (backingField == null)
  52.             backingField = t.GetComponentsInChildren<T>().ToList();
  53.         return backingField;
  54.     }
  55.  
  56.     public static T CachedComponentInParent<T>(this Transform t, ref T backingField)
  57.         where T : Component
  58.     {
  59.         if (backingField == null)
  60.             backingField = t.GetComponentInParent<T>();
  61.         return backingField;
  62.     }
  63.     # endregion
  64.  
  65.     public static T[] GetComponentsInChildrenOnly<T>(this Transform t, bool includeInactive = false)
  66.     {
  67.         List<T> result = t.GetComponentsInChildren<T>(includeInactive).ToList();
  68.         T parentComponent = t.GetComponent<T>();
  69.         if (!IsNull(parentComponent))
  70.             result.Remove(parentComponent);
  71.         return result.ToArray();
  72.     }
  73.  
  74.     public static T[] GetComponentsInFirstChildrenOnly<T>(this Transform t, bool includeInactive = false)
  75.     {
  76.         List<T> result = new List<T>();
  77.         foreach (Transform child in t)
  78.         {
  79.             T component;
  80.             if ((child.gameObject.activeInHierarchy || includeInactive) && (component = child.GetComponent<T>()) != null)
  81.                 result.Add(component);
  82.         }
  83.         return result.ToArray();
  84.     }
  85.  
  86.     private static bool IsNull<T>(T obj)
  87.     {
  88.         return EqualityComparer<T>.Default.Equals(obj, default(T));
  89.     }
  90.  
  91.     public static T GetComponentOrInParent<T>(this Transform t)
  92.     {
  93.         if (IsNull(t))
  94.             return default(T);
  95.         T component = t.GetComponent<T>();
  96.         if (!IsNull(component))
  97.             return component;
  98.         return GetComponentOrInParent<T>(t.parent);
  99.     }
  100.  
  101.     public static Transform GetTopLevelParent(this Transform t)
  102.     {
  103.         return t.root != t ? t.root : null;
  104.     }
  105.  
  106.     public static T GetComponentOrInChildren<T>(this Transform t)
  107.     {
  108.         T component = t.GetComponentInChildren<T>();
  109.         if (!IsNull(component))
  110.             return component;
  111.         return t.GetComponentInChildren<T>();
  112.     }
  113.  
  114.     public static Transform FindDeepChild(this Transform aParent, string aName)
  115.     {
  116.         var result = aParent.Find(aName);
  117.         if (result != null)
  118.             return result;
  119.         foreach (Transform child in aParent)
  120.         {
  121.             result = child.FindDeepChild(aName);
  122.             if (result != null)
  123.                 return result;
  124.         }
  125.         return null;
  126.     }
  127.  
  128.     public static void Reset(this Transform t, bool local = true)
  129.     {
  130.         if (local)
  131.         {
  132.             t.localPosition = Vector3.zero;
  133.             t.localRotation = Quaternion.identity;
  134.         }
  135.         else
  136.         {
  137.             t.position = Vector3.zero;
  138.             t.rotation = Quaternion.identity;
  139.         }
  140.         t.localScale = Vector3.one;
  141.     }
  142.  
  143.     public static Transform[] FindChildren(this Transform transform, string name)
  144.     {
  145.         return transform.GetComponentsInChildren<Transform>().Where(t => t.name == name).ToArray();
  146.     }
  147.  
  148.     public static void DestroyAllChildren(this Transform transform)
  149.     {
  150.         for (int i = transform.childCount - 1; i >= 0; i--)
  151.         {
  152. #if UNITY_EDITOR
  153.             if (EditorApplication.isPlaying)
  154.                 Object.Destroy(transform.GetChild(i).gameObject);
  155.             else
  156.                 Object.DestroyImmediate(transform.GetChild(i).gameObject);
  157. #else
  158.             Object.Destroy(transform.GetChild(i).gameObject);
  159. #endif
  160.         }
  161.     }
  162.  
  163.     public static Transform[] GetChildren(this Transform transform, bool includeInactive = false)
  164.     {
  165.         return transform.GetComponentsInChildrenOnly<Transform>(includeInactive);
  166.     }
  167.  
  168.     public static Transform[] GetFirstChildren(this Transform transform, bool includeInactive = false)
  169.     {
  170.         return transform.GetComponentsInFirstChildrenOnly<Transform>(includeInactive);
  171.     }
  172.  
  173.     public static RectTransform[] GetChildren(this RectTransform transform, bool includeInactive = false)
  174.     {
  175.         return transform.GetComponentsInChildrenOnly<RectTransform>(includeInactive);
  176.     }
  177.  
  178.     public static RectTransform[] GetFirstChildren(this RectTransform transform, bool includeInactive = false)
  179.     {
  180.         return transform.GetComponentsInFirstChildrenOnly<RectTransform>(includeInactive);
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement