Skip to content

Instantly share code, notes, and snippets.

@ByronMayne
Created January 20, 2018 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ByronMayne/7a438b360e70c32596c95209c0d05a22 to your computer and use it in GitHub Desktop.
Save ByronMayne/7a438b360e70c32596c95209c0d05a22 to your computer and use it in GitHub Desktop.
A simple method to force Unity Editor to recompile all scripts.
public class AssemblyUtility
{
/// <summary>
/// Forces Unity to recompile all scripts and then refresh.
/// </summary>
public static void DirtyAllScripts()
{
// Grab the UnityEditor assembly
Assembly editorAssembly = typeof(UnityEditor.Editor).Assembly;
// Find the type that contains the method we want
Type compilationInterface = editorAssembly.GetType("UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface");
// Make sure it's not null
if (compilationInterface != null)
{
// Create our binding flags
BindingFlags staticBindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
// Grab the dirty method
MethodInfo dirtyAllScriptsMethod = compilationInterface.GetMethod("DirtyAllScripts", staticBindingFlags);
// Invoke the static method with no arguments.
dirtyAllScriptsMethod.Invoke(null, null);
}
// Force the database to refresh.
UnityEditor.AssetDatabase.Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment