/// <summary> /// Adds the currently open scene to the TextScene buildsettings, and updates /// the standard Unity buildsettings with the corresponding temp binary scene file. /// User will be notified if the current scene is not saved or is not a temp binary /// file. /// </summary> public static void AddCurrentSceneToBuild() { string currentScene = EditorApplication.currentScene; if (currentScene.Length == 0) { EditorUtility.DisplayDialog("Unsaved", "The currently open scene is not saved. Please save it using the TextScene menu option and try again.", "OK"); return; } if (currentScene.StartsWith("Assets/")) { EditorUtility.DisplayDialog("Invalid scene", "The currently open scene is not a TextScene. Please re-save using the TextScene menu options and try again", "OK"); return; } //Debug.Log("Text scene file to save in build settings: " + textSceneFile); TextScene.AddSceneToBuild(TextScene.TempToTextSceneFile(EditorApplication.currentScene)); }
public static void SaveAs() { string currentScene = EditorApplication.currentScene; string startFolder = ""; if (currentScene.Length == 0) { SaveCurrent(); return; } else if (currentScene.StartsWith("Assets/")) { string needResaveAs = currentScene.Substring(currentScene.IndexOf('/')); needResaveAs = needResaveAs.Replace(".unity", ".txt"); needResaveAs = Application.dataPath + needResaveAs; startFolder = needResaveAs.Substring(0, needResaveAs.LastIndexOf('/')); } else { //TODO: Verify that it starts with TempScenes? startFolder = EditorHelper.GetProjectFolder() + TextScene.TempToTextSceneFile(currentScene); startFolder = startFolder.Substring(0, startFolder.LastIndexOf('/')); } string fileName = EditorUtility.SaveFilePanel("Save TextScene as", startFolder, "textscene", "txt"); if (fileName.Length > 0) { Save(fileName); } }
public Status Update() { if (saveAndReload.Length == 0) { Debug.LogError("Invalid saveandreload name! Cancelling load/save process..."); return(Status.Failed); } if (saveAndReloadTimer > 0) { EditorUtility.DisplayProgressBar("Creating temp...", "Creating binary temp file for TextScene: " + state.ToString(), 1.0f - saveAndReloadTimer / SAVE_AND_RELOAD_FRAMES); saveAndReloadTimer--; return(Status.Working); } else { if (state == State.SaveTemp) { Debug.Log("SaveAndReload: " + saveAndReload); ///FIXME: Unity sometimes puts a lock on the scenes we try to save, this is a CRUEL way to ///get around it. /// ///Repro-steps: *Comment out the try/catch /// *Clean out tempscenes-folder. /// *Open up a scene (LEVEL1) from build settings, hit play /// *While playing, do something to make the game /// change to another level (LEVEL2). /// *Stop playing, you should now be back in the level where you /// hit play from. /// *Try to switch to the level you switched to in-game (LEVEL2). /// *You should, after the progress bar has completed, be prompted /// with an error saying Unity could not move file from Temp/Tempfile /// try { FileStream f = File.OpenWrite(saveAndReload); f.Close(); } catch { Debug.LogWarning("HACK: Getting around 'access denied' on temp files!"); //HACK: This seems to make Unity release the file so we can try to save it in a new go. if (!EditorApplication.OpenScene(saveAndReload)) { //Uh oh. Debug.LogError("HACK failed! What to do next?"); EditorUtility.ClearProgressBar(); return(Status.Failed); } TextSceneDeserializer.Load(EditorHelper.GetProjectFolder() + TextScene.TempToTextSceneFile(EditorApplication.currentScene), saveAndReloadCallback); return(Status.Working); } if (!EditorApplication.SaveScene(saveAndReload)) { Debug.LogError("Failed to save temp: " + saveAndReload); if (EditorUtility.DisplayDialog("ERROR", "Failed to save temp (" + saveAndReload + ") - try again?", "Yes", "No")) { saveAndReloadTimer = Mathf.RoundToInt(SAVE_AND_RELOAD_FRAMES); } else { return(Status.Failed); } EditorUtility.ClearProgressBar(); return(Status.Working); } state = State.CreateNew; saveAndReloadTimer = Mathf.RoundToInt(SAVE_AND_RELOAD_FRAMES); return(Status.Working); } else if (state == State.CreateNew) { EditorApplication.NewScene(); state = State.LoadTemp; saveAndReloadTimer = Mathf.RoundToInt(SAVE_AND_RELOAD_FRAMES); return(Status.Working); } else if (state == State.LoadTemp) { if (!EditorApplication.OpenScene(saveAndReload)) { Debug.LogError("Failed to load temp: " + saveAndReload); if (EditorUtility.DisplayDialog("ERROR", "Failed to load temp (" + saveAndReload + ") - try again?", "Yes", "No")) { saveAndReloadTimer = Mathf.RoundToInt(SAVE_AND_RELOAD_FRAMES); } else { return(Status.Failed); } EditorUtility.ClearProgressBar(); return(Status.Working); } string writtenFile = EditorHelper.GetProjectFolder() + EditorApplication.currentScene; DateTime writtenTime = File.GetLastWriteTime(writtenFile); Debug.Log("Wrote temp file at " + writtenTime); TextSceneMonitor.Instance.SetCurrentScene(EditorHelper.GetProjectFolder() + TextScene.TempToTextSceneFile(EditorApplication.currentScene)); saveAndReload = ""; EditorUtility.ClearProgressBar(); return(Status.Complete); } } Debug.LogError("Failing...."); return(Status.Failed); }