using UnityEngine; using System.Collections.Generic; namespace FIMSpace { /// /// FM: Class with methods which can be helpful when using unity Transforms /// public static class FTransformMethods { /// /// Method which is searching in depth of choosed transform for other transform with choosed name /// public static Transform FindChildByNameInDepth(string name, Transform transform, bool findInDeactivated = true, string[] additionalContains = null) { /* If choosed transform is already one we are searching for */ if (transform.name == name) { return transform; } /* Searching every transform component inside choosed transform */ foreach (Transform child in transform.GetComponentsInChildren(findInDeactivated)) { if (child.name.ToLower().Contains(name.ToLower())) { bool allow = false; if (additionalContains == null || additionalContains.Length == 0) allow = true; else for (int i = 0; i < additionalContains.Length; i++) if (child.name.ToLower().Contains(additionalContains[i].ToLower())) { allow = true; break; } if (allow) return child; } } return null; } /// /// Method which finds all components of given type in all children in choosed transform /// public static List FindComponentsInAllChildren(Transform transformToSearchIn, bool includeInactive = false, bool tryGetMultipleOutOfSingleObject = false) where T : Component { List components = new List(); foreach (T child in transformToSearchIn.GetComponents()) { if (child) components.Add(child); } foreach (Transform child in transformToSearchIn.GetComponentsInChildren(includeInactive)) { if (tryGetMultipleOutOfSingleObject == false) { T component = child.GetComponent(); if (component) if (components.Contains(component) == false) components.Add(component); } else { foreach (T component in child.GetComponents()) { if (component) if (components.Contains(component) == false) components.Add(component); } } } return components; } /// /// Method which finds component of given type in all children in choosed transform /// public static T FindComponentInAllChildren(Transform transformToSearchIn) where T : Component { foreach (Transform childInDepth in transformToSearchIn.GetComponentsInChildren()) { T component = childInDepth.GetComponent(); if (component) return component; } return null; } /// /// Method which finds component of given type in all parents in choosed transform /// public static T FindComponentInAllParents(Transform transformToSearchIn) where T : Component { Transform p = transformToSearchIn.parent; for (int i = 0; i < 100 /* safe limit */; i++) { T component = p.GetComponent(); if (component) return component; p = p.parent; if (p == null) return null; } return null; } /// /// Changing activation for all children in give transform /// public static void ChangeActiveChildrenInside(Transform parentOfThem, bool active) { for (int i = 0; i < parentOfThem.childCount; i++) { parentOfThem.GetChild(i).gameObject.SetActive(active); } } /// /// Making parents active from one transform until reach choosen transform or null /// public static void ChangeActiveThroughParentTo(Transform start, Transform end, bool active, bool changeParentsChildrenActivation = false) { start.gameObject.SetActive(active); Transform p = start.parent; for (int i = 0; i < 100 /* safe limit */; i++) { if (p == end) return; if (p == null) return; if (changeParentsChildrenActivation) ChangeActiveChildrenInside(p, active); p = p.parent; } } } }