Is there a way to completely reset a style of a VisualElement. I need its hierarchy to stay intact, so I can not just create a new one. I know I can reset each property one by one, but that seems like a lot of extra work for something that should be solvable like this: style.Reset() or like this: visualElement.style = new Style().
Here is my code for resetting a style. Now you don’t have to spend time writing 100 variables yourself. Label can be switched out with VisualElement if you don’t care about text.
public static void Reset(ref Label l)
{
var s = l.style;
l.text = default;
//A//
s.alignContent = StyleKeyword.Null;
s.alignItems = StyleKeyword.Null;
s.alignSelf = StyleKeyword.Null;
//B//
s.backgroundColor = StyleKeyword.Null;
s.backgroundImage = StyleKeyword.Null;
s.borderBottomColor = StyleKeyword.Null;
s.borderBottomLeftRadius = StyleKeyword.Null;
s.borderBottomRightRadius = StyleKeyword.Null;
s.borderBottomWidth = StyleKeyword.Null;
s.borderLeftColor = StyleKeyword.Null;
s.borderLeftWidth = StyleKeyword.Null;
s.borderRightColor = StyleKeyword.Null;
s.borderRightWidth = StyleKeyword.Null;
s.borderTopColor = StyleKeyword.Null;
s.borderTopLeftRadius = StyleKeyword.Null;
s.borderTopRightRadius = StyleKeyword.Null;
s.borderTopWidth = StyleKeyword.Null;
s.bottom = StyleKeyword.Null;
//C//
s.color = StyleKeyword.Null;
s.cursor = StyleKeyword.Null;
//D//
s.display = StyleKeyword.Null;
//F//
s.flexBasis = StyleKeyword.Null;
s.flexDirection = StyleKeyword.Null;
s.flexGrow = StyleKeyword.Null;
s.flexShrink = StyleKeyword.Null;
s.flexWrap = StyleKeyword.Null;
s.fontSize = StyleKeyword.Null;
//H//
s.height = StyleKeyword.Null;
//J//
s.justifyContent = StyleKeyword.Null;
//L//
s.left = StyleKeyword.Null;
s.letterSpacing = StyleKeyword.Null;
//M//
s.marginBottom = StyleKeyword.Null;
s.marginLeft = StyleKeyword.Null;
s.marginRight = StyleKeyword.Null;
s.marginTop = StyleKeyword.Null;
s.maxHeight = StyleKeyword.Null;
s.maxWidth = StyleKeyword.Null;
s.minHeight = StyleKeyword.Null;
s.minWidth = StyleKeyword.Null;
//O//
s.opacity = StyleKeyword.Null;
s.overflow = StyleKeyword.Null;
//P//
s.paddingBottom = StyleKeyword.Null;
s.paddingLeft = StyleKeyword.Null;
s.paddingRight = StyleKeyword.Null;
s.paddingTop = StyleKeyword.Null;
s.position = StyleKeyword.Null;
s.right = StyleKeyword.Null;
//T//
s.textOverflow = StyleKeyword.Null;
s.textShadow = StyleKeyword.Null;
s.top = StyleKeyword.Null;
s.unityBackgroundImageTintColor = StyleKeyword.Null;
s.unityBackgroundScaleMode = StyleKeyword.Null;
s.unityFont = StyleKeyword.Null;
s.unityFontDefinition = StyleKeyword.Null;
s.unityFontStyleAndWeight = StyleKeyword.Null;
s.unityOverflowClipBox = StyleKeyword.Null;
s.unityParagraphSpacing = StyleKeyword.Null;
s.unitySliceBottom = StyleKeyword.Null;
s.unitySliceLeft = StyleKeyword.Null;
s.unitySliceRight = StyleKeyword.Null;
s.unitySliceTop = StyleKeyword.Null;
s.unityTextAlign = StyleKeyword.Null;
s.unityTextOutlineColor = StyleKeyword.Null;
s.unityTextOutlineWidth = StyleKeyword.Null;
s.unityTextOverflowPosition = StyleKeyword.Null;
//V//
s.visibility = StyleKeyword.Null;
//W//
s.whiteSpace = StyleKeyword.Null;
s.width = StyleKeyword.Null;
s.wordSpacing = StyleKeyword.Null;
}
Noted.
I think one way to avoid this situation would be to work with USS classes set/reset a bunch of styles together. It’s easier to add/remove USS styles classes to an element than working with individual inline styles. If you can share more details about your workflow, we may be able to provide more precise feedback.
Working on a modular hierarchy based system that allows me to make graphs and other complex elements (Add events and animations etc.). Each module sets some inline properties on an element, and different modules combined can create more complex elements and contain graphs (to visualize data) where each element also has a style based on an array of modules. If I was able to generate USS files, then I might switch to that, depending on how easy it would be to modify them dynamically. Right now, I don’t want a bunch of assets piling up in my project folders, and it would also become a problem when I delete elements. Would be interested to hear how one might approach that.
I wrote and extension for Unity 2020.2 to reset all styles here - and also why we need this.