// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; namespace FlaxEngine { partial class Level { /// /// Unloads all active scenes and loads the given scene (in the background). /// /// The scene asset identifier (scene to load). /// True if action fails (given asset is not a scene asset, missing data, scene loading error), otherwise false. public static bool ChangeSceneAsync(Guid sceneAssetId) { UnloadAllScenesAsync(); return LoadSceneAsync(sceneAssetId); } /// /// Unloads all active scenes and loads the given scene (in the background). /// /// The asset with the scene to load. /// True if action fails (given asset is not a scene asset, missing data, scene loading error), otherwise false. public static bool ChangeSceneAsync(SceneReference sceneAsset) { return ChangeSceneAsync(sceneAsset.ID); } /// /// Loads scene from the asset. /// /// The asset with the scene to load. /// True if action fails (given asset is not a scene asset, missing data, scene loading error), otherwise false. public static bool LoadScene(SceneReference sceneAsset) { return LoadScene(sceneAsset.ID); } /// /// Loads scene from the asset. Done in the background. /// /// The asset with the scene to load. /// True if failed (given asset is not a scene asset, missing data), otherwise false. public static bool LoadSceneAsync(SceneReference sceneAsset) { return LoadSceneAsync(sceneAsset.ID); } /// /// Tries to find script of the given type in all loaded scenes. /// /// Type of the object. /// Found script or null. public static T FindScript() where T : Script { return FindScript(typeof(T)) as T; } /// /// Tries to find actor of the given type in all loaded scenes. /// /// Type of the object. /// Found actor or null. public static T FindActor() where T : Actor { return FindActor(typeof(T)) as T; } /// /// Tries to find actor of the given type and name in all loaded scenes. /// /// Name of the object. /// Type of the object. /// Found actor or null. public static T FindActor(string name) where T : Actor { return FindActor(typeof(T), name) as T; } /// /// Tries to find actor of the given type and tag in a root actor or all loaded scenes. /// /// A tag on the object. /// The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes. /// Type of the object. /// Found actor or null. public static T FindActor(Tag tag, Actor root = null) where T : Actor { return FindActor(typeof(T), tag, root) as T; } /// /// Tries to find actor with the given ID in all loaded scenes. It's very fast O(1) lookup. /// /// Type of the object. /// The id. /// Found actor or null. public static T FindActor(ref Guid id) where T : Actor { return FindActor(id) as T; } /// /// Finds all the scripts of the given type in all the loaded scenes. /// /// Type of the object. /// Found scripts list. public static T[] GetScripts() where T : Script { var scripts = GetScripts(typeof(T)); if (scripts.Length == 0) return Array.Empty(); var result = new T[scripts.Length]; for (int i = 0; i < scripts.Length; i++) result[i] = scripts[i] as T; return result; } /// /// Finds all the actors of the given type in all the loaded scenes. /// /// Type of the object. /// Finds only active actors. /// Found actors list. public static T[] GetActors(bool activeOnly = false) where T : Actor { var actors = GetActors(typeof(T), activeOnly); if (actors.Length == 0) return Array.Empty(); var result = new T[actors.Length]; for (int i = 0; i < actors.Length; i++) result[i] = actors[i] as T; return result; } } }