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

Question Reload only specific C# files or folders

Discussion in 'Scripting' started by theCodeHermit, Feb 17, 2023.

  1. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    I am having longer and longer reload times the more I work on my project.
    Is there a way to make a custom reload file. Lets say I create a few toggle checkboxes.

    * Assets
    * Ui_Scripts
    * Plugins
    * Character_Movement

    If all checkboxes are true then Unity will reload everything as usual. But if only the "Character_Movement" boolean is true, then only the scripts inside Character_Movement folder will be reloaded and nothing else will.

    Please please tell me this is possible to do. I am getting insane waiting a million years after changing a single character.

    PS:

    - I am aware there are a few mega threads related to the reload issue, but they dont have any solution and are full with people saying "I have the same problem" ...

    - I have tried one unity asset from their store. The one which lets you update stuff in update mode without reloading. I have discarded using it because it is using high CPU even outside play mode when it shouldn't be doing anything. So please do not suggest that.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,845
  3. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,845
    There's no need for toggling. Assemblies just organise your scripts into separate compiled .dll files. Unity only recompiles the code of assemblies you have edited, and any other assemblies with references to said altered assemblies.

    It's important to note that the script complication is a very small part of the domain reload process, and often takes milliseconds. There's a lot of other things that happen that Unity must do, that each take a portion of time themselves.

    I believe in the editor log file you can see a timing breakdown of reloads that happen in your project.
     
  5. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    hmmm "Reloading script assemblies" takes the longest. And "Compiling C# scripts" is few milliseconds.
    Does that mean that the assembly definitions wont help in the "Reloading script assemblies" part ?
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,845
    Script compilation is asynchronous already. Depending on your Unity version, "Reloading script assemblies" is just what the pop-up says even though it's doing an overarching domain reload; though in Unity 2021+, you get more descriptive messages as to what it's presently working on.

    Like I said, the editor log gives you a much more granular breakdown as to what it happening during a domain reload.

    And I'd also say assembly definitions are just good practice to keep your code well structured and modular.
     
  7. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    I dont get how ImportAndPostprocessOutOfDateAssets takes 12 sec when the biggest time in it is CompileScripts: 2422.077ms. Even if I combine every stat here I still wont get to 12 seconds.

    Eh It seems this wont be something easy to solve... And those other unity megathreads for the same problem are entirely useless. Just people complaining without providing anything constructive.


    Code (CSharp):
    1. Refresh completed in 12.924684 seconds.
    2. RefreshInfo: RefreshV2(AllowForceSynchronousImport) scanfilter:
    3. RefreshProfiler: Total: 12924.009ms
    4.     InvokeBeforeRefreshCallbacks: 0.328ms
    5.     ApplyChangesToAssetFolders: 0.087ms
    6.     Scan: 4.577ms
    7.     OnSourceAssetsModified: 0.645ms
    8.     InitializeImportedAssetsSnapshot: 27.078ms
    9.     GetAllGuidsForCategorization: 2.751ms
    10.     CategorizeAssets: 258.446ms
    11.     ImportAndPostprocessOutOfDateAssets: 12580.292ms (10045.064ms without children)
    12.         ImportManagerImport: 16.564ms (9.408ms without children)
    13.             ImportInProcess: 7.127ms
    14.             UpdateCategorizedAssets: 0.030ms
    15.         CompileScripts: 2422.077ms
    16.         PostProcessAllAssets: 29.337ms
    17.         ReloadImportedAssets: 0.263ms
    18.         EnsureUptoDateAssetsAreRegisteredWithGuidPM: 5.279ms
    19.         InitializingProgressBar: 0.004ms
    20.         PostProcessAllAssetNotificationsAddChangedAssets: 1.959ms
    21.         OnDemandSchedulerStart: 1.042ms
    22.         RestoreLoadedAssetsState: 16.139ms
    23.     UpdateImportedAssetsSnapshot: 42.564ms
    24.     ReloadSourceAssets: 16.874ms
    25.     UnloadImportedAssets: 1.921ms
    26.     Hotreload: 11.235ms
    27.     FixTempGuids: 0.005ms
    28.     GatherAllCurrentPrimaryArtifactRevisions: 0.856ms
    29.     UnloadStreamsBegin: 1.134ms
    30.     LoadedImportedAssetsSnapshotReleaseGCHandles: 1.802ms
    31.     GetLoadedSourceAssetsSnapshot: 28.326ms
    32.     PersistCurrentRevisions: 0.853ms
    33.     UnloadStreamsEnd: 0.101ms
    34.     GenerateScriptTypeHashes: 19.261ms
    35.     Untracked: -13.302ms
    36. [MODES] ModeService[default].RefreshMenus
    37. [MODES] ModeService[default].UpdateModeMenus
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,845
    ImportAndPostprocessOutOfDateAssets
    is probably doing it's own processes on top of the breakdown of its child processes.

    The biggest single improvement I've seen to domain reloads is to remove all unnecessary Unity packages from your project, and only add any back in when required.

    There's a lot of heavyweight packages, like Visual Scripting, that you can definitely drop if you don't ever plan to use them. That will improve domain reloads greatly. You also don't need all three IDE packages when you only ever use one.

    A fresh URP project has a domain reload of about 1-2 seconds for me. It's loaded with tons of packages I'm never going to use. Once I strip everything out to just URP, Visual Studio integration, Input System, Addressables and my own core code library package, and Odin Inspector, reloads are instantaneous.

    The next best option, is, of course, better hardware. But that's not exactly a popular one.
     
  9. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    Hmm maybe I could give it a shot removing some unused stuff... How exactly do you get rid of those. Do you remove specific folders from the project view or in other way ?
    Though I guess that if on an empty project it takes 2-3 sec then that is also the maximum amount of time that can be shaved off by removing the unnecessary stuff.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,845
    You remove Unity packages with the package manager, naturally.
     
  11. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    Hahah, naturally :D
    Sometimes I do feel like a caveman figuring out what the best shape for the wheel is. All the while at the next valley over there are people living in skyscrapers.
    Anyway thanks for all the help, you did give me lots of things to consider.
     
  12. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Note that if you have tons of serialized data, a "domain reload" has to serialize, reload and deserialize all those objects. So if you have a lot of complex serialized data loaded all the time, the reload time would stay quite high. Of course it's difficult to give clear advice here as each project is unique and can be quite different.
     
  13. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    I was wondering why did the reload time got progressively bigger when all I was doing at the time was make serialized classes. I guess that could explain it, but I dont get how a bunch of classes could increase reload time with 2-3 secs.
    Though admittedly I don't know very much about the subject so that's on me...
    I am really curious if Unreal engine has all those issues. Maybe I chose the wrong engine when I started learning game dev.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,845
    They're not talking about the classes themselves, but the all the potential serialised data in assets throughout your project.

    Larger numbers of serialised classes could contribute to this, and during a domain reload Unity will walk through the serialised hierarchy of all assets (including private/protected fields not decorated with [NonSerialized]) to main state in some cases, and reset state in others.

    Do you have very large numbers of scriptable objects/prefabs with large amounts of serialised data?

    For this reason some projects elect to serialise data in forms Unity can't serialise, such as a binary asset, and it won't be part of the reserialisation process. This is for extreme cases, or in instances where you want to manage data in a large scale outside of Unity.

    Both games engines (all game engines really) are just tools that you need to learn. That takes time. I didn't know any of this stuff two years ago... and now I do.
     
  15. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
  16. theCodeHermit

    theCodeHermit

    Joined:
    Sep 20, 2018
    Posts:
    39
    I have lots of environment prefabs and each one has a script, though there is nothing serialized there. It does use some of Odin inspectors code though. But in any case I will try this Hot reload plugin @eterlan suggested. It does seem interesting, but I am having problems with C# versions. Will be asking for that in another topic