Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is there a way to create a style collection via C# (without using a .uss file)?

Discussion in 'UI Toolkit' started by MostHated, Sep 4, 2020.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Hello,
    I am wondering if there is a way to create a collection of style data in C# that can then be applied to a VisualElements style property?

    Essentially, I would love to be able to do something like this below:
    Code (CSharp):
    1. private IStyle styleData = new IStyle
    2. {
    3.     flexDirection = FlexDirection.Row,
    4.     justifyContent = Justify.FlexStart,
    5.     alignSelf = Align.Stretch
    6. };
    7.  
    8. VisualElement element = new VisualElement().style = styleData;
    Unfortunately, VisualElement().style has no setter for this type of assignment and you can't actually create a new IStyle in this manner, though, attempting to do so in Rider (in the manner used above) the property names are recognized and the autocomplete works, etc. So it feels so close, yet so far away.



    I am just wondering if there is something currently implemented that I am unaware of that can be used to accomplish this? I saw under UIElements.Experimental there was a StyleValues, but it only contains a limited subset of the available style types, missing things like FlexDirection, Justify, etc, really, all the ones I wanted to use.

    Thanks,
    -MH