Unity 自动保存项目工程的场景

在网络上查找了有关unity工程项目自动保存场景的脚本,使用时发现有些方法已经被Unity弃用并出现频繁保存导致运行卡顿的现象。于是自己修改了代码,实现每隔60s左右自动保存场景。代码如下:

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

public class AutoSave : EditorWindow
{

    private bool autoSaveScene = true;
    private bool showMessage = true;
    private bool isStarted = false;
    private int intervalScene;
    private DateTime lastSaveTimeScene;
    private string projectPath;
    private string scenePath;

    [MenuItem("Window/AutoSave")]

    public void OnEnable()
    {
        lastSaveTimeScene = DateTime.Now;
        projectPath = Application.dataPath;
    }
    static void Init()
    {
        AutoSave saveWindow = (AutoSave)EditorWindow.GetWindow(typeof(AutoSave));
        saveWindow.Show();
    }

    float time = 0;
    void Update()
    {      
        if ( autoSaveScene )
        {       
            time += Time.fixedDeltaTime;
           // Debug.Log(time);
            if ( time > 120.0f ) {              
                time = 0;
                saveScene();              
            }           
        }
        else
        {
            isStarted = false;
        }

    }

    void saveScene()
    {
        scenePath = EditorSceneManager.GetActiveScene().path;
        EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), scenePath);
        lastSaveTimeScene = DateTime.Now;
        isStarted = true;
        if ( showMessage )
        {
            Debug.Log("自动保存路径: " + scenePath + " 保存时间: " + lastSaveTimeScene);
        }
        AutoSave repaintSaveWindow = (AutoSave)EditorWindow.GetWindow(typeof(AutoSave));
        repaintSaveWindow.Repaint();
    }
}

你可能感兴趣的:(Unity开发)