Search Unity

Add/Remove Enable/Disable USS class best practice?

Discussion in 'UI Toolkit' started by BinaryCats, Jan 7, 2020.

  1. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Hello,

    I frequently want to adjust the styling on ui elements (e.g text styling or toggle display:none)

    I have noticed that you can enable/disable classes and also add/remove classes from an element. They both have the same result, however the former means the class is added in the UXML and needs to be disabled in set up (unless it's possible to add a disabled class to the element?).

    I can imagine add/remove alters the UQUERY results, but this is not a factor my editor as I do not use these classes as qualifiers.

    What is the best practice for toggling class' on/off?

    Another frequent thing I come up against is I have no way of knowing if the class exists on the element, or if it needs adding to the element.

    Thanks in advance!
     
    MiguelOscarGarcia likes this.
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    229
    Hi BinaryCats,

    The EnableInClassList(string className, bool enable) function is basically a wrapper around AddToClassList() and RemoveFromClassList() based on the enable parameter. That's why they both have the same result.

    Use ToggleInClassList("className")

    Use ClassListContains("className")
     
    MiguelOscarGarcia and BinaryCats like this.