下面代码实现在Unity编辑器里,点击Play时,自动保存当前场景。
- 在Editor里新建AudoSave.cs
- 将下面的代码复制进去即可
csharp
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[InitializeOnLoad]
public class Autosave
{
static Autosave()
{
EditorApplication.playModeStateChanged += (PlayModeStateChange state) =>
{
if (state == PlayModeStateChange.ExitingEditMode)
{
try
{
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
Debug.Log("[AutoSave] Scenes and assets saved!");
}
catch (System.Exception ex)
{
Debug.LogError($"[AutoSave] Failed: {ex.Message}");
}
}
};
}
}