public static bool SaveScene (SceneManagement.Scene scene, string dstScenePath= "", bool saveAsCopy= false);

Parameters

scene要保存的场景。
dstScenePath场景要保存到的文件路径。如果路径为空,则表示当前打开的场景已被覆盖。如果此场景尚未完全保存,则会显示一个保存对话框。
saveAsCopy如果设置为 true,系统将保存场景且不会更改当前场景,也不会清除未保存的更改标记。

Returns

bool 如果保存成功,则返回 true,否则返回 false。

Description

保存场景。

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.

如果保存失败,此函数将返回 false。如果指定的路径无效,或用户取消此保存对话,则会出现这种情况。

另请参阅:EditorSceneManager.GetActiveScene

Example:
Simple Editor Window that saves each 300 seconds the current Scene.

注意:无论是否将场景标记为“脏”,此方法都会保存此场景。

using UnityEditor;
using UnityEngine;
using System.Collections;
using UnityEditor.SceneManagement;

public class SimpleAutoSave : EditorWindow { public float saveTime = 300; public float nextSave = 0; [MenuItem("Example/Simple autoSave")] static void Init() { SimpleAutoSave window = (SimpleAutoSave)EditorWindow.GetWindowWithRect(typeof(SimpleAutoSave), new Rect(0, 0, 200, 50)); window.Show(); }

void OnGUI() { EditorGUILayout.LabelField("Save Each:", saveTime + " Secs"); float timeToSave = nextSave - (float)EditorApplication.timeSinceStartup; EditorGUILayout.LabelField("Next Save:", timeToSave.ToString() + " Sec"); Repaint(); if (EditorApplication.timeSinceStartup > nextSave) { string[] path = EditorSceneManager.GetActiveScene().path.Split(char.Parse("/")); path[path.Length - 1] = "AutoSave_" + path[path.Length - 1]; bool saveOK = EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), string.Join("/", path)); Debug.Log("Saved Scene " + (saveOK ? "OK" : "Error!")); nextSave = (float)EditorApplication.timeSinceStartup + saveTime; } } }