Legacy Documentation: Version 5.2
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EditorApplication.SaveScene

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static function SaveScene(path: string = "", saveAsCopy: bool = false): bool;
public static bool SaveScene(string path = "", bool saveAsCopy = false);
public static function SaveScene(path: string = "", saveAsCopy: bool = false): bool;
public static bool SaveScene(string path = "", bool saveAsCopy = false);
public static function SaveScene(path: string = "", saveAsCopy: bool = false): bool;
public static bool SaveScene(string path = "", bool saveAsCopy = false);

Parameters

path The file path to save at. If empty, the current open scene will be overwritten, or if never saved before, a save dialog is shown.
saveAsCopy If set to true, the scene will be saved without changing the currentScene and without clearing the unsaved changes marker.

Returns

bool True if the save succeeded, otherwise false.

Description

Save the open scene.

All paths are relative to the project folder, such as: "Assets/MyScenes/MyScene.unity". Folders specified in the path must already exist before calling the function. If no path is specified, the path of the current open scene is used, except if it was never saved before, in which case a save dialog is shown.

The function returns false if the save failed. This can happen if the specified path is invalid or if the user cancels in the case of a save dialog.

When calling the function, the unsaved changes marker is cleared, just as when saving using the file menu. (On Windows, the unsaved changes marker is an asterix after the file name in the window title. On Mac OS X it's a dot inside the red close button of the window.) When a path is specified, the currentScene is also changed to be the specified asset.

When saveAsCopy is set to true however, neither the currentScene or the unsaved changes marker is changed.

See Also: currentScene.


Simple Editor Window that saves each 300 seconds the current scene.

	// Simple editor window that autosaves the working scene
	// Make sure to have this window opened to be able to execute the auto save.
	
	import UnityEditor;
	
	class SimpleAutoSave extends EditorWindow {
		
		var saveTime : float = 300;
		var nextSave : float = 0;
	
		@MenuItem("Example/Simple autoSave")
		static function Init() {
			var window : SimpleAutoSave = 
				EditorWindow.GetWindowWithRect(
					SimpleAutoSave, 
					Rect(0,0,165,40));
			window.Show();
		}
		function OnGUI() {
			 EditorGUILayout.LabelField("Save Each:", saveTime + " Secs");
			 var timeToSave : int = nextSave - EditorApplication.timeSinceStartup;
			 EditorGUILayout.LabelField("Next Save:", timeToSave.ToString() + " Sec");
			 this.Repaint();
			 
			if(EditorApplication.timeSinceStartup > nextSave) {
				var path : String [] = EditorApplication.currentScene.Split(char.Parse("/"));
				path[path.Length -1] = "AutoSave_" + path[path.Length-1];	
				EditorApplication.SaveScene(String.Join("/",path), true);
				Debug.Log("Saved Scene");
				nextSave = EditorApplication.timeSinceStartup + saveTime;
			}
		}
	}