基于unity2017\2020版本
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public static class DisableAutoCompile
{
private const string _KEY = "DisableAutoCompile";
private static string s_RecoverValue = null;
static DisableAutoCompile()
{
UnityEditor.Compilation.CompilationPipeline.assemblyCompilationStarted += CompilationPipeline_assemblyCompilationStarted;
#if UNITY_2018_1_OR_NEWER
UnityEditor.Compilation.CompilationPipeline.compilationFinished += CompilationPipeline_compilationFinished;
#endif
}
private static void CompilationPipeline_assemblyCompilationStarted(string obj)
{
CheckAvoidCompile();
}
private static void CompilationPipeline_compilationFinished(object obj)
{
if (s_RecoverValue != null)
{
EditorPrefs.SetString(_KEY, s_RecoverValue);
s_RecoverValue = null;
}
}
private static void CheckAvoidCompile()
{
if (EditorPrefs.GetString(_KEY, "") == "true")
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
#if UNITY_2018_1_OR_NEWER
Assembly coreModule = assemblies.FirstOrDefault(x => x.FullName.StartsWith("UnityEditor.CoreModule,"));
#else
Assembly coreModule = assemblies.FirstOrDefault(x => x.FullName.StartsWith("UnityEditor,"));
#endif
Type t1 = coreModule.GetType("UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface");
object editorCompilation = t1.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
Type t2 = coreModule.GetType("UnityEditor.Scripting.ScriptCompilation.EditorCompilation");
MethodInfo StopAllCompilation = t2.GetMethod("StopAllCompilation", BindingFlags.Public | BindingFlags.Instance);
StopAllCompilation.Invoke(editorCompilation, null);
Debug.Log("forbid compile");
}
}
[MenuItem("C#编译/允许自动编译", true)]
public static bool CheckAllowCompile()
{
return EditorPrefs.GetString(_KEY, "") == "true";
}
[MenuItem("C#编译/允许自动编译")]
public static bool AllowCompile()
{
EditorPrefs.SetString(_KEY, "");
}
[MenuItem("C#编译/禁止自动编译", true)]
public static bool CheckForbidCompile()
{
return EditorPrefs.GetString(_KEY, "") != "true";
}
[MenuItem("C#编译/禁止自动编译", true)]
public static bool ForbidCompile()
{
EditorPrefs.SetString(_KEY, "true");
}
#if UNITY_2018_1_OR_NEWER
[MenuItem("C#编译/现在编译")]
public static bool CompileNow()
{
string oldValue = EditorPrefs.GetString(_KEY, "");
AllowCompile();
s_RecoverValue = oldValue;
UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
}
#else
[MenuItem("C#编译/现在编译(会允许自动编译)")]
public static bool CompileNow()
{
AllowCompile();
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly coreModule = assemblies.FirstOrDefault(x => x.FullName.StartsWith("UnityEditor,"));
Type t1 = coreModule.GetType("UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface");
object editorCompilation = t1.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static).GetValue(null, null);
Type t2 = coreModule.GetType("UnityEditor.Scripting.ScriptCompilation.EditorCompilation");
MethodInfo DirtyAllScripts = t2.GetMethod("DirtyAllScripts", BindingFlags.Public | BindingFlags.Instance);
DirtyAllScripts.Invoke(editorCompilation, null);
}
#endif
}