【Unity技巧】实现在Play时自动保存当前场景

下面代码实现在Unity编辑器里,点击Play时,自动保存当前场景。

  1. 在Editor里新建AudoSave.cs
  2. 将下面的代码复制进去即可
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}");
                }
            }
        };
    }
}
相关推荐
侠客行031712 小时前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
蛇皮划水怪12 小时前
深入浅出LangChain4J
java·langchain·llm
老毛肚14 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎14 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码14 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚14 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂14 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
fuquxiaoguang15 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
琹箐15 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
__WanG15 小时前
JavaTuples 库分析
java