static MySceneBundle() { SceneBundleTransferOption = TransferSceneBundleOption.TRANSFER_ON_LAST_SCENE; currentSceneBundle = new SceneBundle(); nextSceneBundle = new SceneBundle(); SceneManager.sceneUnloaded += PrepareSceneBundleForNextSceneByTransferOptions; }
/// <summary> /// Display the button of a Scene Bundle /// </summary> private void DisplaySceneBundleButton(SceneBundle bundle) { GUIContent content = new GUIContent(bundle.name, bundle.Description); if (GUILayout.Button(content, GUILayout.Height(30f))) { EditorEnhancedSceneManager.OpenSceneBundle(bundle); } if (GUILayout.Button("A", GUILayout.Width(20f), GUILayout.Height(30f))) { Selection.activeObject = bundle; EditorGUIUtility.PingObject(bundle); } }
/// <summary> /// Return all scene assets in the target bundle, pass by reflection to limit scene assets array access /// </summary> /// <param name="targetBundle"></param> /// <returns></returns> internal static SceneAsset[] GetBundleScenesAssets(SceneBundle targetBundle) { if (targetBundle == null) { return(null); } SerializedObject tbSerialized = new SerializedObject(targetBundle); SerializedProperty scenesProp = tbSerialized.FindProperty("sceneAssets"); SceneAsset[] assets = new SceneAsset[scenesProp.arraySize]; for (int i = 0; i < assets.Length; i++) { assets[i] = scenesProp.GetArrayElementAtIndex(i).objectReferenceValue as SceneAsset; } return(assets); }
public static void Init() { SceneBundle scenebundle = new SceneBundle(); scenebundle.Start(); }
private static void PrepareSceneBundleForNextScene() { currentSceneBundle = nextSceneBundle; nextSceneBundle = new SceneBundle(); }
/// <summary> /// Load a new scene group and unload the current one by group reference, editor only /// </summary> /// <param name="groupName">The target group</param> public static void OpenSceneBundle(SceneBundle newBundle) { //This value will be incremented each persisant scene loaded, will be checked later to get missing persistant scenes var persistantScenesCheckCount = 0; //Register dirty scenes var dirtyScenes = new List <Scene>(); var saveFlag = false; SceneAsset[] persistantScenesAssets = SceneBundleEditor.GetBundleScenesAssets(CurrentSceneList.PersistantScenesBundle); bool hasPersistantBundle = persistantScenesAssets != null; for (int i = 0; i < EditorSceneManager.sceneCount; i++) { //Filter persistant scenes Scene scene = EditorSceneManager.GetSceneAt(i); //Check if opended scenes are dirty to call a save pannel if (scene.isDirty && !saveFlag) { saveFlag = true; } //Check if iterated scene is persisant var persisantFlag = false; if (hasPersistantBundle) { for (int j = 0; j < persistantScenesAssets.Length; j++) { if (scene.name == persistantScenesAssets[j].name) { persisantFlag = true; } } } if (!persisantFlag) { dirtyScenes.Add(scene); } else { //Doesn't add persistant scenes to dirty list persistantScenesCheckCount++; //Count persistant scenes to check if each one is correctly loaded } } //Asks the user to save the scenes if a dirty one is detected if (saveFlag) { EditorSceneManager.SaveModifiedScenesIfUserWantsTo(dirtyScenes.ToArray()); } //Check if all the scenes will be unload var fullLoad = EditorSceneManager.sceneCount <= dirtyScenes.Count; //Unload current scene except persistant ones, skip one scene if all scenes have to be reloaded for (int i = fullLoad ? 1 : 0; i < dirtyScenes.Count; i++) { EditorSceneManager.CloseScene(dirtyScenes[i], true); } //Load scenes in the bundle SceneAsset[] newBundleSceneAssets = SceneBundleEditor.GetBundleScenesAssets(newBundle); var scenesPaths = new string[newBundleSceneAssets.Length]; for (int i = 0; i < newBundleSceneAssets.Length; i++) { scenesPaths[i] = AssetDatabase.GetAssetPath(newBundleSceneAssets[i]); } //Threat first scene var firstSceneLoadMode = fullLoad ? OpenSceneMode.Single : OpenSceneMode.Additive; Scene active = EditorSceneManager.OpenScene(scenesPaths[0], firstSceneLoadMode); for (int i = 1; i < scenesPaths.Length; i++) { EditorSceneManager.OpenScene(scenesPaths[i], OpenSceneMode.Additive); } //Check if persistant scenes are correctly loaded if (hasPersistantBundle && persistantScenesCheckCount != persistantScenesAssets.Length) { //Re-open persistant scenes for (int i = 0; i < persistantScenesAssets.Length; i++) { var scenePath = AssetDatabase.GetAssetPath(persistantScenesAssets[i]); Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive); EditorSceneManager.MoveSceneBefore(scene, EditorSceneManager.GetSceneAt(i)); } } //Active the first loaded scene of the bundle EditorSceneManager.SetActiveScene(active); }