using System.Collections; using System.Collections.Generic; using UnityEngine; using Terra.StudioPro.GameSetupV2; using System; using UnityEngine.SceneManagement; namespace Terra.CustomGame.Code { public class ICEYSceneManager : MonoBehaviour { public static SceneParserV2 sceneParser; public static ICEYSceneManager Instance; public GameObject LoadingScreen; [HideInInspector]public string ActiveSceneName; public string MAIN_SCENE; #region SCENE_ACTIONS public event Action OnGameSceneLoaded; public event Action OnGameReadyEvent; #endregion #region MONOBEHAVIOUR_METHODS void Awake() { sceneParser = FindObjectOfType(); if(LoadingScreen!=null) LoadingScreen.SetActive(true); if (sceneParser != null) { ActiveSceneName = ""; sceneParser.OnGameReadyEvent += OnGameReady; sceneParser.OnGameSceneLoaded += OnSceneLoaded; } if (Instance == null) Instance = this; } #endregion #region Scene Loading private void OnGameReady() { OnGameReadyEvent?.Invoke(); } #if TERRA_STUDIO private string currentLoadedScene = null; #endif private void OnSceneLoaded(string name, PhysicsScene physicsScene) { if (LoadingScreen != null) LoadingScreen.SetActive(false); ActiveSceneName = name; Debug.Log("AS: " + ActiveSceneName); OnGameSceneLoaded?.Invoke(name); } private void OnSceneUnloaded(string name, PhysicsScene physicsScene) { OnGameSceneLoaded?.Invoke(name); } public void LoadScene(string sceneName, bool showDefaultLoadingScreen) { if (LoadingScreen != null) LoadingScreen.SetActive(true); #if TERRA_STUDIO if (!string.IsNullOrEmpty(currentLoadedScene) && currentLoadedScene != "main_scene" && SceneManager.GetSceneByName(currentLoadedScene).isLoaded) { SceneManager.UnloadSceneAsync(currentLoadedScene); } var loadCall = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive); currentLoadedScene = sceneName; loadCall.completed += LoadCallOnCompleted; return; #endif sceneName = GameLoaderV2.GetCorrectSceneName(sceneName); GameLoaderV2.Instance.LoadGameScene(sceneName, showDefaultLoadingScreen); } private void LoadCallOnCompleted(AsyncOperation obj) { #if TERRA_STUDIO SceneManager.SetActiveScene(SceneManager.GetSceneByName(currentLoadedScene)); OnSceneLoaded(currentLoadedScene, SceneManager.GetSceneByName(currentLoadedScene).GetPhysicsScene()); #endif } public void UnloadScene(string sceneName) { #if TERRA_STUDIO if (sceneName == MAIN_SCENE) { Debug.LogError("can't unload main scene"); return; } SceneManager.UnloadSceneAsync(sceneName); return; #endif sceneName = GameLoaderV2.GetCorrectSceneName(sceneName); GameLoaderV2.Instance.UnloadGameScene(sceneName); } public bool IsSceneDownloaded(string sceneName) { #if TERRA_STUDIO return true; #endif sceneName = GameLoaderV2.GetCorrectSceneName(sceneName); var sceneBundleInfo = GameLoaderV2.Instance.GetSceneBundleInfo(sceneName); if (sceneBundleInfo == null) return false; return GameLoaderV2.Instance.AreResourcesAndSceneBundlesDownloaded(sceneBundleInfo); } public float GetSceneDownloadProgress(string sceneName) { #if TERRA_STUDIO return 1f; #endif sceneName = GameLoaderV2.GetCorrectSceneName(sceneName); return GameLoaderV2.Instance.GetSceneDownloadProgress(sceneName); } public PhysicsScene GetCurrentPhysicsScene() { #if TERRA_STUDIO return SceneManager.GetActiveScene().GetPhysicsScene(); #endif return sceneParser.GetCurrentScene().GetPhysicsScene(); } public Scene GetGameMainScene() { #if TERRA_STUDIO return SceneManager.GetSceneByName(MAIN_SCENE); #endif return sceneParser.GetMainScene(); } public Scene GetCurrentLoadedSubScene() { #if TERRA_STUDIO return SceneManager.GetActiveScene(); #endif return sceneParser.GetCurrentScene(); } public void MoveGameObjectToScene(GameObject obj, string sceneName) { #if TERRA_STUDIO var scene = SceneManager.GetSceneByName(sceneName); if (scene.isLoaded) SceneManager.MoveGameObjectToScene(obj, scene); return; #endif sceneName = GameLoaderV2.GetCorrectSceneName(sceneName); GameLoaderV2.Instance.MoveGameObjectToScene(obj, sceneName); } #endregion } }