// Copyright (c) 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 Internal_LoadSceneAsync(ref 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 Internal_LoadScene(ref 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 Internal_LoadSceneAsync(ref sceneAsset.ID); } /// /// Begins preloading scene in the background. Intended to use by scene streaming systems that want to prepare scene but not add to the level yet. /// Loads scene asset, deserializes actors and scripts, setup objects hierarchy with transformations and initializes them (incl. calling OnAwake for scripts). /// Preloaded scene is not added to the loaded scenes and not active in the game (no BeginPlay/OnEnable/OnStart called yet). Use or to finish loading preloaded scene and add it to the level. /// Preloaded scene can be normally unloaded via to release resources if it's not needed anymore. /// /// The asset with the scene to preload. /// True if preloading cannot be done, otherwise false. public static bool PreloadSceneAsync(SceneReference sceneAsset) { return Internal_PreloadSceneAsync(ref sceneAsset.ID); } /// /// Unloads given scene. /// /// The asset with the scene to unload. /// True if action cannot be done, otherwise false. public static bool UnloadScene(SceneReference sceneAsset) { return Internal_UnloadScene(ref 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 an actor or all the loaded scenes. /// /// Type of the object. /// The root to find scripts. If null, will search in all scenes /// Found scripts list. public static T[] GetScripts(Actor root = null) where T : Script { var scripts = GetScripts(typeof(T), root); if (typeof(T) == typeof(Script)) return (T[])scripts; 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 (typeof(T) == typeof(Actor)) return (T[])actors; 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; } } }