Skip to content

Instantly share code, notes, and snippets.

@sinistersnare
Last active May 14, 2018 01:19
Show Gist options
  • Save sinistersnare/1f397c66d3290c2fde437ddcd5bc44d3 to your computer and use it in GitHub Desktop.
Save sinistersnare/1f397c66d3290c2fde437ddcd5bc44d3 to your computer and use it in GitHub Desktop.
Auto saves current Unity scene when entering play mode.
#if UNITY_EDITOR
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class AutoSaveSceneOnPlay: ScriptableObject {
static AutoSaveSceneOnPlay() {
EditorApplication.playModeStateChanged += OnStateChanged;
}
private static void OnStateChanged(PlayModeStateChange state) {
Scene currentScene = SceneManager.GetActiveScene();
if (state == PlayModeStateChange.ExitingEditMode) {
Debug.Log("Auto-Saving scene before entering Play mode: " + currentScene.name);
bool saveOK = EditorSceneManager.SaveScene(currentScene, currentScene.path);
if (!saveOK) Debug.Log("SAVE FAILED");
AssetDatabase.SaveAssets();
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment