EgonMilanVotrubec

AutoSave.cs

Mar 25th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System.Threading.Tasks;
  2.  
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEditor.SceneManagement;
  6.  
  7. [InitializeOnLoad]
  8. public class AutoSave
  9. {
  10.     private static bool active = true;
  11.     public static int Seconds = 10;
  12.  
  13.     [MenuItem ( "StartAutoSave", menuItem = "Tools/Start AutoSave.", priority = 10, validate = false )]
  14.     public static async void Start ( )
  15.     {
  16.         active = true;
  17.  
  18.         while ( active )
  19.         {
  20.             await Task.Delay ( Seconds * 1_000 );
  21.             Save ( );
  22.         }
  23.     }
  24.  
  25.     [MenuItem ( "StartAutoSave", menuItem = "Tools/Start AutoSave.", priority = 10, validate = true )]
  26.     public static bool ValidateStart ( )
  27.     {
  28.         return ( !active );        
  29.     }
  30.          
  31.     [MenuItem ( "StopAutoSave", menuItem = "Tools/Stop AutoSave.", priority = 11, validate = false )]
  32.     public static void Stop ( )
  33.     {
  34.         active = false;
  35.     }
  36.  
  37.     [MenuItem ( "StopAutoSave", menuItem = "Tools/Stop AutoSave.", priority = 11, validate = true )]
  38.     public static bool ValidateStop ( )
  39.     {
  40.         return ( active );
  41.     }
  42.    
  43.     private static void Save ()
  44.     {
  45.         var saved = EditorSceneManager.SaveOpenScenes ( );
  46.         Debug.LogWarning ( $"Open Scenes {( saved ? "" : "NOT " )}saved." );
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment