Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Generating Class Names from uss files

Discussion in 'UI Toolkit' started by harry8525-unity, May 20, 2020.

  1. harry8525-unity

    harry8525-unity

    Joined:
    Nov 15, 2019
    Posts:
    5
    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:
    Code (CSharp):
    1. namespace FooControls
    2. {
    3.    public static class FooUSSClasses
    4.    {
    5.        public const string FooButtonClass = "fooButton";
    6.        public const string NameClass = "name";
    7.    }
    8. }
    Thoughts?
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    970
    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!
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    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.
     
    BinaryCats and mcoted3d like this.
  4. harry8525-unity

    harry8525-unity

    Joined:
    Nov 15, 2019
    Posts:
    5
    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.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    If you prefer to have your visual elements with strong typing, you can just build them from C# to begin with.
     
  6. harry8525-unity

    harry8525-unity

    Joined:
    Nov 15, 2019
    Posts:
    5
    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.
     
    Baste likes this.