using System.Collections.Generic; using UnityEditor.SceneManagement; using UnityEditor.Timeline; using UnityEngine; using UnityEngine.Sequences; using UnityEngine.Sequences.Timeline; using UnityEngine.Timeline; namespace UnityEditor.Sequences { /// /// An interface for manipulating Scenes in the context of . /// internal class SceneManagement { /// /// Creates a new empty Scene and links it with a in the given Sequence. /// /// The Sequence you want the new Scene to belong to. /// True if the operation has succeeded. False if the Editor could not save the new Scene in the project. internal static bool AddNewScene(TimelineAsset timeline) { var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive); if (EditorSceneManager.SaveScene(scene)) { var track = timeline.CreateTrack(); track.scene = new SceneReference() {path = scene.path}; track.name = scene.name; var activationClip = track.CreateClip(); activationClip.displayName = "Active"; activationClip.duration = timeline.duration; EditorUtility.SetDirty(timeline); AssetDatabase.SaveAssetIfDirty(timeline); TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved); return true; } EditorSceneManager.CloseScene(scene, true); return false; } /// /// Opens the Scene located at the specified path in . /// /// Path to the Scene, relative to the project folder. /// Set to true to disable the root GameObjects of the loaded Scenes. Default value: false. internal static void OpenScene(string path, bool deactivate = false) { var scene = EditorSceneManager.GetSceneByPath(path); if (!scene.isLoaded) scene = EditorSceneManager.OpenScene(path, OpenSceneMode.Additive); if (deactivate) { List rootObjects = new List(); scene.GetRootGameObjects(rootObjects); foreach (GameObject root in rootObjects) root.SetActive(false); } } /// /// Closes the Scene located at the specified path. /// /// Path to the Scene, relative to the project folder. internal static void CloseScene(string path) { if (IsLoaded(path)) EditorSceneManager.CloseScene(EditorSceneManager.GetSceneByPath(path), true); } /// /// Opens all Scenes referenced through a in the specified . /// /// The Sequence you want to open all related activatable Scenes from. /// Set to true to disable the root GameObjects of the loaded Scenes. Default value: false. internal static void OpenAllScenes(SequenceNode sequence, bool deactivate = false) { foreach (var path in sequence.GetRelatedScenes()) OpenScene(path, deactivate); } /// /// Indicates if the Scene located at the specified path is already loaded or not in the Hierarchy. /// /// Path to the Scene, relative to the project folder. /// True if the Scene located at is already loaded in the Hierarchy. False otherwise. internal static bool IsLoaded(string path) { var scene = EditorSceneManager.GetSceneByPath(path); return scene.isLoaded; } } }