Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using UnityEditor;
- using UnityEditor.SceneManagement;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Debug = UnityEngine.Debug;
- // ReSharper disable InvertIf
- [InitializeOnLoad]
- public class EditorTools
- {
- static EditorTools()
- {
- EditorApplication.update += TogglesUpdate;
- EditorApplication.update += CompileUpdate;
- }
- #region Saving
- [MenuItem("EditorTools/SaveProject %q")]
- public static void SaveAll()
- {
- Save();
- CommitAll();
- }
- [MenuItem("EditorTools/SaveProject (with non-dirty files) %w")]
- public static void SaveAllNonDirty()
- {
- Save(true);
- CommitAll();
- }
- [MenuItem("EditorTools/SaveProject (exclude scenes) %e")]
- public static void SaveAllExcludeScenes()
- {
- Save(saveScenes: false);
- CommitAll();
- }
- private static void Save(bool saveNonDirty = false, bool saveScenes = true)
- {
- if (Application.isPlaying)
- return;
- AssetDatabase.SaveAssets();
- if (saveScenes)
- for (var i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
- {
- var scene = SceneManager.GetSceneByBuildIndex(i);
- if (scene.isDirty || saveNonDirty)
- EditorSceneManager.SaveScene(scene);
- }
- Debug.Log(saveScenes ? "Assets and Scenes saved.." : "Assets saved..");
- }
- private static void CommitAll()
- {
- if (!EditorPrefs.GetBool(ToggleCommitMenuName, false) || Application.isPlaying)
- return;
- #if UNITY_EDITOR_WIN
- var psi = new ProcessStartInfo
- {
- FileName = "cmd",
- UseShellExecute = true,
- WindowStyle = ProcessWindowStyle.Hidden,
- Arguments = $"/C git add -A && git commit -m\"unity gen. commit {DateTime.Now:g}\""
- };
- Process.Start(psi);
- Debug.Log("Changes were automatically committed..");
- #endif
- }
- #endregion
- #region Compiling
- private static bool _doRefresh;
- private static void CompileUpdate()
- {
- if (!EditorPrefs.GetBool(ToggleRestartOnCompileMenuName))
- return;
- if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive && Application.isPlaying)
- _doRefresh = true;
- /* Trigger refresh after being tabbed out */
- if (UnityEditorInternal.InternalEditorUtility.isApplicationActive && Application.isPlaying && _doRefresh)
- {
- /* Needs to be delayed twice to trigger script compilation */
- EditorApplication.delayCall += () => { EditorApplication.delayCall += AssetDatabase.Refresh; };
- _doRefresh = false;
- }
- if (EditorPrefs.GetBool("PlayScheduled") && !EditorApplication.isCompiling && !EditorApplication.isPlaying)
- {
- Debug.Log("Entering play mode due to script compilation during last run.");
- EditorApplication.isPlaying = true;
- EditorPrefs.SetBool("PlayScheduled", false);
- }
- if (EditorApplication.isPlaying && EditorApplication.isCompiling)
- {
- Debug.Log("Exiting play mode due to script compilation.");
- EditorApplication.isPlaying = false;
- EditorPrefs.SetBool("PlayScheduled", true);
- }
- }
- #endregion
- #region MenuItems
- private const string ToggleCommitMenuName = "EditorTools/Commit all on save";
- private const string ToggleRestartOnCompileMenuName = "EditorTools/Restart on compile";
- [MenuItem(ToggleCommitMenuName)]
- private static void ToggleCommitAll()
- {
- var checkState = !Menu.GetChecked(ToggleCommitMenuName);
- Menu.SetChecked(ToggleCommitMenuName, checkState);
- EditorPrefs.SetBool(ToggleCommitMenuName, checkState);
- }
- [MenuItem(ToggleRestartOnCompileMenuName)]
- private static void ToggleRestartOnCompile()
- {
- var checkState = !Menu.GetChecked(ToggleRestartOnCompileMenuName);
- Menu.SetChecked(ToggleRestartOnCompileMenuName, checkState);
- EditorPrefs.SetBool(ToggleRestartOnCompileMenuName, checkState);
- }
- private static void TogglesUpdate()
- {
- Menu.SetChecked(ToggleCommitMenuName, EditorPrefs.GetBool(ToggleCommitMenuName, false));
- Menu.SetChecked(ToggleRestartOnCompileMenuName, EditorPrefs.GetBool(ToggleRestartOnCompileMenuName, false));
- }
- #endregion
- }
Add Comment
Please, Sign In to add comment