using UnityEditor ;

using UnityEngine ;

using System ;

using UnityEditor.SceneManagement ;

[ InitializeOnLoad ]

///A L M I G H T Y A U T O S A V E R S C R I P T

/////You only need to include this file inside a folder named "Editor" anywhere in your Assets folder hierarchy

class AutoSaver

{

//is the autosave feature enabled

public static bool autoSave ;

//DateTime of the last save time for evaluating passed time

public static System . DateTime lastSaveTime ;

//how often auto save happens

public static float autoSaveInterval = 30f ;

static AutoSaver ( )

{

EditorApplication . update += Update ;

}

static void Update ( )

{

if ( autoSave )

{

//get the difference between DateTime.Now and last save time

TimeSpan timeDifference = DateTime . Now . Subtract ( lastSaveTime ) ;

//if enough time has passed since last save and the scene is modified

if ( timeDifference . Seconds > autoSaveInterval && EditorSceneManager . GetActiveScene ( ) . isDirty )

{

lastSaveTime = DateTime . Now ;

//save all open scenes

EditorSceneManager . SaveOpenScenes ( ) ;

Debug . Log ( "Scene autosaved." ) ;

}

}

}

//Show the menu option to toggle the autoSave feature on/off

//sometimes it takes an extra toggle to switch the mode for some reason

[ MenuItem ( "Auto Save/Enabled" , false ) ]

static void EnableAutoSave ( )

{

autoSave = ! autoSave ;

Menu . SetChecked ( "Auto Save/Enabled" , autoSave ) ;

}