Guest User

Untitled

a guest
Sep 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEditor;
  4. using UnityEditor.SceneManagement;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using Debug = UnityEngine.Debug;
  8.  
  9. // ReSharper disable InvertIf
  10.  
  11. [InitializeOnLoad]
  12. public class EditorTools
  13. {
  14. static EditorTools()
  15. {
  16. EditorApplication.update += TogglesUpdate;
  17. EditorApplication.update += CompileUpdate;
  18. }
  19.  
  20. #region Saving
  21.  
  22. [MenuItem("EditorTools/SaveProject %q")]
  23. public static void SaveAll()
  24. {
  25. Save();
  26. CommitAll();
  27. }
  28.  
  29. [MenuItem("EditorTools/SaveProject (with non-dirty files) %w")]
  30. public static void SaveAllNonDirty()
  31. {
  32. Save(true);
  33. CommitAll();
  34. }
  35.  
  36. [MenuItem("EditorTools/SaveProject (exclude scenes) %e")]
  37. public static void SaveAllExcludeScenes()
  38. {
  39. Save(saveScenes: false);
  40. CommitAll();
  41. }
  42.  
  43. private static void Save(bool saveNonDirty = false, bool saveScenes = true)
  44. {
  45. if (Application.isPlaying)
  46. return;
  47.  
  48. AssetDatabase.SaveAssets();
  49.  
  50. if (saveScenes)
  51. for (var i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
  52. {
  53. var scene = SceneManager.GetSceneByBuildIndex(i);
  54. if (scene.isDirty || saveNonDirty)
  55. EditorSceneManager.SaveScene(scene);
  56. }
  57.  
  58. Debug.Log(saveScenes ? "Assets and Scenes saved.." : "Assets saved..");
  59. }
  60.  
  61. private static void CommitAll()
  62. {
  63. if (!EditorPrefs.GetBool(ToggleCommitMenuName, false) || Application.isPlaying)
  64. return;
  65.  
  66. #if UNITY_EDITOR_WIN
  67. var psi = new ProcessStartInfo
  68. {
  69. FileName = "cmd",
  70. UseShellExecute = true,
  71. WindowStyle = ProcessWindowStyle.Hidden,
  72. Arguments = $"/C git add -A && git commit -m\"unity gen. commit {DateTime.Now:g}\""
  73. };
  74.  
  75. Process.Start(psi);
  76. Debug.Log("Changes were automatically committed..");
  77. #endif
  78. }
  79.  
  80. #endregion
  81.  
  82. #region Compiling
  83.  
  84. private static bool _doRefresh;
  85.  
  86. private static void CompileUpdate()
  87. {
  88. if (!EditorPrefs.GetBool(ToggleRestartOnCompileMenuName))
  89. return;
  90.  
  91. if (!UnityEditorInternal.InternalEditorUtility.isApplicationActive && Application.isPlaying)
  92. _doRefresh = true;
  93.  
  94. /* Trigger refresh after being tabbed out */
  95. if (UnityEditorInternal.InternalEditorUtility.isApplicationActive && Application.isPlaying && _doRefresh)
  96. {
  97. /* Needs to be delayed twice to trigger script compilation */
  98. EditorApplication.delayCall += () => { EditorApplication.delayCall += AssetDatabase.Refresh; };
  99. _doRefresh = false;
  100. }
  101.  
  102. if (EditorPrefs.GetBool("PlayScheduled") && !EditorApplication.isCompiling && !EditorApplication.isPlaying)
  103. {
  104. Debug.Log("Entering play mode due to script compilation during last run.");
  105.  
  106. EditorApplication.isPlaying = true;
  107. EditorPrefs.SetBool("PlayScheduled", false);
  108. }
  109.  
  110. if (EditorApplication.isPlaying && EditorApplication.isCompiling)
  111. {
  112. Debug.Log("Exiting play mode due to script compilation.");
  113.  
  114. EditorApplication.isPlaying = false;
  115. EditorPrefs.SetBool("PlayScheduled", true);
  116. }
  117. }
  118.  
  119. #endregion
  120.  
  121. #region MenuItems
  122.  
  123. private const string ToggleCommitMenuName = "EditorTools/Commit all on save";
  124. private const string ToggleRestartOnCompileMenuName = "EditorTools/Restart on compile";
  125.  
  126. [MenuItem(ToggleCommitMenuName)]
  127. private static void ToggleCommitAll()
  128. {
  129. var checkState = !Menu.GetChecked(ToggleCommitMenuName);
  130. Menu.SetChecked(ToggleCommitMenuName, checkState);
  131. EditorPrefs.SetBool(ToggleCommitMenuName, checkState);
  132. }
  133.  
  134. [MenuItem(ToggleRestartOnCompileMenuName)]
  135. private static void ToggleRestartOnCompile()
  136. {
  137. var checkState = !Menu.GetChecked(ToggleRestartOnCompileMenuName);
  138. Menu.SetChecked(ToggleRestartOnCompileMenuName, checkState);
  139. EditorPrefs.SetBool(ToggleRestartOnCompileMenuName, checkState);
  140. }
  141.  
  142. private static void TogglesUpdate()
  143. {
  144. Menu.SetChecked(ToggleCommitMenuName, EditorPrefs.GetBool(ToggleCommitMenuName, false));
  145. Menu.SetChecked(ToggleRestartOnCompileMenuName, EditorPrefs.GetBool(ToggleRestartOnCompileMenuName, false));
  146. }
  147.  
  148. #endregion
  149. }
Add Comment
Please, Sign In to add comment