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. Dismiss Notice

Question Custom control's code defined style doesn't get affected by custom styles

Discussion in 'UI Toolkit' started by Kemorno, Jun 5, 2023.

  1. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    I'm making custom controls for me (and some other people) to use, and some of them i want to have a suggested style, (ie: a Label to have flex-grow by default) by i want to make it customizeable, just like Unity's controls. But every style that i edit via code it seems to be permanent, as shown on the example below. How would i go for allowing the uss selector to have priority over the code's style.
    Im guessing it has something to do with
    CustomStyleResolvedEvent
    but I don't know how it would be done.

    Code (CSharp):
    1.  
    2. public class Foo : VisualElement
    3. {
    4.     public Foo()
    5.     {
    6.         VisualElement visualElement = new VisualElement();
    7.         visualElement.style.minHeight = 5;
    8.         visualElement.AddToClassList("TestClass");
    9.         Add(visualElement);
    10.     }
    11.  
    12.     public new class UxmlFactory : UxmlFactory<Foo, UxmlTraits> { }
    13. }

    As you can see, i set TestClass to have a minHeight of 100 but its not being applied
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
  3. Kemorno

    Kemorno

    Joined:
    Apr 5, 2017
    Posts:
    18
    makes sense, i'll probably make a Style and set it as inherited on the Panel Settings

    Thx!