namespace TPSBR { using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityScene = UnityEngine.SceneManagement.Scene; public static class SceneExtensions { // PUBLIC METHODS public static T GetComponent(this UnityScene scene, bool includeInactive = false) where T : class { List roots = ListPool.Shared.Get(16); scene.GetRootGameObjects(roots); T component = default; for (int i = 0, count = roots.Count; i < count; ++i) { component = roots[i].GetComponentInChildren(includeInactive); if (component != null) break; } ListPool.Shared.Return(roots); return component; } public static List GetComponents(this UnityScene scene, bool includeInactive = false) where T : class { List allComponents = new List(); List objectComponents = ListPool.Shared.Get(16); List sceneRootObjects = ListPool.Shared.Get(16); scene.GetRootGameObjects(sceneRootObjects); for (int i = 0, count = sceneRootObjects.Count; i < count; ++i) { sceneRootObjects[i].GetComponentsInChildren(includeInactive, objectComponents); allComponents.AddRange(objectComponents); objectComponents.Clear(); } ListPool.Shared.Return(sceneRootObjects); ListPool.Shared.Return(objectComponents); return allComponents; } public static void GetComponents(this UnityScene scene, List components, bool includeInactive = false) where T : class { List objectComponents = ListPool.Shared.Get(16); List sceneRootObjects = ListPool.Shared.Get(16); scene.GetRootGameObjects(sceneRootObjects); components.Clear(); for (int i = 0, count = sceneRootObjects.Count; i < count; ++i) { sceneRootObjects[i].GetComponentsInChildren(includeInactive, objectComponents); components.AddRange(objectComponents); objectComponents.Clear(); } ListPool.Shared.Return(sceneRootObjects); ListPool.Shared.Return(objectComponents); } public static void MoveRootGameObjects(this UnityScene scene, UnityScene targetScene) { List roots = ListPool.Shared.Get(16); scene.GetRootGameObjects(roots); for (int i = 0, count = roots.Count; i < count; ++i) { SceneManager.MoveGameObjectToScene(roots[i], targetScene); } ListPool.Shared.Return(roots); } public static GameObject CreateGameObject(this UnityScene scene, string name) { GameObject gameObject = new GameObject(name); if (gameObject.scene != scene) { SceneManager.MoveGameObjectToScene(gameObject, scene); } return gameObject; } public static GameObject Instantiate(this UnityScene scene, GameObject original) { GameObject gameObject = GameObject.Instantiate(original); if (gameObject.scene != scene) { SceneManager.MoveGameObjectToScene(gameObject, scene); } return gameObject; } public static T Instantiate(this UnityScene scene, T original) where T : Component { T component = GameObject.Instantiate(original); GameObject gameObject = component.gameObject; if (gameObject.scene != scene) { SceneManager.MoveGameObjectToScene(gameObject, scene); } return component; } public static Camera FindMainCamera(this UnityScene scene) { foreach (Camera camera in scene.GetComponents(true)) { if (camera.CompareTag("MainCamera") == true) return camera; } return null; } } }