Esempio n. 1
0
        /// <summary>
        /// Unloads any scene from a specific config ( if they are loaded )
        /// </summary>
        /// <param name="thisConfig"></param>
        private void UnloadConfigInternal(SceneConfig thisConfig)
        {
            try
            {
                foreach (var thisScene in thisConfig.sceneList)
                {
                    var scene = SceneManager.GetSceneByName(thisScene.SceneName);
                    if (scene != null)
                    {
                        if (Application.isPlaying)
                        {
                            if (scene.isLoaded)
                            {
                                Debug.Log("Unload scene async: " + scene.name);

                                SceneManager.UnloadSceneAsync(scene);
                            }
                        }
#if UNITY_EDITOR
                        else
                        {
                            Debug.Log("Editor: close scene : " + scene.name);

                            EditorSceneManager.CloseScene(scene, true);
                        }
#endif
                    }
                }
            }
            finally
            {
            }
        }
        /// <summary>
        /// Retrieves the current index of the given config in our master list
        /// </summary>
        /// <param name="thisEntry">the entry that you are searching for</param>
        /// <returns>the current index in the master multiscene config list</returns>
        private int GetConfigIndex(SceneConfig thisEntry)
        {
            var index = -1;

            if (sceneConfig == null)
            {
                return(index);
            }

            if (thisEntry == null)
            {
                return(index);
            }

            if (!sceneConfig.config.Contains(thisEntry))
            {
                return(index);
            }

            for (var i = 0; i < sceneConfig.config.Count; i++)
            {
                if (sceneConfig.config[i] == thisEntry)
                {
                    index = i;
                }
            }

            return(index);
        }
        /// <summary>
        /// Unloads any scene from a specific config ( if they are loaded )
        /// </summary>
        /// <param name="thisConfig"></param>
        public void UnloadConfig(SceneConfig thisConfig)
        {
            var loadedSceneCount = SceneManager.sceneCount;

            for (var i = 0; i < loadedSceneCount; i++)
            {
                var loadedScene = SceneManager.GetSceneAt(i);
                foreach (var scene in thisConfig.sceneList)
                {
                    if (loadedScene.name == scene.SceneName)
                    {
                        if (Application.isPlaying)
                        {
                            if (IsScene_CurrentlyLoaded(loadedScene.name))
                            {
                                if (loadedScene.isLoaded)
                                {
                                    //  Debug.Log("Unload scene: " + loadedScene.name);
                                    SceneManager.UnloadSceneAsync(loadedScene);
                                }
                            }
                        }
#if UNITY_EDITOR
                        else
                        {
                            EditorSceneManager.CloseScene(loadedScene, true);
                        }
#endif
                    }
                }
            }
        }
 /// <summary>
 /// Load a specific scene config, optionally unloads existing first (ie can be optionally additively loaded)
 ///
 /// If a particular scene is already loaded then ignores it.
 /// </summary>
 /// <param name="config"></param>
 /// <param name="unloadExisting"></param>
 public void LoadSceneConfig(SceneConfig config, bool unloadExisting = true)
 {
     for (int i = 0; i < config.sceneList.Count; i++)
     {
         if (i == 0)
         {
             // if we need to unload existing, then load the first in single mode, otherwise everything is additive
             LoadScene(config.sceneList[i], !unloadExisting);
         }
         else
         {
             // and the rest additive
             LoadScene(config.sceneList[i], true);
         }
     }
 }
        /// <summary>
        /// List sorting functionality
        /// </summary>
        /// <param name="thisEntry">the entry you want to sort</param>
        /// <param name="sort">the sorting type</param>
        private void ReorderListEntry(SceneConfig thisEntry, ListSort sort)
        {
            if (thisEntry == null)
            {
                return;
            }

            // get our current index
            var index = GetConfigIndex(thisEntry);

            // remove the old entry
            sceneConfig.config.RemoveAt(index);
            switch (sort)
            {
            case ListSort.MovetoTop:
            {
                // insert at the top
                sceneConfig.config.Insert(0, thisEntry);
                break;
            }

            case ListSort.MoveToBottom:
            {
                // add it to the end
                sceneConfig.config.Add(thisEntry);
                break;
            }

            case ListSort.MoveUp:
            {
                var newIndex = (index - 1);
                sceneConfig.config.Insert(newIndex, thisEntry);
                break;
            }

            case ListSort.MoveDown:
            {
                var newIndex = (index + 1);
                sceneConfig.config.Insert(newIndex, thisEntry);
                break;
            }
            }
        }
        /// <summary>
        /// Draw our inspector
        /// </summary>
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            sceneConfig = (MultiSceneLoader)target;

            EditorGUILayout.Space();
            GUILayout.Label(Loc.WindowTitle, EditorStyles.boldLabel);

            GUILayout.Label(Loc.TopDesc, EditorStyles.helpBox);

            GUILayout.Space(15f);

            topToggle = EditorGUILayout.Foldout(topToggle, Loc.ConfigList);
            if (topToggle)
            {
                topScroll = GUILayout.BeginScrollView(topScroll, false, true);
                {
                    GUILayout.BeginVertical();
                    {
                        GUILayout.BeginHorizontal();
                        {
                            GUILayout.Space(15f);

                            GUILayout.BeginVertical();
                            {
                                // Render our config editors
                                for (var j = 0; j < sceneConfig.config.Count; j++)
                                {
                                    var entry = sceneConfig.config[j];
                                    if (foldoutState.ContainsKey(entry))
                                    {
                                        foldoutState[entry] = EditorGUILayout.Foldout(foldoutState[entry], entry.name);
                                    }
                                    else
                                    {
                                        foldoutState.Add(entry, false);
                                        foldoutState[entry] = EditorGUILayout.Foldout(foldoutState[entry], entry.name);
                                    }

                                    if (foldoutState[entry])
                                    {
                                        GUILayout.BeginHorizontal();
                                        {
                                            GUILayout.Space(10f);
                                            GUILayout.BeginVertical();
                                            {
                                                GUILayout.BeginHorizontal();
                                                {
                                                    GUILayout.Label(Loc.ConfigName, GUILayout.Width(120f));
                                                    entry.name = GUILayout.TextField(entry.name);
                                                }
                                                GUILayout.EndHorizontal();
                                                // scene list
                                                for (var i = 0; i < entry.sceneList.Count; i++)
                                                {
                                                    GUILayout.BeginHorizontal();
                                                    {
                                                        entry.sceneList[i] = EditorGUILayout.ObjectField(Loc.SceneName, entry.sceneList[i], typeof(Object), false);
                                                        if (GUILayout.Button("-", GUILayout.Width(35f)))
                                                        {
                                                            entry.sceneList.Remove(entry.sceneList[i]);
                                                        }
                                                    }
                                                    GUILayout.EndHorizontal();
                                                }

                                                if (GUILayout.Button(Loc.AddNewScene))
                                                {
                                                    entry.sceneList.Add(new Object());
                                                }
                                                GUILayout.Space(15f);
                                                if (sceneConfig.config.Count > 1)
                                                {
                                                    GUILayout.Label(Loc.MoveConfig, EditorStyles.helpBox);
                                                    GUILayout.BeginHorizontal();
                                                    {
                                                        if (GUILayout.Button(Loc.MoveTop))
                                                        {
                                                            ReorderListEntry(entry, ListSort.MovetoTop);
                                                        }
                                                        if (GetConfigIndex(entry) != 0)
                                                        {
                                                            if (GUILayout.Button(Loc.MoveUp))
                                                            {
                                                                ReorderListEntry(entry, ListSort.MoveUp);
                                                            }
                                                        }
                                                        if (GetConfigIndex(entry) != sceneConfig.config.Count - 1)
                                                        {
                                                            if (GUILayout.Button(Loc.MoveDown))
                                                            {
                                                                ReorderListEntry(entry, ListSort.MoveDown);
                                                            }
                                                        }
                                                        if (GUILayout.Button(Loc.MoveBottom))
                                                        {
                                                            ReorderListEntry(entry, ListSort.MoveToBottom);
                                                        }
                                                    }
                                                    GUILayout.EndHorizontal();
                                                }
                                                if (GUILayout.Button(Loc.RemoveConfig))
                                                {
                                                    sceneConfig.config.Remove(entry);
                                                    foldoutState.Remove(entry);
                                                }
                                                GUILayout.Space(15f);
                                            }
                                            GUILayout.EndVertical();
                                        }
                                        GUILayout.EndHorizontal();
                                    }
                                }
                            }
                            GUILayout.EndVertical();
                        }
                        GUILayout.EndHorizontal();
                    }
                    GUILayout.EndVertical();

                    if (GUILayout.Button(Loc.AddNewConfig))
                    {
                        var newConfig = new SceneConfig()
                        {
                            name = Loc.NewConfigName
                        };
                        sceneConfig.config.Add(newConfig);
                    }
                }
                GUILayout.Space(5f);
                GUILayout.EndScrollView();
            }

            EditorGUILayout.Space(10f);

            botToggle = EditorGUILayout.Foldout(botToggle, Loc.SceneLoading);
            if (botToggle)
            {
                botScroll = GUILayout.BeginScrollView(botScroll, false, true);
                {
                    EditorGUILayout.Space(5);
                    GUILayout.Label(Loc.LoadAllScenes, EditorStyles.boldLabel);
                    EditorGUILayout.Space(5);
                    if (GUILayout.Button(Loc.LoadAllScenes, GUILayout.MinHeight(100), GUILayout.Height(35)))
                    {
                        sceneConfig.LoadAllScenes();
                    }
                    GUILayout.Label(Loc.LoadAllScenesDesc, EditorStyles.helpBox);

                    EditorGUILayout.Space(5);
                    GUILayout.Label(Loc.LoadSubScenes, EditorStyles.boldLabel);
                    GUILayout.Label(Loc.LoadSubScenesDesc, EditorStyles.helpBox);

                    foreach (var entry in sceneConfig.config)
                    {
                        EditorGUILayout.Space(5);
                        var buttonText = string.Format(Loc.LoadXScenes, entry.name);
                        if (GUILayout.Button(buttonText, GUILayout.MinHeight(100), GUILayout.Height(35)))
                        {
                            sceneConfig.LoadSceneConfig(entry, true);
                        }
                        GUILayout.Label(Loc.LoadOnlyScenes + entry.name + ".", EditorStyles.helpBox);
                    }
                }
                GUILayout.EndScrollView();
                GUILayout.Space(5f);
            }

            EditorGUILayout.Space();

            if (GUI.changed)
            {
                needToSave = true;
            }

            if (GUILayout.Button("Save Changes", GUILayout.Height(35f)))
            {
                if (needToSave)
                {
                    SaveChanges(serializedObject);
                }
            }
        }
Esempio n. 7
0
        private void LoadSceneConfig(SceneConfig config, bool unloadExisting, bool useAsyncLoading, Action <string> callback = null)
        {
            // is this config already in our cache? ie are the scenes loaded already?
            if (configCache.Contains(config.name))
            {
                // Debug.Log("LoadSceneConfig() - " + config.name + " is already in our cache, ignoring call");
                return;
            }
            else
            {
                Debug.Log("Starting scene load for config: " + config.name);
                configCache.Add(config.name);
            }

            for (int i = 0; i < config.sceneList.Count; i++)
            {
                var sceneName = config.sceneList[i].SceneName;
                Debug.Log("Loading scene from config : " + sceneName);
                if (Application.isPlaying)
                {
                    if (SceneManager.GetSceneByName(sceneName) == null)
                    {
                        Debug.LogError("Scene: " + sceneName + " doesn't exist in build settings");
                    }

                    if (!IsScene_CurrentlyLoaded(sceneName))
                    {
                        if (i == 0)
                        {
                            if (unloadExisting)
                            {
                                // if we need to unload existing, then load the first in single mode, otherwise everything is additive
                                LoadNewScene(sceneName, useAsyncLoading, innercallback =>
                                {
                                    callback?.Invoke(sceneName);

                                    if (IsScene_CurrentlyLoaded(sceneName))
                                    {
                                        // if so, then do light magic
                                        LightProbes.Tetrahedralize();
                                    }
                                });
                            }
                            else
                            {
                                // and the rest additive
                                LoadSceneAdditive(sceneName, useAsyncLoading, innercallback =>
                                {
                                    callback?.Invoke(sceneName);

                                    if (IsScene_CurrentlyLoaded(sceneName))
                                    {
                                        // if so, then do light magic
                                        LightProbes.TetrahedralizeAsync();
                                    }
                                });
                            }
                        }
                        else
                        {
                            // and the rest additive
                            LoadSceneAdditive(sceneName, useAsyncLoading, innercallback =>
                            {
                                callback?.Invoke(sceneName);

                                if (IsScene_CurrentlyLoaded(sceneName))
                                {
                                    // if so, then do light magic
                                    LightProbes.TetrahedralizeAsync();
                                }
                            });
                        }
                    }
                }
                else
                {
#if UNITY_EDITOR
                    // if it's not already loaded
                    if (!IsScene_CurrentlyLoaded_inEditor(sceneName))
                    {
                        Debug.Log("Editor: loading scene: " + sceneName);
                        // load the scene
                        EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(config.sceneList[i].Scene), OpenSceneMode.Additive);

                        // now is it loaded?
                        if (IsScene_CurrentlyLoaded_inEditor(sceneName))
                        {
                            // if so, then do light magic
                            LightProbes.Tetrahedralize();
                        }
                    }
#endif
                }
            }

            Debug.Log("Scene load for config: " + config.name + " COMPLETE");
        }
Esempio n. 8
0
        public SceneConfig config;              // the config this belongs to

        public SceneConfigEntry(SceneConfig thisConfig)
        {
            config = thisConfig;
        }