Generating Class Names from uss files

It would be great if we could have come C# constants generated from uss files.

This way we could have stronger typing on the class names when we use them in code. This way if you update the uss classnames and forget to update the other references you add in C#, you get a compile error.

Example Uss File:

.fooButton {

}

.fooButton .name {

}

Possible C# Generation:

namespace FooControls
{
   public static class FooUSSClasses
   {
       public const string FooButtonClass = "fooButton";
       public const string NameClass = "name";
   }
}

Thoughts?

Interesting suggestion. I’m not sure of the consequences this would have to the USS authoring workflow (changing a USS file would have to trigger a C# assembly rebuild), but I’ll forward this suggestion for our PM to review. Thanks!

Look at how the Input System does the same thing. The key there is that it’s customizable - the user can ask to generate it or not. The user also supplies the generated file name, path, and namespace.

2 Likes

Awesome thanks.

After making a few custom visualelements using templates it would be also great to get c# generation on the uxml files too since you almost always lookup some child element by class or id. I would prefer to understand any breaks at compile time ideally instead of run time in these cases. It would also be awesome if you could also keep the type information in the id case (e.g. It was a button vs a label i added the id too). Maybe generate a partial class or something.

If you prefer to have your visual elements with strong typing, you can just build them from C# to begin with.

That would work but using the uibuilder is so mich more productive for getting layout right (vs enter playmode in editor). So hoping for a nice middle ground.

1 Like