Search Unity

Editor Compile in Background?

Discussion in 'Editor & General Support' started by larryPlayablStudios, Feb 11, 2019.

  1. larryPlayablStudios

    larryPlayablStudios

    Joined:
    May 15, 2018
    Posts:
    31
    Is there any way to have the editor recompile the project in the background on file change in an external IDE?

    Currently, I have to focus on the editor to kick off a recompilation.

    This would really help coding workflow.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    florianhanke likes this.
  3. larryPlayablStudios

    larryPlayablStudios

    Joined:
    May 15, 2018
    Posts:
    31
    Interesting, you're saying that I can access the running editor instance via command line and trigger an editor recompile? I don't want to do a full build. I just want to code in my external IDE and then have the editor already building or ideally ready to run when I tab back over to it.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Rider/Resharper's Unity plugin has a toggle for "automatically refresh assets in Unity", which does what you're asking. Maybe VS' plugin has something similar?
     
    ProbePLayer likes this.
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    No, I was referring to a command line build.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,412
    ariessanchezsulit likes this.
  7. larryPlayablStudios

    larryPlayablStudios

    Joined:
    May 15, 2018
    Posts:
    31
    Yeah, I'd noticed that as well. Looks like it launches a monitoring program that polls for file metadata updates (e.g. time updated) and then kicks off an asset refresh. Seems like a reasonable approach. I was just a bit nervous about running a pretty unknown quantity like this. I'm also on osx for now, and it utilizes an exe for the monitoring program. Could potentially be rebuilt for osx.

    Maybe I'll roll my own that uses an explicit system event sent from Visual Studio vs polling for file changes if I get around to it.
     
  8. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    356
    It seems VS2019 + Unity 2019.1 do that out of box
     
    iddqd, karl_jones, Peter77 and 3 others like this.
  9. llMarty

    llMarty

    Joined:
    Dec 14, 2014
    Posts:
    33
    That's great, however, it binds you to to Visual Studio, some developers are on Visual Studio Code or other IDEs that are less bloated, so they cannot enjoy this feature. Also, it will not work for other types of assets that you edit in another software.

    However, I have a solution to this that I will share, once my tests are complete.
     
    ihysoal, DebugLogWarning and vhman like this.
  10. charlie_sbg

    charlie_sbg

    Joined:
    Dec 18, 2016
    Posts:
    13
    Haha. Came here because I just installed VS 2019 Community (but with Unity 2018.3.x) and now I want it to STOP compiling in background. (I don't want it to start every time I save a file!). Any ideas?
     
    deeprest, JiongXia and laessnb like this.
  11. laessnb

    laessnb

    Joined:
    Jun 10, 2014
    Posts:
    101
    Exact same thing here. I need it to stop and am considering going back to 2017. It makes editing code so incredibly sluggish when it's constantly compiling and recompiling and re-recompiling. Is there any hack to disable it before we (hopefully) get an option to do so?
     
    Grey_Cloak likes this.
  12. Harriet92

    Harriet92

    Joined:
    Jul 2, 2015
    Posts:
    3
    Same here! I've just created a new folder in the Solution window and it vanished instantly (stopped being included) before I had time to insert any scripts there.
     
  13. Harriet92

    Harriet92

    Joined:
    Jul 2, 2015
    Posts:
    3
    @charlie_sbg @laeusnb I might have just found a solution! in Visual Studio go to Tools -> Options

    upload_2019-7-10_9-1-52.png

    It seems that it helped :) What a relief :)
     
    AdrienVR, cxode, sas67uss and 9 others like this.
  14. tosiabunio

    tosiabunio

    Joined:
    Jun 29, 2010
    Posts:
    115
    I actually love the compilation in the background, unfortunately very often, it doesn't want to stay in the background and the editor comes to the top, stopping me from writing the code. This is very annoying - is there a way to prevent the editor from popping up?
     
    Ruslank100 and Colin_MacLeod like this.
  15. lumanskiy

    lumanskiy

    Joined:
    Oct 23, 2015
    Posts:
    8
    This feature does not work properly for me. It keep reloading the project in Visual Studio, freezing VS for good 5-10 seconds.
     
    AdrienVR likes this.
  16. r137

    r137

    Joined:
    Mar 12, 2018
    Posts:
    47
    If you are using visual studio you can manually recompile in it and unity doesn't need to recompile again when you focus to editor. You just need to use ctrl+shift+b instead of ctrl+s :)
     
    Railon23 and cxode like this.
  17. flyingaudio

    flyingaudio

    Joined:
    Dec 3, 2010
    Posts:
    98
    Thank you, exactly what the team needed.
     
    deeprest likes this.
  18. eladleb4

    eladleb4

    Joined:
    Apr 25, 2016
    Posts:
    57
    @JeffDUnity3D is there any existing method or plans to enable background compiling for VS Code?
    Can the same method being used by Rider and the complete Visual Studio be used by VS Code?
     
    GameDesignLab, Bezoro and WermHat like this.
  19. WermHat

    WermHat

    Joined:
    Jul 13, 2012
    Posts:
    88
    Seconded!
     
  20. GameDesignLab

    GameDesignLab

    Joined:
    Jul 2, 2019
    Posts:
    1
    That would be a dream!
     
  21. NemoStein

    NemoStein

    Joined:
    Feb 17, 2014
    Posts:
    9
    Any news on this?
    I'd love to see a workaround to this issue...
     
    Egad_McDad and whiitehead like this.
  22. betaFlux

    betaFlux

    Joined:
    Jan 7, 2013
    Posts:
    112
    +1 for background compiling.
     
  23. etopian

    etopian

    Joined:
    Dec 11, 2018
    Posts:
    9
    +1
    Just switched to VSCode and would love a more generic support for this
     
  24. Chris_Payne_QS

    Chris_Payne_QS

    Joined:
    Jul 16, 2016
    Posts:
    84
    Thanks for pointer to new option, I've turned it off.
    It was causing Unity to reimport as soon as I tabbed, but the reimport prevented the editor from foregrounding.
    I just want a big fat progress bar so I can see when it's done TBH instead of clicking things to see if the editor responds yet...
     
  25. AndrewCzarnietzki

    AndrewCzarnietzki

    Joined:
    Jul 23, 2013
    Posts:
    189
    So I'm a little confused. New computer, new 2020.1.8f1 Unity install + Visual Studio 2019. I'd really like to try the compile in the background--but I have to give the Editor window focus before it refreshes the database and starts compiling.

    I have the Refresh Unity's Asset Database on Save set to true in the Unity Tools in VS. I also have Auto Refresh set to true in Unity preferences. Player Run In Background is also true, though I don't know if this affects the Editor itself.

    I've opened VS via Unity, and everything seems connected correctly. From some googling, the sense is that this should 'just work', and most of the posts are about how to turn it off. Yet on my end, I make a code change, save, and even if VS was a small window overtop of of the editor I need to enter the editor before it starts working and compiling.

    There is some mention of the Editor needing to be running in the background, but I don't know if this is different than the Player Settings game centric one and or how to set this.

    I am aware of building in visual studio, etc--I just want to try this new build-in-background-feature. Any idea how to turn it on?
     
    MassimoFrancesco, wing3s and wlwl2 like this.
  26. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Axel-F likes this.
  27. MassimoFrancesco

    MassimoFrancesco

    Joined:
    Jul 6, 2019
    Posts:
    44
    Have you found a solution in the meantime?

    Your post describes exactly what I'm experiencing. However it works in the beginning when freshly opening Unity 2020.2 + VS Community 16.7 and then suddenly just stops background compiling when saving in VS.

    It's driving me mad, because I really love theoretically not having to tab back forth all the time. Cause I can see the Editor changes on second monitor, while still having focused VS.

    I have checked all settings and have them exactly like you. However it just randomly stops behaving like it should, until I restart Unity.
     
    ivanngoc likes this.
  28. chaneya

    chaneya

    Joined:
    Jan 12, 2010
    Posts:
    416
    I'm seeing the same behavior as you. If I restart Unity and Visual Studio, I am able to compile in the background after I save in Visual Studio a few times and then it just stops working for no apparent reason. I really prefer to have Visual Studio compile in the background as soon as I save my code in Visual Studio. But ultimately I just wish it were consistent. I'm using Unity 2020.2.2 and VS 16.8.4
     
    Last edited: Jan 29, 2021
  29. m0guz

    m0guz

    Joined:
    Mar 4, 2015
    Posts:
    32
    @Elringus 's script works fine but also utilizes 10% of the CPU nonstop. Adding to a solution list; I have made a script that creates TCP Server and invokes AssetDatabase.Refresh() when client sends any message. I used Python/watchgod package to watch files, you can use Nodejs or other language.

    Use at your own risk. May not work if you are behind firewall. Tested on Windows 10/Unity 2019, Unity 2020
    https://gist.github.com/arcv/d1269e6480629a015dedd6a50621f79f
     
    Elringus likes this.
  30. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Thanks for sharing! I didn't initially notice that, as Unity's profiler not showing other threads, but indeed, the watcher thread seem to be in constant heavy usage. Not sure why this is happening, though. I believe watcher shouldn't consume anything while there are no tracked changes happening.

    I've noticed github for Unity also utilizes the watcher (https://github.com/github-for-unity...ub.Api/IO/Watches/FileSystemWatcherWrapper.cs) and it doesn't seem to impose that constant load. I wonder how they're achieving that...
     
  31. John_MSFT

    John_MSFT

    Microsoft Employee

    Joined:
    Feb 21, 2018
    Posts:
    118
    For those experiencing issues with VS and the Refresh Asset Database feature, please report it using Visual Studio > Help > Submit Feedback > Report a Problem. Include as much detail about the issue as possible so we can investigate. Thank you!
     
  32. ivanngoc

    ivanngoc

    Joined:
    Nov 9, 2018
    Posts:
    9
  33. pbritton

    pbritton

    Joined:
    Nov 14, 2016
    Posts:
    159
    I have encountered this problem all the time and I have placed my vote.
     
  34. Axel-F

    Axel-F

    Joined:
    Mar 20, 2009
    Posts:
    224
    I've altered the file-watcher script from above a little bit - and it is now consuming ~1,5% of my CPU. As long as the refresh is broken I'm totally fine with this "price".

    Simply put it in any "Editor" script directory in your project, add the full path to your scripts into "ScriptPath" and you are ready to go.

    Happy compiling.

    Code (CSharp):
    1.  
    2. using System.IO;
    3. using System.Threading;
    4. using UnityEditor;
    5.  
    6. [InitializeOnLoad]
    7. public class FileWatcher
    8. {
    9.     public static string ScriptPath = "PATH TO YOUR SCRIPTS";
    10.     public static bool SetRefresh;
    11.  
    12.     static FileWatcher()
    13.     {
    14.         ThreadPool.QueueUserWorkItem(MonitorDirectory, ScriptPath);
    15.         EditorApplication.update += OnUpdate;
    16.     }
    17.  
    18.     private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
    19.     {
    20.         SetRefresh = true;
    21.     }
    22.  
    23.     private static void MonitorDirectory(object obj)
    24.     {
    25.         string path = (string)obj;
    26.  
    27.         FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
    28.         fileSystemWatcher.Path = path;
    29.         fileSystemWatcher.IncludeSubdirectories = true;
    30.         fileSystemWatcher.Changed += FileSystemWatcher_Changed;
    31.         fileSystemWatcher.Created += FileSystemWatcher_Changed;
    32.         fileSystemWatcher.Renamed += FileSystemWatcher_Changed;
    33.         fileSystemWatcher.Deleted += FileSystemWatcher_Changed;
    34.         fileSystemWatcher.EnableRaisingEvents = true;
    35.     }
    36.  
    37.     private static void OnUpdate()
    38.     {
    39.         if (!SetRefresh) return;
    40.  
    41.         if (EditorApplication.isCompiling) return;
    42.         if (EditorApplication.isUpdating) return;
    43.  
    44.         AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport & ImportAssetOptions.ForceUpdate);
    45.         AssetDatabase.ImportAsset(ScriptPath, ImportAssetOptions.ForceSynchronousImport & ImportAssetOptions.ForceUpdate);
    46.         SetRefresh = false;
    47.     }
    48. }
    49.  
     

    Attached Files:

    Last edited: Apr 1, 2021
  35. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    I've found that the high CPU usage is due to cross-directory watching. If you disable `IncludeSubdirectories` and instead spawn a watcher instance per directory, there won't be any noticeable CPU load at all (with the same number of dirs/files watched). I've ended up doing that and additionally filtering watched dirs based on whether the target file types are present inside. This also requires re-spawning the watchers when a file of watched type is added/removed, which can be hooked with Unity's AssetProcessor (https://docs.unity3d.com/ScriptReference/AssetPostprocessor.html). Here is an example: https://gist.github.com/Elringus/7c0fcf0fdcaa3d0ffa4a5408209d8f10
     
    HelckOne, petey and Axel-F like this.
  36. chantey

    chantey

    Joined:
    Mar 5, 2017
    Posts:
    49
    This is awesome!
    I can now 'hot reload' my unit tests by adding the following code:

    Code (CSharp):
    1.     [UnityEditor.Callbacks.DidReloadScripts]
    2.     private static void OnScriptsReloaded()
    3.     {
    4.         Debug.Log("running all tests");
    5.         var testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();
    6.         var filter = new Filter()
    7.         {
    8.             testMode = TestMode.EditMode
    9.         };
    10.         testRunnerApi.Execute(new ExecutionSettings(filter));
    11.     }
     
  37. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    Baste, I can not find the aformentioned option within the vscode plugin

    upload_2022-10-10_11-56-10.png

    Using Unity 2020 lts and visual studio 2022.
     
  38. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    It's either VSCode or Visual Studio! Not the same program.

    Also I wrote that it was in Rider/Resharper, and that maybe it's in VS' plugin. I didn't know if it was or not.

    I haven't touched vanilla Visual Studio for anything serious for years - it's garbage bloatware. The only reason I have it installed is that some consoles requires it to be around. And before you ask me "what do you use instead" - I don't know of any free code editors for C# that are actually decent.
     
  39. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    My bad, totally meant Visual Studio, is that I normally use VScode for my other than Unity coding needs...

    BTW Visual studio 2022 works way faster than previous versions...
     
  40. HelckOne

    HelckOne

    Joined:
    Dec 21, 2020
    Posts:
    5
  41. Starburst999

    Starburst999

    Joined:
    May 8, 2017
    Posts:
    54
    It's not really compiling in the background tho.

    What would be really useful is having an option so the compilation is truly done in a background thread so I can use the editor while it is compiling and not have to wait for it when I want to edit a scene. Does that exists?
     
  42. m0guz

    m0guz

    Joined:
    Mar 4, 2015
    Posts:
    32
    Unfortunately no. Because once script compilation done, whole editor/inspectors also refreshed.

    I was in the impression that UIToolkit enabled editor would allow that kind of behaviour but it didn't. I use Playmaker as much as I can. Unity's Visual Scripting also has massive performance issue/spike during enabled state.