Reducing C compile time# when a Unity project grows
Minh Khoa
Author

Fixing how item counts are displayed in InventoryPanel does the combat logic need to be recompiled? If both are in Assembly-CSharp, Unity must recompile the assembly that contains both. Splitting them into two folders UI and Combat makes code easier to find, but only when there is an Assembly Definition (.asmdef)does Unity have separate compilation boundaries. Unity Manual — Introduction to assemblies
1. Measure one code change the team makes every day
Wait for the Editor to finish importing, keep the build target unchanged, then edit one line in the body of the function InventoryPanel.Refresh(). Return to Unity in Edit Mode, open the window menu Console → Open Editor Log and find the corresponding Asset Pipeline Refresh run. Record three numbers: C compile time#, domain reload time and the total time from saving until you can test the new code. Unity Manual — Log files
Suppose compilation takes 2 seconds, while reload takes 8 seconds. Even if compile time is cut in half, the team would only save about 1 second in that phase. In this case, I would use Editor Iteration Profiler to find which Editor tool is running again after each reload, for example a class that [InitializeOnLoad] scans all types to build a cache. Turning off Reload Domain when entering Play Mode only affects what happens when you press Play. Unity still reloads code when it detects script changes. Editor Iteration Profiler · Unity Manual — Domain reload
2. Split Inventory by dependency direction
If compilation accounts for most of the time, I would try splitting one feature first. With Inventory, the item management logic, the display screen, and the catalog editing tool can be organized as follows:
Assets/Game/Inventory/
├── Runtime/ Game.Inventory.Runtime.asmdef ItemStack, InventoryModel
├── UI/ Game.Inventory.UI.asmdef InventoryPanel, SlotView
└── Editor/ Game.Inventory.Editor.asmdef ItemCatalogWindow
In each folder, create an asset with Assets → Create → Scripting → Assembly Definition. In Assembly Definition References, let UI and Editor reference each other Runtime. Runtime manage item data and do not need to know which panel is displaying it. For Game.Inventory.Editor, uncheck Any Platform and select only Editor so the catalog editing tool stays outside the Player build. Unity Manual — Creating assembly assets
With the three assemblies above, the compilation scope depends on the type of change:
| Change | Compilation scope in the example |
|---|---|
Fix InventoryPanel the number format | Only UI needs to be recompiled. |
Change the algorithm in the body of the function InventoryModel.GetQuantity()while keeping API | Runtime recompiled. Unity can reuse the results of UI and Editor. |
Change the return type of GetQuantity()since both the panel and the catalog tool call this function | Runtime, UI and Editor needs to be recompiled. The two call sites must also be compatible with the new type. |
This is why it is better to keep API that many modules use relatively stable. With incremental compilation, changing the implementation details inside a DLL can have a smaller scope than changing API that DLL provides. Unity Engineering — Incremental script compilation
3. Gradually split an existing project
Suppose InventoryPanel still calls PlayerInventory in Assembly-CSharp. If you create .asmdef for UI first, the panel will no longer be able to access that type. Unity does not allow an assembly to define a reverse reference to Assembly-CSharp. In this case, split InventoryModel and the data types it needs into Game.Inventory.Runtime first. The old code can still call into Runtime while Auto Referenced is enabled. After you remove the dependency on the old code, then split UI. Unity Manual — Referencing assemblies
For an independent Editor tool, you can follow a simpler order. If the tool does not use any type in Assembly-CSharp and there is no code in the default assemblies that needs to call it, split the tool first, then turn off Auto Referenced. This setting controls automatic references from the default assemblies to tool. Unity Manual — Assembly Definition Inspector
After Unity finishes the first compilation with the new structure, repeat the test InventoryPanel.Refresh() several times on the same machine. I will extend this split to the next feature when both of these are true: changes UI have been isolated as expected, and the time from saving to actually testing has been reduced.