Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Resetting C# styles to defaults

Discussion in 'UI Toolkit' started by Maverick, Sep 14, 2020.

  1. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    Hello

    From docs:
    But, sometimes there is a need to reset them, so styles from USS would be used again. Is there a way to do this? Couldn't find a way and docs only mention about initial keyword, but had no success in using it.

    Thanks.
     
  2. jonathanma_unity

    jonathanma_unity

    Unity Technologies

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

    Here's how to reset inline styles :

    Code (CSharp):
    1. element.style.width = StyleKeyword.Null; // will reset the width to its default value or USS value if any
     
  3. MoruganKodi

    MoruganKodi

    Joined:
    Feb 11, 2015
    Posts:
    79

    and what if you want to reset ALL of the styles, not just individual keywords?
     
  4. MoruganKodi

    MoruganKodi

    Joined:
    Feb 11, 2015
    Posts:
    79
    I currently have built infrastucture which relies on reusable shells for windows-style modal dialogs and popups , and in the case of common dialogs, the content is also pooled. Which means the modals need to be reset when being reused.

    You can show a messagebox 50 times while sharing a single loaded heirarchy with all of them. But if a user applies style overrides in their code, then we have to reset it so their changes dont persist to other modals.

    Currently the only way to reset a visual element entirely to a default state is by writing out every single keyword by hand to cover for things other developers do when using your custom controls.
    At least make IStyle enumerable so we can iterate and reset all of the styles while covering future new keywords without issue (this is one of those things that should be trivial, but unity always finds a way to make trivial things tedious).

    So for anyone else who wants to reset ALL of their styles - here is an extension method (as of Unity 2020.2)
    This will have to be updated manually for new style keywords in later versions.
    Code (CSharp):
    1.  
    2.         public static void ResetAllStyles(this VisualElement e)
    3.         {
    4.             e.style.alignContent = StyleKeyword.Null;
    5.             e.style.alignItems = StyleKeyword.Null;
    6.             e.style.alignSelf = StyleKeyword.Null;
    7.             e.style.backgroundColor = StyleKeyword.Null;
    8.             e.style.backgroundImage = StyleKeyword.Null;
    9.             e.style.borderBottomColor = StyleKeyword.Null;
    10.             e.style.borderBottomLeftRadius = StyleKeyword.Null;
    11.             e.style.borderBottomRightRadius = StyleKeyword.Null;
    12.             e.style.borderBottomWidth = StyleKeyword.Null;
    13.             e.style.borderLeftColor = StyleKeyword.Null;
    14.             e.style.borderLeftWidth = StyleKeyword.Null;
    15.             e.style.borderRightColor = StyleKeyword.Null;
    16.             e.style.borderRightWidth = StyleKeyword.Null;
    17.             e.style.borderTopColor = StyleKeyword.Null;
    18.             e.style.borderTopRightRadius = StyleKeyword.Null;
    19.             e.style.borderTopWidth = StyleKeyword.Null;
    20.             e.style.bottom = StyleKeyword.Null;
    21.             e.style.color = StyleKeyword.Null;
    22.             e.style.cursor = StyleKeyword.Null;
    23.             e.style.display = StyleKeyword.Null;
    24.             e.style.flexBasis = StyleKeyword.Null;
    25.             e.style.flexDirection = StyleKeyword.Null;
    26.             e.style.flexGrow = StyleKeyword.Null;
    27.             e.style.flexShrink = StyleKeyword.Null;
    28.             e.style.flexWrap = StyleKeyword.Null;
    29.             e.style.fontSize = StyleKeyword.Null;
    30.             e.style.height = StyleKeyword.Null;
    31.             e.style.justifyContent = StyleKeyword.Null;
    32.             e.style.left = StyleKeyword.Null;
    33.             e.style.letterSpacing = StyleKeyword.Null;
    34.             e.style.marginBottom = StyleKeyword.Null;
    35.             e.style.marginLeft = StyleKeyword.Null;
    36.             e.style.marginRight = StyleKeyword.Null;
    37.             e.style.marginTop = StyleKeyword.Null;
    38.             e.style.maxHeight = StyleKeyword.Null;
    39.             e.style.maxWidth = StyleKeyword.Null;
    40.             e.style.minHeight = StyleKeyword.Null;
    41.             e.style.minWidth = StyleKeyword.Null;
    42.             e.style.opacity = StyleKeyword.Null;
    43.             e.style.overflow = StyleKeyword.Null;
    44.             e.style.paddingBottom = StyleKeyword.Null;
    45.             e.style.paddingLeft = StyleKeyword.Null;
    46.             e.style.paddingRight = StyleKeyword.Null;
    47.             e.style.paddingTop = StyleKeyword.Null;
    48.             e.style.position = StyleKeyword.Null;
    49.             e.style.right = StyleKeyword.Null;
    50.             e.style.textOverflow = StyleKeyword.Null;
    51.             e.style.textShadow = StyleKeyword.Null;
    52.             e.style.top = StyleKeyword.Null;
    53.             e.style.unityBackgroundImageTintColor = StyleKeyword.Null;
    54.             e.style.unityBackgroundScaleMode = StyleKeyword.Null;
    55.             e.style.unityFont = StyleKeyword.Null;
    56.             e.style.unityFontDefinition = StyleKeyword.Null;
    57.             e.style.unityFontStyleAndWeight = StyleKeyword.Null;
    58.             e.style.unityOverflowClipBox = StyleKeyword.Null;
    59.             e.style.unityParagraphSpacing = StyleKeyword.Null;
    60.             e.style.unitySliceBottom = StyleKeyword.Null;
    61.             e.style.unitySliceLeft = StyleKeyword.Null;
    62.             e.style.unitySliceRight = StyleKeyword.Null;
    63.             e.style.unitySliceTop = StyleKeyword.Null;
    64.             e.style.unityTextAlign = StyleKeyword.Null;
    65.             e.style.unityTextOutlineColor = StyleKeyword.Null;
    66.             e.style.unityTextOutlineWidth = StyleKeyword.Null;
    67.             e.style.unityTextOverflowPosition = StyleKeyword.Null;
    68.             e.style.visibility = StyleKeyword.Null;
    69.             e.style.whiteSpace = StyleKeyword.Null;
    70.             e.style.width = StyleKeyword.Null;
    71.             e.style.wordSpacing = StyleKeyword.Null;
    72.         }
    73.  
     
  5. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    I thought I had posted a thread about this, but apparently not. Anyways, this was the solution I ended up with too. Even cleared the values in the same order (alphabetical). I did also reset the transform and any eventual text if it was a label.
     
  6. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    242
    For styles applied via the "transform" API, is it correct to use StyleKeyword.Null on the styles applied by the ITransform interface ?

    Code (CSharp):
    1. Vector3 ITransform.position
    2.     {
    3.       get => this.resolvedStyle.translate;
    4.       set => this.style.translate = (StyleTranslate) new Translate((Length) value.x, (Length) value.y, value.z);
    5.     }
    6.  
    7.     Quaternion ITransform.rotation
    8.     {
    9.       get => this.resolvedStyle.rotate.ToQuaternion();
    10.       set
    11.       {
    12.         float angle;
    13.         Vector3 axis;
    14.         value.ToAngleAxis(out angle, out axis);
    15.         this.style.rotate = (StyleRotate) new Rotate((Angle) angle, axis);
    16.       }
    17.     }
    18.  
    19.     Vector3 ITransform.scale
    20.     {
    21.       get => this.resolvedStyle.scale.value;
    22.       set => this.style.scale = (StyleScale) new Scale(value);
    23.     }

    Meaning I would do the following to reset the style :
    Code (CSharp):
    1.  
    2. targetElement.style.translate = StyleKeyword.Null;
    3. targetElement.style.rotate= StyleKeyword.Null;
    4. targetElement.style.scale= StyleKeyword.Null;
    5.  
     
  7. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    242
    So, it doesn't work :D


    The InlineStyleAccess implementation behind VisualElement.style does additional stuff to dirty the VisualElement with internal methods.

    According to decompiler, it looks like this for position :
    Code (CSharp):
    1.  
    2. StyleEnum<Position> IStyle.position
    3. {
    4.   get
    5.   {
    6.     StyleInt styleInt = this.GetStyleInt(StylePropertyId.Position);
    7.     return new StyleEnum<Position>((Position) styleInt.value, styleInt.keyword);
    8.   }
    9.   set
    10.   {
    11.     if (!this.SetStyleValue<Position>(StylePropertyId.Position, value))
    12.       return;
    13.     this.ve.IncrementVersion(VersionChangeType.Layout | VersionChangeType.Styles);
    14.     this.ve.yogaNode.PositionType = (YogaPositionType) this.ve.computedStyle.position;
    15.   }
    16. }
    17.  
    And like this for rotation :
    Code (CSharp):
    1.  
    2. StyleRotate IStyle.rotate
    3. {
    4.   get
    5.   {
    6.     StyleRotate styleRotate = new StyleRotate();
    7.     return this.TryGetInlineRotate(ref styleRotate) ? styleRotate : (StyleRotate) StyleKeyword.Null;
    8.   }
    9.   set
    10.   {
    11.     if (!this.SetInlineRotate(value))
    12.       return;
    13.     this.ve.IncrementVersion(VersionChangeType.Styles | VersionChangeType.Transform);
    14.   }
    15. }
    16.  
    Am I missing something ?
     
  8. uBenoitA

    uBenoitA

    Unity Technologies

    Joined:
    Apr 15, 2020
    Posts:
    214
    Midiphony-panda likes this.
  9. seyfe

    seyfe

    Joined:
    May 10, 2019
    Posts:
    74
    What about the image tint color? It’s not possible to assing `StyleKeyword.Null` to this right? How would I reset this?