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

参数

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

返回

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

描述

保存场景。

所有路径均是项目文件夹的相对路径,例如:"Assets/MyScenes/MyScene.unity"。调用此函数之前,路径中指定的文件夹必须已存在。如果未指定任何路径,则使用当前打开的场景的路径,除非此场景在这之前从未保存,在这种情况下,会显示一个保存对话框。

如果保存失败,此函数将返回 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; } } }