Advertisement
Muk99

Autosave

Nov 15th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using UnityEditor;
  2. #if UNITY_5_3_OR_NEWER
  3. using UnityEditor.SceneManagement;
  4. #endif
  5. using UnityEngine;
  6.  
  7. internal class AutosaveUnity {
  8.  
  9.     private static bool autosaveEnabled {
  10.         get { return EditorPrefs.GetBool("Autosave", true); }
  11.         set { EditorPrefs.SetBool("Autosave", value); }
  12.     }
  13.  
  14.     [MenuItem("Edit/Autosave", false, -10000000)]
  15.     private static void AutosaveMenu() {
  16.         autosaveEnabled = !autosaveEnabled;
  17.         Menu.SetChecked("Edit/Autosave", autosaveEnabled);
  18.     }
  19.  
  20.     [MenuItem("Edit/Autosave", true, -10000000)]
  21.     private static bool MenuCheck() {
  22.         Menu.SetChecked("Edit/Autosave", autosaveEnabled);
  23.         return true;
  24.     }
  25.  
  26.     [InitializeOnLoadMethod]
  27.     private static void Init() {
  28.         EditorApplication.playmodeStateChanged += () => {
  29.             if(EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying) {
  30.                 if(autosaveEnabled) {
  31.                     Debug.Log("Auto-Saving project");
  32.  
  33. #if UNITY_5_3_OR_NEWER
  34.                     EditorSceneManager.SaveOpenScenes();
  35. #else
  36.                     EditorApplication.SaveScene();
  37. #endif
  38.                     AssetDatabase.SaveAssets();
  39.                 }
  40.             }
  41.         };
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement