When building for android, or exporting as a package all my UI files, if a script is being used inside a UXML hierarchy it is replaced by a “Label” visual element (and any functionality brought by that script is gone).
It works:
Exported as a package into new project.
Unity version: Unity 2121.2.9f1
Using builtin version of UI toolkit and UI builder
I use 2 scripts in my UI. One of them is the MenuManager solution as seen in this tutorial:
This has been of great help since I could do all I need to do in my UI.
The other:
Also I’ve implemented a script that keeps aspect ratio of a visual element
(I’ve improved my solution by affecting width and not padding)
None of them is kept in place when making a build for android, or even exporting a package (the script itself is kept inside the package, but it disappears from the UXML hierarchy)
Is it an Editor configure issue with UI toolkit? Any ideas?
When building for android, before 2022.1.0a8, you need to add the [Preserve] annotation to your class. IL2CPP has a very aggressive code stripping, and the system uses some form of reflection to resolve factories at runtime.
Starting with 2022.1.0a8, there should be a mechanism to help with that. As per the releases notes : UI Toolkit: UXML factory types are now preserved automatically
I personally did not test the behavior of the changes, but is seems we never strip any UXML factories now, even when they are not used.
Can you test the Android build with [preserve] to confirm that this was the issue?
We can look into the “export package” after that, but I suspect the script file was simply not packaged when exporting. The error would be the same, so I want to separate the root cause of both in the first place.
Thank you very much for this suggestion. It was difficult to solve this issue and I think the solution was part lost references, and also setting the preserve setting that you mentioned.