Skip to content

Instantly share code, notes, and snippets.

@R3tr0BoiDX
Last active August 29, 2022 19:13
Show Gist options
  • Save R3tr0BoiDX/274636a98b636caa4bdeb33b0c8043ca to your computer and use it in GitHub Desktop.
Save R3tr0BoiDX/274636a98b636caa4bdeb33b0c8043ca to your computer and use it in GitHub Desktop.
A custom compilation window, as Unity's default behavior became kinda painful recently. Allows you to turn off auto recompile and gives you the option to compile on demand and, if wanted, enters play mode
/// Allows you to turn off auto recompile and gives you the option to compile on demand and, if wanted, enters play mode
using UnityEditor;
using UnityEditor.Compilation;
using UnityEngine;
namespace Editor {
public class CompilationWindow : EditorWindow {
private static bool autoRefresh;
private bool enterPlaymode;
private void Update() {
if (enterPlaymode && !EditorApplication.isCompiling) {
enterPlaymode = false;
EditorApplication.EnterPlaymode();
}
}
private void OnGUI() {
CheckAutoRefreshState();
if (GUILayout.Button("Compile")) {
CompilationPipeline.RequestScriptCompilation();
}
if (GUILayout.Button("Compile and play")) {
CompilationPipeline.RequestScriptCompilation();
enterPlaymode = true;
}
autoRefresh = EditorGUILayout.Toggle("Auto recompile", autoRefresh);
EditorPrefs.SetInt("kAutoRefresh", autoRefresh ? 1 : 0);
DrawLine();
ShowIfCompiling();
}
[MenuItem("Window/Compilation")]
private static void ShowWindow() {
CheckAutoRefreshState();
CompilationWindow window = GetWindow<CompilationWindow>();
window.titleContent = new GUIContent("Compilation");
window.Show();
}
private void ShowIfCompiling() {
EditorGUILayout.LabelField("Compiling:", EditorApplication.isCompiling ? "Yes" : "No");
Repaint();
}
private static void CheckAutoRefreshState() {
int status = EditorPrefs.GetInt("kAutoRefresh");
autoRefresh = status == 1;
}
private static void DrawLine(int _height = 1) {
Rect rect = EditorGUILayout.GetControlRect(false, _height);
rect.height = _height;
EditorGUI.DrawRect(rect, Color.grey);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment