Hello!
Here is a tip for those who want to continue building their VisualElements into libraries (dll).
UI Toolkit now uses a code generator via UxmlElement to support UXML serialization. When compiling custom elements into DLLs, you might have encountered issues with elements not appearing in the UI Builder or failing to serialize correctly. This problem occurs because the generated serialization code isn’t included in the DLL. To resolve this, you need to run the UI Toolkit Source Generator during the DLL compilation process.
Steps
- Locate the Source Generator DLL:
Find the file namedUnity.UIToolkit.SourceGenerator.dll
. It’s typically located atData\Tools\Unity.SourceGenerators\Unity.UIToolkit.SourceGenerator.dll
within your Unity installation. - Add a Reference to Your
.csproj
File:
In your library project’s.csproj
file, add the following within an<ItemGroup>
:<ItemGroup> <Analyzer Include="PATH TO Unity.UIToolkit.SourceGenerator.dll"/> </ItemGroup>
- Compile Your Library: Compiling your library will now trigger the source generator, ensuring your custom UI Toolkit elements have the required UxmlSerializedData class generated.
Note: Its a good idea to rebuild the library against the version of Unity you are currently using as the generated code may change.
We are also working on updating our docs to include this information.