Search Unity

Custom VisualElement custom USS Attribute

Discussion in 'UI Toolkit' started by svobodajak, Aug 6, 2020.

  1. svobodajak

    svobodajak

    Joined:
    Jun 27, 2017
    Posts:
    11
    Hello, I've been wondering whether it is possible to implement a custom USS attribute, that we would be able to use in an .uss style sheet? Or whether something like this is planned for the future?

    Right now I have a custom VisualElement (eg LineElement) and I would like to define a custom USS attribute (eg line-color), so that I would be able to use that attribute in the style sheet (eg .line { line-color: rgb(...) }).

    I am basically looking for a similar functionality that is now available for custom UXML attributes via the UXMLTraits and TypedUxmlAttributeDescription<T> combo.
     
  2. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
  3. svobodajak

    svobodajak

    Joined:
    Jun 27, 2017
    Posts:
    11
    @BinaryCats I will check it out, but just in case: my question is about custom USS attributes (not custom UXML attributes)
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    This is what you're looking for:
    https://docs.unity3d.com/Packages/c...gine.UIElements.CustomStyleResolvedEvent.html

    Here's an example I randomly extracted from...somewhere:

    Code (CSharp):
    1. class MyElement : VisualElement
    2. {
    3.     static readonly CustomStyleProperty<int> k_CellSizeProperty = new CustomStyleProperty<int>("--cell-size");
    4.     static readonly CustomStyleProperty<Color> k_OddCellColorProperty = new CustomStyleProperty<Color>("--odd-cell-color");
    5.     static readonly CustomStyleProperty<Color> k_EvenCellColorProperty = new CustomStyleProperty<Color>("--even-cell-color");
    6.  
    7.     int m_CellSize;
    8.     Color m_OddCellColor;
    9.     Color m_EvenCellColor;
    10.  
    11.     public MyElement()
    12.     {
    13.         RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
    14.     }
    15.  
    16.     void OnCustomStyleResolved(CustomStyleResolvedEvent e)
    17.     {
    18.         if (e.customStyle.TryGetValue(k_CellSizeProperty, out var cellSizeProperty))
    19.             m_CellSize = cellSizeProperty;
    20.  
    21.         if (e.customStyle.TryGetValue(k_OddCellColorProperty, out var oddCellColor))
    22.             m_OddCellColor = oddCellColor;
    23.  
    24.         if (e.customStyle.TryGetValue(k_EvenCellColorProperty, out var evenCellColor))
    25.             m_EvenCellColor = evenCellColor;
    26.     }
    27. }
     
    PrimalCoder and Onigiri like this.
  5. svobodajak

    svobodajak

    Joined:
    Jun 27, 2017
    Posts:
    11
    @uDamian Thank you, this in fact is exactly what I am looking for! ^_^
     
    uDamian likes this.
  6. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    149
    Sorry to resurrect this thread; but how can I use transition for CustomStyleProperties (without Coroutines for example) and is there an easy way to support shorthand values (like "2px 5px", "2px 5px 3px 7px").
     
    PrimalCoder likes this.
  7. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    Unity doesn't seem to allow something like a CustomStyleProperty<Vector2>, customStyle.TryGetValue just gives an error: Vector2 must be convertible to UnityEngine.Object or something like that.

    Anyone know how I could get two floats in one property?
     
  8. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    149
    I am using string for that :') and than parse it. So, that way I can use all of the values like "3", "3 4", "3 4 6 7" (but UI Toolkit totally need to give us some shortcut to use shorthand values, etc)