namespace Dreamteck { using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public static class SceneUtility { public static List childrenList = new List(); public static void GetChildrenRecursively(Transform current) { childrenList.Clear(); GetChildrenRecursivelyInternal(current); } private static void GetChildrenRecursivelyInternal(Transform current) { childrenList.Add(current); int childCount = current.childCount; for (int i = 0; i < childCount; i++) { GetChildrenRecursivelyInternal(current.GetChild(i)); } } public static T[] GetComponentsInChildrenRecusrively(this GameObject gameObject) where T : Component { GetChildrenRecursively(gameObject.transform); List components = new List(); for (int i = 0; i < childrenList.Count; i++) { T component = childrenList[i].GetComponent(); if(component != null) { components.Add(component); } } return components.ToArray(); } public static void GetChildrenRecursively(Transform current, ref List transformList) { transformList.Add(current); foreach (Transform child in current) GetChildrenRecursively(child, ref transformList); } public static T GetComponentInScene(this Scene scene, string objectName = null) where T : Component { var component = default(T); var rootObjects = scene.GetRootGameObjects(); foreach (var obj in rootObjects) { if (objectName != null && obj.name != objectName) continue; component = obj.GetComponentInChildren(); if(component != null) { break; } } return component; } public static T[] GetComponentsInScene(this Scene scene, string objectName = null, bool includeInactive = false) { var rootObjects = scene.GetRootGameObjects(); var components = new List(); foreach (var obj in rootObjects) { var rootComponent = obj.GetComponent(); if (rootComponent != null && (objectName == null || (objectName != null && obj.gameObject.name == obj.name))) { components.Add(rootComponent); } var foundComponents = obj.GetComponentsInChildren(includeInactive); for (int i = 0; i < foundComponents.Length; i++) { var component = foundComponents[i] as Component; if (objectName != null && component != null && component.gameObject.name != objectName) continue; components.Add(foundComponents[i]); } } return components.ToArray(); } } }