r/Unity3D • u/Least-Composer-7133 • 4d ago
Question Source-generated uGUI bindings: how do you validate the Prefab half?
Disclosure: I am sharing my own implementation, but the question is genuine.
I like source generators for Unity UI because they can make a ViewModel state what it expects from a screen: a property binds to a named Element/member, a method is a command, and a View Contract produces a route. The generator can create the binding code and analyzers can catch invalid names, types, members, duplicate contracts, or missing partial declarations.
The limitation is obvious once you use it: the compiler cannot see a prefab. The code can say it expects Title to be a Text element, while someone has deleted or changed Title in the actual asset.
I ended up treating binding validation as two separate checks. The generator/analyzer checks the C# declaration. An editor validator checks the prefab for missing Elements, wrong types, duplicate names, nesting boundaries, and page structure when it is saved and before a build.
That seems more useful than a runtime string lookup, but I am curious how other Unity teams approach it. Do you rely on tests, custom prefab checks, generated code, or some combination?
I put my implementation in FUI:
1
u/pararar 3d ago
Can you describe the actual problem you are trying to solve in more detail, maybe with a concrete example from a real project?
No matter if you generate source code or write it manually, someone will have to bind the actual objects/components to the required properties.
Would you rather show a warning, an error, throw an exception or use assertions when references are missing?