Search Unity

Can I remove the class unity-button from a button?

Discussion in 'UI Toolkit' started by nixarn_, Sep 16, 2020.

  1. nixarn_

    nixarn_

    Joined:
    Jul 25, 2015
    Posts:
    42
    Hey,

    I'm new to UI Toolkit and did search for this problem I'm having without finding an answer.

    Creating a button adds some default styles. For example 'unity-button' comes with some style attributes I don't want, like a border, but how do I skip these? It doesn't seem possible to remove the class from a button.

    Overriding them would be one alternative. But, this I guess is a problem with the UI Builder in general. What if you want to override border-width from one class with 0? Now the default values are 0, although they don't apply, so you kinda need to re-type 0 to get it to apply the value, so values need to have a 'on/off' switch as well?

    Thanks

    EDIT: I looked at the uss & uxml, and seems much easier just to edit this directly (as I'm used to from web development). So here I just set the border width & radius to 0 and it's all good. So that's a workaround, I did get some strange null pointer reference at one point with very little code, submitted a bug report for that.
     
    Last edited: Sep 16, 2020
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    In C#, you can do
    Code (CSharp):
    1. button.RemoveFromClassList("classname")
     
  3. nixarn_

    nixarn_

    Joined:
    Jul 25, 2015
    Posts:
    42
    Thanks :)
     
  4. CoryMaklin

    CoryMaklin

    Joined:
    Oct 10, 2020
    Posts:
    29
    Is there a way to remove the class from the UI Builder? I don't see an X next to the default styles.
     
  5. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    It's not possible to remove default classes from the UI Builder since they are added from the elements themselves.
    What you can do instead is to provide your own class in your style sheet. For example you can declare a .unity-button rule in your style sheet to override the default styles.