using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; namespace MoreMountains.Tools { /// /// Transform extensions /// public static class TransformExtensions { /// /// Destroys a transform's children /// /// public static void MMDestroyAllChildren(this Transform transform) { for (int t = transform.childCount - 1; t >= 0; t--) { if (Application.isPlaying) { UnityEngine.Object.Destroy(transform.GetChild(t).gameObject); } else { UnityEngine.Object.DestroyImmediate(transform.GetChild(t).gameObject); } } } /// /// Finds children by name, breadth first /// /// /// /// public static Transform MMFindDeepChildBreadthFirst(this Transform parent, string transformName) { Queue queue = new Queue(); queue.Enqueue(parent); while (queue.Count > 0) { Transform child = queue.Dequeue(); if (child.name == transformName) { return child; } foreach (Transform t in child) { queue.Enqueue(t); } } return null; } /// /// Finds children by name, depth first /// /// /// /// public static Transform MMFindDeepChildDepthFirst(this Transform parent, string transformName) { foreach (Transform child in parent) { if (child.name == transformName) { return child; } Transform result = child.MMFindDeepChildDepthFirst(transformName); if (result != null) { return result; } } return null; } /// /// Changes the layer of a transform and all its children to the new one /// /// /// public static void ChangeLayersRecursively(this Transform transform, string layerName) { transform.gameObject.layer = LayerMask.NameToLayer(layerName); foreach (Transform child in transform) { child.ChangeLayersRecursively(layerName); } } /// /// Changes the layer of a transform and all its children to the new one /// /// /// public static void ChangeLayersRecursively(this Transform transform, int layerIndex) { transform.gameObject.layer = layerIndex; foreach (Transform child in transform) { child.ChangeLayersRecursively(layerIndex); } } } }