Follow

How to stop automatic assembly compilation from script

Symptoms

  • I do not always want Unity to reload my script assemblies.
  • Recompiling script files during play mode is causing problems.

Cause

Unity will recompile any changes to script files as soon as the changes are imported.

Resolution

Unity provides a method for preventing the script assemblies from being loaded.

Firstly, create an Editor Script that will be loaded at while the project is open with [InitializeOnLoad], and receive update events from the Editor with the EditorApplication.update delegate.

using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class CompilerOptionsEditorScript
{
    static CompilerOptionsEditorScript()
    {
        EditorApplication.update += OnEditorUpdate;
    }

    static void OnEditorUpdate()
    {
        if( EditorApplication.isCompiling )
            Debug.Log( "Scripts are compiling" );
    }
}

When EditorApplication.isCompiling is detected, use EditorApplication.LockReloadAssemblies() where needed to prevent Unity from compiling the scripts until EditorApplication.UnlockReloadAssemblies() is called.

To prevent scripts recompiling during PlayMode, EditorApplication.isPlaying can be used to detect if the Editor is in PlayMode; and the EditorApplication.playmodeStateChange event for changes to the state.

using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class CompilerOptionsEditorScript
{
   static bool waitingForStop = false;

    static CompilerOptionsEditorScript()
    {
        EditorApplication.update += OnEditorUpdate;
    }

    static void OnEditorUpdate()
    {
        if( ! waitingForStop
&& EditorApplication.isCompiling
&& EditorApplication.isPlaying )
        {
            EditorApplication.LockReloadAssemblies();
            EditorApplication.playmodeStateChanged
+= PlaymodeChanged;
            waitingForStop = true;
       }
    }

   static void PlaymodeChanged()
   {
       if( EditorApplication.isPlaying )
return;
       
       EditorApplication.UnlockReloadAssemblies();
       EditorApplication.playmodeStateChanged
-= PlaymodeChanged;
       waitingForStop = false;
   }
}

More Information

http://docs.unity3d.com/Manual/RunningEditorCodeOnLaunch.html
http://docs.unity3d.com/ScriptReference/EditorApplication.html

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.