/// <summary> /// Used during the build to check for unsaved scenes and provide a user popup if there are any. /// </summary> /// <returns>True if there were no unsaved scenes, or if user hits "Save and Continue" on popup. /// False if any scenes were unsaved, and user hits "Cancel" on popup.</returns> public static bool CheckModifiedScenesAndAskToSave() { var dirtyScenes = new List <Scene>(); for (int i = 0; i < SceneManager.sceneCount; ++i) { var scene = SceneManager.GetSceneAt(i); if (scene.isDirty) { dirtyScenes.Add(scene); } } if (dirtyScenes.Count > 0) { if (EditorUtility.DisplayDialog( "Unsaved Scenes", "Modified Scenes must be saved to continue.", "Save and Continue", "Cancel")) { EditorSceneManager.SaveScenes(dirtyScenes.ToArray()); } else { return(false); } } return(true); }
/// <summary> /// Calls Prepare() on any MonoBehaviour with IPrepare interface. If Prepare() returns true, parent scene will be marked dirty /// </summary> public static void RunPrepare() { if (OnPrepare != null) { OnPrepare(); } var toPrepare = MyExtensions.FindObjectsOfInterfaceAsComponents <IPrepare>(); HashSet <Scene> modifiedScenes = null; foreach (var prepare in toPrepare) { bool changed = prepare.Interface.Prepare(); if (changed && prepare.Component != null) { if (modifiedScenes == null) { modifiedScenes = new HashSet <Scene>(); } modifiedScenes.Add(prepare.Component.gameObject.scene); EditorUtility.SetDirty(prepare.Component); Debug.Log(prepare.Component.name + "." + prepare.Component.GetType().Name + ": Changed on Prepare", prepare.Component); } } if (modifiedScenes != null) { EditorSceneManager.SaveScenes(modifiedScenes.ToArray()); } }
static void SaveDirtyScenes( List <string> paths, bool askForUserConfirmation, out bool isCancelled) { isCancelled = false; List <Scene> scenesToSave = new List <Scene>(); foreach (Scene dirtyScene in GetDirtyScenes()) { if (Contains(paths, dirtyScene)) { scenesToSave.Add(dirtyScene); } } if (scenesToSave.Count == 0) { return; } if (askForUserConfirmation) { isCancelled = !EditorSceneManager. SaveModifiedScenesIfUserWantsTo( scenesToSave.ToArray()); return; } EditorSceneManager.SaveScenes( scenesToSave.ToArray()); }
private static void FixMissingGuidComponents() { foreach (var monoBehaviour in GetSavableMonoBehaviours()) { if (!monoBehaviour.GetComponent <GuidComponent>()) { monoBehaviour.gameObject.AddComponent <GuidComponent>(); } } AssetDatabase.SaveAssets(); EditorSceneManager.SaveScenes(GetOpenScenes()); }
public static void SaveDirtyScenes() { var scenes = new List <Scene>(); var sceneCount = EditorSceneManager.sceneCount; for (var i = 0; i < sceneCount; ++i) { var scene = EditorSceneManager.GetSceneAt(i); if (!scene.isDirty) { continue; } scenes.Add(scene); } EditorSceneManager.SaveScenes(scenes.ToArray()); }
public static bool SaveDirtyScenesWithPrompt(Scene[] scenesToSave) { var anySceneIsDirty = false; var scenesPaths = string.Empty; for (var i = 0; i < scenesToSave.Length; i++) { var scene = scenesToSave[i]; if (!scene.IsValid()) { continue; } if (!scene.isDirty) { continue; } if (IsSceneUntitled(scene)) { scenesPaths += "\nUntitled"; } else { scenesPaths += "\n" + scene.path; } anySceneIsDirty = true; } if (!anySceneIsDirty) { return(true); } var selection = EditorUtility.DisplayDialogComplex("Scene(s) Have Been Modified", "Do you want to save the changes you made in the scenes:" + scenesPaths + "\n\nYour changes will be lost if you don't save them.", "Save", "Don't Save", "Cancel"); if (selection == 0) { EditorSceneManager.SaveScenes(scenesToSave.ToArray()); } return(selection != 2); }
public static void RunPrepare() { BeforePrepared?.Invoke(); Preparing?.Invoke(); IEnumerable <ComponentOfInterface <IPrepare> > prepareComponents = TheGameObjectUtility .FindObjectsOfInterfaceAsComponents <IPrepare>(); HashSet <Scene> modifiedScenes = null; foreach (ComponentOfInterface <IPrepare> prepare in prepareComponents) { bool changed = prepare.Interface.Prepare(); Component prepareComponent = prepare.Component; if (!changed || !prepareComponent) { continue; } if (modifiedScenes == null) { modifiedScenes = new HashSet <Scene>(); } modifiedScenes.Add(prepareComponent.gameObject.scene); EditorUtility.SetDirty(prepareComponent); TheBugger.Log( $"{prepareComponent.name}.{prepareComponent.GetType().Name}: changed on prepare...", prepareComponent); } if (modifiedScenes != null) { EditorSceneManager.SaveScenes(modifiedScenes.ToArray()); } AfterPrepared?.Invoke(); }