Search Unity

UIElements Button

Discussion in 'UIElements' started by vincismurf, Jun 10, 2019.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    157
    I am trying to wrap my head around this concept. So I was wondering, if I make a button like so

    Code (CSharp):
    1.  
    2. VisualElement root = rootVisualElement;
    3. StyleSheet styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Scripts/Editor/UIElements/ShaderSelector.uss");
    4. VisualElement but = new Button();
    5. but.styleSheets.Add(styleSheet);
    6. but.name = "fred";
    7. root.Add(but);
    8.  
    Is there anyway to add Text to the Button via code? ie Do I have to use .UXML to put in text on the button?

    Please clarify.
     
  2. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    157
    Solution

    Code (CSharp):
    1.  
    2. Button b = new Button();
    3. b.text = "hello";
    4. VisualElement but = b;
    5. but.styleSheets.Add(styleSheet);
    6. but.name = "fred";
    7. root.Add(but);
    8.