Unity Assembly Definitions and Editor Folders

Unity added Assembly Definition files in Unity 2017.3 these were handy for packaging up code. In Unity 2018.1 they now work the way you would expect. Helping improve compile times, but they can still be tricky to hook up with Editor folders.

Unity have done a few blog posts on what assembly definition files are, Unity 2018.1 now ensures they are compiled on startup before any other scripts (Assembly-CSharp.dll, etc.). This lets the assembly definitions be built and loaded regardless of other compile errors in the project.

littlelot assembly definition

LittleLot Assembly Definition

If you’ve tried to add assembly definition files to your project you may have came across these errors when you try to build your project:

Assets/LittleLot/Core/Editor/SceneDrawer.cs(1,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing an assembly reference?

These errors happen because the assembly definition created in Assets/LittleLot/ compiles all files and folders into LittleLot.dll. This is fine when we are in the editor as we have a reference to UnityEditor however, when you try to build the project the scripts inside the Editor folder get compiled into your build but it does not have access to the UnityEditor assembly.

Normally Unity will split your code into Assembly-CSharp.dll and Assembly-CSharp-Editor.dll, we just have to imitate this behaviour with our assembly definitions. For each Editor folder in our LittleLot folder we add an assembly definition file and set it to only be included in the Editor.

littlelot core editor assembly definition

LittleLot Core Editor Assembly Definition

Even with this we still get errors, the scripts in our LittleLot.Core.Editor.dll can’t find anything that is referenced in the original LittleLot.dll, this is most obvious with custom inspectors or property drawers.

Assets/LittleLot/Core/Editor/SceneDrawer.cs(17,31): error CS0246: The type or namespace name `SceneAttribute' could not be found. Are you missing an assembly reference?

This happens because there is no link between the two .dll’s so they have no way of knowing about each other, this is where the references section in our assembly definition comes into effect. In our Editor assembly definition we just need to add a reference to the parent assembly definition this will allow both of them to reference each other correctly, whilst still allowing us to strip all Editor code from our builds.

littlelot core editor assembly definition

LittleLot Core Editor Assembly Definition - With Reference

Now we have assembly definitions that work nicely with Editor folders, and our LittleLot folder will only be recompiled if we change anything inside the scripts or change platform.

Related

comments powered by Disqus