Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Can I stop auto compile after edit, create or remove a script?

Discussion in 'Editor & General Support' started by banksazero, Apr 15, 2022.

  1. banksazero

    banksazero

    Joined:
    May 8, 2015
    Posts:
    77
    I've unchecked both directory monitoring and auto-refresh but it still compiles every time I edit, create, or remove a script. How to disable Unity to auto-reload?

    PS.I try both 2021 and 2020 LTS
     
  2. SiniKettu_

    SiniKettu_

    Joined:
    Aug 30, 2017
    Posts:
    19
    I've extracted this from this site :
    https://support.unity.com/hc/en-us/...op-automatic-assembly-compilation-from-script

    I didn't try it but it comes from an unity support page so I guess we can rely on it.
     
    banksazero and ruzgames like this.
  3. banksazero

    banksazero

    Joined:
    May 8, 2015
    Posts:
    77
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Unity has to recompile when you make/remove a script. You're introducing/removing a class into one of its assemblies, however small it might be.
     
    banksazero likes this.
  5. banksazero

    banksazero

    Joined:
    May 8, 2015
    Posts:
    77
    So Unity will recompile if I remove/create a new script and even disable auto-refresh? and I can't disable this function?

    Thx you so much
     
  6. QuebecDev

    QuebecDev

    Joined:
    May 9, 2020
    Posts:
    37
    But still, there should be a way to make it reload only when I hit Ctrl+R.

    It's no different than adding/modifying a class in an existing script but this.. we can disable auto-refresh!

    I think Unity just forget to pass that removing/adding files and folders function to the auto-refresh bool. :D

    But one thing I know is unity doesn't have to make it reload live.
     
  7. QuebecDev

    QuebecDev

    Joined:
    May 9, 2020
    Posts:
    37
  8. QuebecDev

    QuebecDev

    Joined:
    May 9, 2020
    Posts:
    37
    This work for me.

    1) Add the function below to a 'public static class'

    2) I have removed the shortcut Ctrl+R from Edit/Shortcuts/...

    Because I have added this Attribute [MenuItem("Editor/Refresh %r")] this trigger the function on Ctrl+R. And I did not want the 'script reload' to be double tap.

    3) Restart the editor, because private static void Initialize() won't be triggered the first time.

    4) Its done. Scripts will not reload each time you create/delete, there is just a little loading wheel that appears for 0.25sec.

    Tip: If you want to move a lot of .cs, assemblies, etc you should close Visual studio.

    Code (CSharp):
    1.         [MenuItem("Sklapps/Editor/Auto Refresh")]
    2.         private static void AutoRefreshToggle()
    3.         {
    4.             var status = EditorPrefs.GetInt("kAutoRefresh");
    5.  
    6.             EditorPrefs.SetInt("kAutoRefresh", status == 1 ? 0 : 1);
    7.         }
    8.  
    9.         [MenuItem("Sklapps/Editor/Auto Refresh", true)]
    10.         private static bool AutoRefreshToggleValidation()
    11.         {
    12.             var status = EditorPrefs.GetInt("kAutoRefresh");
    13.  
    14.             Menu.SetChecked("Sklapps/Editor/Auto Refresh", status == 1);
    15.  
    16.             return true;
    17.         }
    18.  
    19.         [MenuItem("Sklapps/Editor/Refresh %r")]
    20.         private static void Refresh()
    21.         {
    22.             Debug.Log("Request script reload.");
    23.  
    24.             EditorApplication.UnlockReloadAssemblies();
    25.            
    26.             AssetDatabase.Refresh();
    27.  
    28.             EditorUtility.RequestScriptReload();
    29.         }
    30.  
    31.         [InitializeOnLoadMethod]
    32.         private static void Initialize()
    33.         {
    34.             Debug.Log("Script realoded!");
    35.            
    36.             AssetDatabase.SaveAssets();
    37.  
    38.             EditorApplication.LockReloadAssemblies();
    39.         }
     
    Last edited: Jan 2, 2023
    Zarpyk and PixelPockets like this.
  9. firestoke

    firestoke

    Joined:
    Oct 21, 2015
    Posts:
    13
    It works for me. Thanks! :)
     
  10. navratilvictori

    navratilvictori

    Joined:
    Feb 21, 2021
    Posts:
    4
    If with auto-refresh disabled, your scripts still continue compiling I might have found a solution (in my case). I have spent a few hours trying to figure this out and hopefully this will help someone using other Unity versions

    I am using Unity 2022.2 and Unity 2021.3 (LTS) with the latest version at the time of writing this comment.Auto refresh was off but I've seen that 2 different projects had different versions of Burst. And in both cases Burst had the "chain link" icon (installed as a dependency)

    The solution in my case was: I reinstalled Burst by name (Packager Manager -> "+" sign -> "Add packager by name.. -> added "com.unity.burst"). Installed version is 1.8.4.This seems to have solved it. Some settings might have been reinitialized.
     
  11. HawkAngel

    HawkAngel

    Joined:
    Oct 7, 2022
    Posts:
    9
    Must be nice to have this work, even after following the steps navratilvictori suggested it still doesn't work at all.
     
  12. Zarpyk

    Zarpyk

    Joined:
    Mar 17, 2019
    Posts:
    3
    You also can remove the
    EditorUtility.RequestScriptReload();
    adding
    EditorApplication.projectChanged
    event and/or
    FileSystemWatcher
    , this will prevent Unity reload when you haven't made changes (I don't test it, but I think is possible using only
    FileSystemWatcher
    with more
    NotifyFilter
    to replace
    EditorApplication.projectChanged
    )

    For better performance, I limit the file to "*.cs;*.asmdef;*.inputactions" extensions, but I don't know if some other asset need recompile and if you need more performance, you can set more specific path (For example /Assets/Scripts)

    (Also I add auto refresh before enter Play Mode, and make AutoRefresh enable by default)

    (I have this script on the Editor folder and with the Auto Refresh disabled)
    Code (CSharp):
    1.  
    2. using System.IO;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. namespace Editor {
    7.     [InitializeOnLoad]
    8.     public static class PlayRefresh {
    9.         private static bool _projectChanged;
    10.         private static readonly FileSystemWatcher _fileSystemWatcher;
    11.  
    12.         static PlayRefresh() {
    13.             EditorApplication.playModeStateChanged += Refresh;
    14.             EditorApplication.projectChanged += ProjectChanged;
    15.  
    16.             _fileSystemWatcher = new FileSystemWatcher();
    17.             _fileSystemWatcher.Path = Application.dataPath;
    18.             _fileSystemWatcher.IncludeSubdirectories = true;
    19.             _fileSystemWatcher.Filter = "*.cs;*.asmdef;*.inputactions";
    20.             _fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
    21.  
    22.             _fileSystemWatcher.Changed += FileSystemChanged;
    23.  
    24.             _fileSystemWatcher.EnableRaisingEvents = true;
    25.         }
    26.  
    27.         private static void Refresh(PlayModeStateChange state) {
    28.             if (state == PlayModeStateChange.ExitingEditMode) {
    29.                 RefreshAssets();
    30.             }
    31.         }
    32.  
    33.         private static void ProjectChanged() {
    34.             _projectChanged = true;
    35.         }
    36.  
    37.         private static void FileSystemChanged(object sender, FileSystemEventArgs e) {
    38.             _projectChanged = true;
    39.         }
    40.  
    41.         [MenuItem("Tools/Auto Refresh")]
    42.         private static void AutoRefreshToggle() {
    43.             int status = EditorPrefs.GetInt("kAutoRefresh");
    44.             int newStatus = status == 1 ? 0 : 1;
    45.  
    46.             EditorPrefs.SetInt("kAutoRefresh", newStatus);
    47.  
    48.             if (newStatus == 1) EditorApplication.UnlockReloadAssemblies();
    49.             else EditorApplication.LockReloadAssemblies();
    50.  
    51.             CheckAutoRefresh();
    52.         }
    53.  
    54.         [MenuItem("Tools/Auto Refresh", true)]
    55.         private static bool AutoRefreshToggleValidation() {
    56.             int status = EditorPrefs.GetInt("kAutoRefresh");
    57.             Menu.SetChecked("Tools/Auto Refresh", status == 1);
    58.             return true;
    59.         }
    60.  
    61.         [MenuItem("Tools/Refresh %r")]
    62.         private static void Refresh() {
    63.             RefreshAssets();
    64.         }
    65.  
    66.         // This will executed after refresh
    67.         [InitializeOnLoadMethod]
    68.         private static void Initialize() {
    69.             if (EditorPrefs.HasKey("kAutoRefresh") == false) {
    70.                 EditorPrefs.SetInt("kAutoRefresh", 1);
    71.                 Menu.SetChecked("Tools/Auto Refresh", true);
    72.             }
    73.             int status = EditorPrefs.GetInt("kAutoRefresh");
    74.             if (status == 1) EditorApplication.UnlockReloadAssemblies();
    75.             else EditorApplication.LockReloadAssemblies();
    76.             CheckAutoRefresh();
    77.         }
    78.  
    79.         private static void RefreshAssets() {
    80.             Debug.Log($"Request refresh assets. (Project changed: {_projectChanged})");
    81.             if (!_projectChanged) return;
    82.             EditorApplication.UnlockReloadAssemblies();
    83.             AssetDatabase.Refresh();
    84.         }
    85.        
    86.         private static void CheckAutoRefresh() {
    87.             int status = EditorPrefs.GetInt("kAutoRefresh");
    88.             _projectChanged = status == 1; // If auto refresh is on, project changed is always true
    89.         }
    90.     }
    91. }
    92.  
     
    Last edited: Oct 8, 2023