I am a mobile game developer. This role demands speed. Most of the code, most of the time is throwaway (although it does not have to be, a topic for another day). In most cases I see myself using a lot of third assets to speed things up. I also have a lot of my own code that I keep in every project which helps me be efficient. In most projects, I don’t even create a new project. I just copy the last project files, delete the assets that I won’t surely need, and start from there. It comes with baggage. It means a bigger project size but it also means everything is available right there to use and reuse which means much faster speed which for me means that the increased project size is worth it. Speed is money. Unfortunately, there is one more baggage. It is the compile-time issue
Ideally, every asset should have at least 2 assembly definitions. One for Editor scripts and the other for Runtime scripts. But it is not an ideal world. Some assets don’t come with it. If you go to your Project\Library\ScriptAssemblies you will see a dll named “Assembly-CSharp.dll”. This is your project assembly. All scripts that are not part of any custom asmdef are automatically part of this assembly. So all of the scripts that are not under any asmdef in your third-party assets are automatically part of project assembly. This is a really bad thing. I’ll explain why.
When you make a change in a script, Unity finds which assembly is the script under, and then it recompiles the whole assembly. So if you are working on your project and you edit your script (considering you are not using asmdef) it will recompile the “Assembly-CSharp.dll”. And having assets without any assembly will cause all those assets to recompile too. Thus, increasing compile time. On top of that, if you have your own framework with lots of reusable scripts and without any asmdef, it will give you a huge blow in compile-time.
Compile-time can vary from 1 second to even a few minutes. So keeping it as low as possible is a really good idea because God knows how many times we swap between writing code and hitting the play button within Unity. To best reduce compile time the best way is to use asmdef even within project-specific scripts. But I don’t do that. Because for a prototype I find it unnecessary. But I do divide my assembly as much as I can by keeping the code that I don’t change often separately and by keeping third party assets separately which code I usually never touch.
Here are 2 quick steps using which I reduced my assembly size (“Assembly-CSharp.dll”) from around 3.5Mb to less than 100Kb and reduced compile time from around 30s to 4-6s.
- Create a folder under “Assets” named “Standard Assets” and drop all third-party assets inside that. This way unity creates a separate assembly named ‘Assembly-CSharp-firstpass’ for the third party assets and it won’t recompile with your code change. This saves most of the compile time. It can be even reduced by moving all Editor folders of third-party assets to a new Editor folder within Standard Assets. Then by doing the same for all Runtime scripts. But it gets painful when the assets require an update.
- Use asmdef for your reused scripts or your own framework. Keep Editor scripts and Runtime scripts in separate assemblies.
This reduces the iteration time (swapping between code editor and Unity Play mode) thus increases development speed.
Before ending this article, there’s one other trick to reduce the iteration time by even more. Turn on Project Settings -> Editor -> Enter Play Mode Options. When you turn it on Unity will not reload the Domain or the Scene, thus it will go to play mode at lightning-quick speed. But it does not go well with static scripts so beware of that