Created
November 16, 2011 00:12
-
-
Save darktable/1368845 to your computer and use it in GitHub Desktop.
Unity3D: Somewhat Hacky Method for Auto Saving Assets in Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#define VERBOSE | |
/* *************************************************************************** | |
Copyright 2011 Calvin Rien | |
(http://the.darktable.com) | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*************************************************************************** */ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
[InitializeOnLoad] | |
public class OnUnityLoad { | |
static OnUnityLoad() { | |
if (AutoSaveAssets.instance == null) | |
AutoSaveAssets.instance = ScriptableObject.CreateInstance<AutoSaveAssets>(); | |
} | |
} | |
/// <summary> | |
/// Drop this into a folder called "Editor" in your Unity project (or Plugins/Editor, or Standard Assets/Editor). | |
/// | |
/// This should autosave modified assets (Materials, Prefabs, etc.) every "interval" seconds, unless you are | |
/// running the game in the editor. | |
/// </summary> | |
public class AutoSaveAssets : ScriptableObject { | |
static int interval = 300; | |
static uint lastRun = 0; | |
public static AutoSaveAssets instance; | |
void OnEnable() { | |
if (instance != null && instance != this) { | |
DestroyImmediate(this); | |
return; | |
} | |
instance = this; | |
lastRun = (uint)EditorApplication.timeSinceStartup; | |
EditorApplication.update += EditorUpdate; | |
} | |
void OnDisable() { | |
if (instance == this) | |
EditorApplication.update -= EditorUpdate; | |
} | |
static void EditorUpdate() { | |
var seconds = (uint)EditorApplication.timeSinceStartup; | |
if (seconds - lastRun >= interval && SaveAssets()) { | |
lastRun = seconds; | |
} | |
} | |
static bool SaveAssets() { | |
if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || EditorApplication.isPaused) | |
return false; | |
#if VERBOSE | |
Debug.Log(string.Format("=== auto saving @ {0} ===", System.DateTime.Now.ToLongTimeString())); | |
#endif | |
EditorApplication.SaveAssets(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment