This troubleshooting guide helps you resolve issues when compiling custom controls into DLLs.
When you compile custom controls into DLLs, you might encounter the following issues:
UI Toolkit uses the UxmlElement code generator to support UXML serialization. However, when compiling custom controls into DLLs, the generated serialization code isn’t included by default, causing issues with element visibility and serialization.
To resolve this issue, run the UI Toolkit source generator (Unity.UIToolkit.SourceGenerator.dll
) during the DLL compilation process.
Find the source generator file in your Unity installation. It’s typically located at: <Unity Installation Path>\Data\Tools\Unity.SourceGenerators\Unity.UIToolkit.SourceGenerator.dll
.
Add the source generator as an analyzer in your library project’s .csproj
file within an <ItemGroup>
:
<ItemGroup>
<Analyzer Include="path\to\Unity.UIToolkit.SourceGenerator.dll" />
</ItemGroup>
Compile your library as usual. This triggers the source generator, which generates the required UxmlSerializedData class for your custom control.
Tip: Always rebuild your library against the Unity version you’re using because the generated code might vary between versions.