Created
December 12, 2022 14:18
-
-
Save xepherys/6bcc53d407e18b7dcc18cd7a5d7e3f0d to your computer and use it in GitHub Desktop.
Unity VisualElement CustomStyleProperties and Callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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