Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Text.RegularExpressions;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- /*
- * MultiAutoSave
- *
- * Autosaves scene at different intervals while keeping a maximum number of files per interval
- *
- * Author: Eneko Castresana Vara
- * Based on AutoSave by Melle Heeres
- *
- * By using this script you understand and acknowledge no warranties of any kind are provided
- * and if you'll sue me you'll burn in hell
- */
- public class MultiAutoSave : EditorWindow {
- private class Periodicity {
- public int periodMinutes;
- public int maxFiles;
- public DateTime lastSaveTime;
- public Periodicity(int periodMinutes, int maxFiles) {
- this.periodMinutes=periodMinutes;
- this.maxFiles=maxFiles;
- this.lastSaveTime=DateTime.Now;
- }
- }
- private bool enabled = true;
- private bool showMessage = true;
- private string projectPath;
- private string scenePath;
- private List<Periodicity> periodicities = new List<Periodicity> ();
- private Vector2 scrollPosition=new Vector2(0,0);
- private bool firstRunActions=true;
- [MenuItem ("Window/MultiAutoSave")]
- static void Init () {
- MultiAutoSave saveWindow = (MultiAutoSave)EditorWindow.GetWindow (typeof (MultiAutoSave));
- saveWindow.Show();
- }
- void OnEnable () {
- projectPath = Application.dataPath;
- scenePath = EditorApplication.currentScene;
- //Set up a sensible choice of default periodicities
- periodicities.Add(new Periodicity(1, 5));
- periodicities.Add(new Periodicity(10, 3));
- periodicities.Add(new Periodicity(60, 12));
- }
- void OnGUI () {
- GUILayout.Label ("Info:", EditorStyles.boldLabel);
- EditorGUILayout.LabelField ("Saving to:", ""+projectPath);
- EditorGUILayout.LabelField ("Saving scene:", ""+scenePath);
- GUILayout.Label ("Options:", EditorStyles.boldLabel);
- enabled = EditorGUILayout.BeginToggleGroup ("Enabled", enabled);
- EditorGUILayout.EndToggleGroup();
- showMessage = EditorGUILayout.BeginToggleGroup ("Show Message", showMessage);
- EditorGUILayout.EndToggleGroup ();
- if (GUILayout.Button ("+")) {
- periodicities.Add (new Periodicity (1, 1));
- //Save from the very beginning
- saveScene (periodicities.Last());
- }
- scrollPosition=EditorGUILayout.BeginScrollView (scrollPosition);
- foreach (Periodicity p in periodicities.ToList()) {
- GUILayout.Label ("Periodicity:", EditorStyles.boldLabel);
- p.periodMinutes = EditorGUILayout.IntSlider ("Interval (minutes)", p.periodMinutes, 1, 1440);
- p.maxFiles = EditorGUILayout.IntSlider ("Max files", p.maxFiles, 1, 100);
- EditorGUILayout.LabelField ("Last save:", ""+p.lastSaveTime);
- if (GUILayout.Button ("Remove"))
- periodicities.Remove (p);
- EditorGUILayout.Separator ();
- }
- EditorGUILayout.EndScrollView ();
- }
- void Update(){
- if (!enabled)
- return;
- if (firstRunActions) {
- firstRunActions = false;
- //Save from the very beginning (Needs to be done here)
- foreach (Periodicity p in periodicities)
- saveScene (p);
- }
- DateTime now = DateTime.Now;
- foreach (Periodicity p in periodicities)
- if (now > p.lastSaveTime.AddMinutes (p.periodMinutes))
- saveScene (p);
- }
- void saveScene(Periodicity p) {
- scenePath = EditorApplication.currentScene;
- GroupCollection groups=Regex.Match (scenePath, @"(.*[/\\])(.+)\.unity$").Groups;
- string sceneDryPath = groups [1].Value;
- string sceneDryName = groups [2].Value;
- p.lastSaveTime = DateTime.Now;
- string timeTag=p.lastSaveTime.ToString("yyyy_MM_dd_HH_mm");
- string fileName = sceneDryPath + sceneDryName + "-autosave-"+p.periodMinutes+ "m-" + timeTag + ".unity";
- if (showMessage)
- Debug.Log ("Now saving "+fileName);
- EditorApplication.SaveScene(fileName, true);
- deleteOldFiles (p, sceneDryPath, sceneDryName);
- MultiAutoSave repaintSaveWindow = (MultiAutoSave)EditorWindow.GetWindow (typeof (MultiAutoSave));
- repaintSaveWindow.Repaint();
- }
- void deleteOldFiles (Periodicity p, string sceneDryPath, string sceneDryName) {
- List<FileInfo> files=new List<FileInfo>();
- foreach (string fileName in Directory.GetFiles(sceneDryPath, sceneDryName+"-autosave-"+p.periodMinutes+"m-*.unity*"))
- files.Add (new FileInfo (fileName));
- files.Sort((a, b) => DateTime.Compare(a.LastWriteTime, b.LastWriteTime));
- //This should work assuming both .unity and .unity.meta files are always saved
- for (int i = 0; i < files.Count - p.maxFiles * 2; i++) {
- if (showMessage)
- Debug.Log ("Deleting old file "+files[i].FullName);
- files [i].Delete ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment