Guest User

Untitled

a guest
Oct 2nd, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.54 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Text.RegularExpressions;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8.  
  9. /*
  10.  * MultiAutoSave
  11.  *
  12.  * Autosaves scene at different intervals while keeping a maximum number of files per interval
  13.  *
  14.  * Author: Eneko Castresana Vara
  15.  * Based on AutoSave by Melle Heeres
  16.  *
  17.  * By using this script you understand and acknowledge no warranties of any kind are provided
  18.  * and if you'll sue me you'll burn in hell
  19. */
  20. public class MultiAutoSave : EditorWindow {
  21.  
  22.     private class Periodicity {
  23.         public int periodMinutes;
  24.         public int maxFiles;
  25.         public DateTime lastSaveTime;
  26.  
  27.         public Periodicity(int periodMinutes, int maxFiles) {
  28.             this.periodMinutes=periodMinutes;
  29.             this.maxFiles=maxFiles;
  30.             this.lastSaveTime=DateTime.Now;
  31.         }
  32.     }
  33.  
  34.     private bool enabled = true;
  35.     private bool showMessage = true;
  36.  
  37.     private string projectPath;
  38.     private string scenePath;
  39.  
  40.     private List<Periodicity> periodicities = new List<Periodicity> ();
  41.     private Vector2 scrollPosition=new Vector2(0,0);
  42.  
  43.     private bool firstRunActions=true;
  44.  
  45.     [MenuItem ("Window/MultiAutoSave")]
  46.     static void Init () {
  47.         MultiAutoSave saveWindow = (MultiAutoSave)EditorWindow.GetWindow (typeof (MultiAutoSave));
  48.         saveWindow.Show();
  49.     }
  50.  
  51.     void OnEnable () {
  52.         projectPath = Application.dataPath;
  53.         scenePath = EditorApplication.currentScene;
  54.  
  55.         //Set up a sensible choice of default periodicities
  56.         periodicities.Add(new Periodicity(1, 5));
  57.         periodicities.Add(new Periodicity(10, 3));
  58.         periodicities.Add(new Periodicity(60, 12));
  59.     }
  60.  
  61.     void OnGUI () {
  62.         GUILayout.Label ("Info:", EditorStyles.boldLabel);
  63.         EditorGUILayout.LabelField ("Saving to:", ""+projectPath);
  64.         EditorGUILayout.LabelField ("Saving scene:", ""+scenePath);
  65.         GUILayout.Label ("Options:", EditorStyles.boldLabel);
  66.         enabled = EditorGUILayout.BeginToggleGroup ("Enabled", enabled);
  67.         EditorGUILayout.EndToggleGroup();
  68.         showMessage = EditorGUILayout.BeginToggleGroup ("Show Message", showMessage);
  69.         EditorGUILayout.EndToggleGroup ();
  70.  
  71.         if (GUILayout.Button ("+")) {
  72.             periodicities.Add (new Periodicity (1, 1));
  73.             //Save from the very beginning
  74.             saveScene (periodicities.Last());
  75.         }
  76.  
  77.         scrollPosition=EditorGUILayout.BeginScrollView (scrollPosition);
  78.         foreach (Periodicity p in periodicities.ToList()) {
  79.             GUILayout.Label ("Periodicity:", EditorStyles.boldLabel);
  80.             p.periodMinutes = EditorGUILayout.IntSlider ("Interval (minutes)", p.periodMinutes, 1, 1440);
  81.             p.maxFiles = EditorGUILayout.IntSlider ("Max files", p.maxFiles, 1, 100);
  82.             EditorGUILayout.LabelField ("Last save:", ""+p.lastSaveTime);
  83.             if (GUILayout.Button ("Remove"))
  84.                 periodicities.Remove (p);
  85.             EditorGUILayout.Separator ();
  86.         }
  87.         EditorGUILayout.EndScrollView ();
  88.     }
  89.  
  90.  
  91.     void Update(){
  92.         if (!enabled)
  93.             return;
  94.  
  95.         if (firstRunActions) {
  96.             firstRunActions = false;
  97.             //Save from the very beginning (Needs to be done here)
  98.             foreach (Periodicity p in periodicities)
  99.                 saveScene (p);
  100.         }
  101.  
  102.         DateTime now = DateTime.Now;
  103.         foreach (Periodicity p in periodicities)
  104.             if (now > p.lastSaveTime.AddMinutes (p.periodMinutes))
  105.                 saveScene (p);
  106.     }
  107.  
  108.     void saveScene(Periodicity p) {
  109.         scenePath = EditorApplication.currentScene;
  110.         GroupCollection groups=Regex.Match (scenePath, @"(.*[/\\])(.+)\.unity$").Groups;
  111.         string sceneDryPath = groups [1].Value;
  112.         string sceneDryName = groups [2].Value;
  113.  
  114.         p.lastSaveTime = DateTime.Now;
  115.         string timeTag=p.lastSaveTime.ToString("yyyy_MM_dd_HH_mm");
  116.  
  117.         string fileName = sceneDryPath + sceneDryName + "-autosave-"+p.periodMinutes+ "m-" + timeTag + ".unity";
  118.  
  119.         if (showMessage)
  120.             Debug.Log ("Now saving "+fileName);
  121.        
  122.         EditorApplication.SaveScene(fileName, true);
  123.  
  124.         deleteOldFiles (p, sceneDryPath, sceneDryName);
  125.            
  126.         MultiAutoSave repaintSaveWindow = (MultiAutoSave)EditorWindow.GetWindow (typeof (MultiAutoSave));
  127.         repaintSaveWindow.Repaint();
  128.    
  129.     }
  130.  
  131.     void deleteOldFiles (Periodicity p, string sceneDryPath, string sceneDryName) {
  132.         List<FileInfo> files=new List<FileInfo>();
  133.         foreach (string fileName in Directory.GetFiles(sceneDryPath, sceneDryName+"-autosave-"+p.periodMinutes+"m-*.unity*"))
  134.             files.Add (new FileInfo (fileName));
  135.         files.Sort((a, b) => DateTime.Compare(a.LastWriteTime, b.LastWriteTime));
  136.  
  137.         //This should work assuming both .unity and .unity.meta files are always saved
  138.         for (int i = 0; i < files.Count - p.maxFiles * 2; i++) {
  139.             if (showMessage)
  140.                 Debug.Log ("Deleting old file "+files[i].FullName);
  141.             files [i].Delete ();
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment