scene | 要保存的场景。 |
dstScenePath | 场景要保存到的文件路径。如果路径为空,则表示当前打开的场景已被覆盖。如果此场景尚未完全保存,则会显示一个保存对话框。 |
saveAsCopy | 如果设置为 true,系统将保存场景且不会更改当前场景,也不会清除未保存的更改标记。 |
bool 如果保存成功,则返回 true,否则返回 false。
保存场景。
所有路径均是项目文件夹的相对路径,例如:“Assets/MyScenes/MyScene.unity”。调用此函数之前,路径中指定的文件夹必须已存在。如果未指定任何路径,则使用当前打开的场景的路径,除非此场景在这之前从未保存,在这种情况下,会显示一个保存对话框。
如果保存失败,此函数将返回 false。如果指定的路径无效,或用户取消此保存对话,则会出现这种情况。
另请参阅:EditorSceneManager.GetActiveScene
示例:
每 300 秒保存一次当前场景的简单编辑器窗口。
注意:无论是否将场景标记为“脏”,此方法都会保存此场景。
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; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.