Skip to content

Instantly share code, notes, and snippets.

@xepherys
Created December 12, 2022 14:18
Show Gist options
  • Save xepherys/6bcc53d407e18b7dcc18cd7a5d7e3f0d to your computer and use it in GitHub Desktop.
Save xepherys/6bcc53d407e18b7dcc18cd7a5d7e3f0d to your computer and use it in GitHub Desktop.
Unity VisualElement CustomStyleProperties and Callbacks
class MyElement : VisualElement
{
static readonly CustomStyleProperty<int> k_CellSizeProperty = new CustomStyleProperty<int>("--cell-size");
static readonly CustomStyleProperty<Color> k_OddCellColorProperty = new CustomStyleProperty<Color>("--odd-cell-color");
static readonly CustomStyleProperty<Color> k_EvenCellColorProperty = new CustomStyleProperty<Color>("--even-cell-color");
int m_CellSize;
Color m_OddCellColor;
Color m_EvenCellColor;
public MyElement()
{
RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
}
void OnCustomStyleResolved(CustomStyleResolvedEvent e)
{
if (e.customStyle.TryGetValue(k_CellSizeProperty, out var cellSizeProperty))
m_CellSize = cellSizeProperty;
if (e.customStyle.TryGetValue(k_OddCellColorProperty, out var oddCellColor))
m_OddCellColor = oddCellColor;
if (e.customStyle.TryGetValue(k_EvenCellColorProperty, out var evenCellColor))
m_EvenCellColor = evenCellColor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment