Search Unity

Feature Request Add class names to a VisualElement with an object initializer

Discussion in 'UI Toolkit' started by eidetic, Jun 18, 2019.

  1. eidetic

    eidetic

    Joined:
    Jun 27, 2016
    Posts:
    14
    At the moment, you can initialize a VisualElement and assign its members with an object initializer like so:
    Code (CSharp):
    1. Box myBox = new Box() { name="my-box", focusable="false" };
    That's pretty handy syntax as I don't have to take up three lines to initialise the box, then set it's name and properties.

    But it looks like you can't do this for class names. You still have to go:
    Code (CSharp):
    1. Box myBox = new Box();
    2. myBox.AddToClassList("box-class");
    It would be nice to be able to do something like this instead:
    Code (CSharp):
    1. Box myBox = new Box() { classList="box-class all-other-classes..." };
    Adding classes is something that I'd expect people to do pretty often before loading anything, and if you could do it in the object initializer, it means elements can be ready to go without needing to do some of the initialization in the parent class's constructor.