I have a CustomLabel:
[UxmlElement]
public partial class CustomLabel : Label
{
[UxmlAttribute]
public Color HighlightColor { get; set; } = Color.yellow;
private Color m_prior_color;
private bool m_enabled_highlight;
//=================================
public bool EnableHighlight
{
get { return m_enabled_highlight; }
set {
m_enabled_highlight = value;
if (m_enabled_highlight) {
m_prior_color = style.color.value;
style.color = HighlightColor;
} else {
style.color = m_prior_color;
}
}
}
}
UXML builder properly shows HighlightColor as a field and I can set it’s color value.
When I use code to set EnableHighlight = true, the color of the Label’s text changes correctly.
When I set EnableHighlight = false, the color of the Label’s text get’s set to RGBA(0,0,0,0) which is NOT what the original color was. The original color was white, as the color is actually unset/undefined.
When I explicitly set the color to something specific, I can properly see the new color, and change it to the HighlightColor, but again when I turn Highlight back off, it gets set to RGBA(0,0,0,0), and not the correct prior color.
When stepping through the code The value held by style.color.keyword has a value of “Null” and has the default RGBA(0,0,0,0) as the value retrieved from style.color.value. (explaining why it gets set to that via the above code)
From where do I read the correct original color value so I can save and restore it later? (Or is this a bug?)
Please do not suggest uss action themes like :hover, :active, :focus, etc. This is not my intent, I explicitly need to dynamically swap colors via code independently of those kind of trigger actions.