[SOLVED] Turning off the auto-compile

Is there a way to turn off the auto-compile in Unity?

Every time I complete writing some code in Visual Studio and return to the Editor it auto-recompiles as you know.

I’m just curious if this is just a default behavior and can be disabled and building would only take place when clicking the Play button in the editor or actually doing a build for real?

Thanks!

That could be useful sometimes…

Just tested that if you rename …Unity\Editor\Data\Mono\bin\mono.exe
compilation fails, so maybe could rename it, write a script to call the compilation manually on command line or rename it back…

It didn’t work yet, couldnt find some dll, but almost… here’s source what i tried:
https://github.com/UnityCommunity/UnityLibrary/blob/master/External/Compiler/ManualCompiler.bat

1 Like

Move your scripts into a dll. Leave only the dll in the Assets folder. Have the rest of your scripts outside the assets folder. Then you can simply recompile the dll from outside the Unity environment as needed.

It doesn’t make sense for most small team workflows. But then again, most of how you work is pretty counter intuitive.

5 Likes

The DLL idea sounds very good. That is basically how I do development at my day job. So, in my case for Unity I would need to leave the GameManager, Configuration, AudioManager and Sprites outside the DLL because they are the only scripts that are on GameObjects. But everything else I can wrap up into DLLs.

Not sure why it never occurred to me other than I was looking for a way to apply the no-compile option to everything including scripts attached to GameObjects. Hmm… so it wouldn’t be perfect but it definitely would be much closer to what I’m looking for. Thanks!

Only if you save it.

2 Likes

Why would those have to be outside a DLL? Being on GameObjects isn’t relevant.

–Eric

2 Likes

You can do it by creating an editor script. See an example here: https://support.unity3d.com/hc/en-us/articles/210452343-How-to-stop-automatic-assembly-compilation-from-script

2 Likes

What’s the benefit to this? Delaying compilation until you play would mean you couldn’t use any modified MonoBehaviours et. al. in your scene until then.

Edit/Preferences/General/Auto Refresh - Uncheck it and when you want to refresh/recompile hit CTRL+R

Am I missing something, or are you talking about something else, or does everyone not realize that’s there? I can’t stand auto-refresh and always keep it off.

35 Likes

LOL! Different answers and in the end it actually exists in a straightforward way!

Thanks!

3 Likes

Clearly a case of not really understanding how Unity works. I figured it literally needed to attach to individual “physical” script source files when adding them as components. I spend the minimum needed working with Unity that way in the Editor and that was the impression I had.

Unity always uses compiled code; whether the code is compiled from scripts inside Unity or pre-compiled into a DLL makes no difference.

–Eric

2 Likes

This has been something that has pissed me off for the longest time with unity…ie compiling during playback, I often go code some changes while playback is in session… I wouldn’t mind it compiling during playback for quicker changes to be seen, however its proven itself to be completely useless at doing it without breaking or crashing 99% of the time. So this is a good solution with tweaking, however seems a little broken still.

Ie Unity little loading compile icon starts spinning as if compiling during playback, even though it is blocked from compiling until playback is stopped.

EditorApplication.LockReloadAssemblies(); clearly isn’t preventing that icon even though the point of that icon is to show it is compiling right.

Adding here just for reference, saw this plugin:

  • Disable Recompiling on Play mode
  • Refreshing Other Assets works

Also: you need to restart Unity for this change in your Preferences to work.

Also: it looks like if you refresh for new assets/project files, and you have scripts that need to be compiled too, you will also need to refresh again for scripts.

Also: Deleting scripts from project view can be problematic sometimes. It will say it can not find the file, and then it will not compile. I have not been able to track down the core of this problem yet. I think it is related to Visual Studio, but appears mostly to be if the script is attached to any object in scene or prefab. If you weed those out, and be sure to remove them, I think it works, if not, make sure a tab for the script is not openn in visual studio (or in the list of active scripts)

5 Likes

I believe this is now under Preferences > Asset Pipeline > Auto Refresh

13 Likes

Now all they need to do is combine that and ‘Run’ into a single operation.

1 Like

Okay so that worked a couple times. They I started getting error messages about cocoapods. I can build and run with auto-refresh but not with manual.

So I will go back to waiting for compile every time a change a source file and go back to unity. Ugh.

What a joke.

EXACTLY, they should have had it day 1.

Add this script to your /assets/Editor folder and you get the functionality by pressing f11 instead of play:
Of course, turn off asset refresh. This will give you an extra half hour of productivity per day.

using System;
using System.IO;
using System.Text;
//using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEditor;
using UnityEngine.Events;
using UnityEngine.Scripting;
using UnityEditorInternal;
using UnityEditor.Scripting;
using UnityEngine.TestTools;
using Unity.Profiling;
using UnityEditor.Profiling;
//using UnityEngine.SceneManagement;
using UnityEditor.VersionControl;
using UnityEngine.Profiling;
using System.Reflection;

public static class GlobalKeyEvents
{
// Exposed delegates to hook up your methods to them.
public static event Action<KeyCode, EventModifiers> GlobalKeyDown;
public static event Action<KeyCode, EventModifiers> GlobalKeyUp;
public static event Action AnyShortcutTriggered;

[InitializeOnLoadMethod]
private static void EditorInit()
{
RegisterToGlobalEventHandler();
RegisterToTriggeredAnyShortcut();
}

private static bool OnAnyShortcutTriggered()
{
AnyShortcutTriggered?.Invoke();
return true;
}

private static void RegisterToGlobalEventHandler()
{
FieldInfo info = typeof(EditorApplication).GetField(“globalEventHandler”, BindingFlags.Static | BindingFlags.NonPublic);
EditorApplication.CallbackFunction value = (EditorApplication.CallbackFunction)info.GetValue(null);
value += OnGlobalKeyPressed;
info.SetValue(null, value);
}

private static void RegisterToTriggeredAnyShortcut()
{
FieldInfo info = typeof(EditorApplication).GetField(“doPressedKeysTriggerAnyShortcut”, BindingFlags.Static | BindingFlags.NonPublic);
Func value = (Func)info.GetValue(null);
value += OnAnyShortcutTriggered;
info.SetValue(null, value);
}

private static void OnGlobalKeyPressed()
{

if (Event.current.type == EventType.KeyDown)
{

KeyCode key1 = Event.current.keyCode;

switch (key1)
{
case KeyCode.W:
//Debug.Log(“W!”);
break;
case KeyCode.F12:
case KeyCode.F11:
//case KeyCode.F10:
case KeyCode.F9:
{

if (Application.isPlaying)
{

#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;

#endif

}
else
{
UnityEditor.AssetDatabase.Refresh();
UnityEditor.EditorApplication.EnterPlaymode();
}

}
break;

}

GlobalKeyDown?.Invoke(Event.current.keyCode, Event.current.modifiers);
}
else if (Event.current.type == EventType.KeyUp)
{
GlobalKeyUp?.Invoke(Event.current.keyCode, Event.current.modifiers);
}
}
/*
public class DisableUnityMenuItems : Editor
{
[MenuItem(“MainMenu/Edit/Play”)]
static void aa()
{
}
}
*/
}

There’s still this rejected thing in Unity that randomly compiles, but the above code mitigates the worst.