I'm trying to get and store the width of a UI element (using the Unity UI toolkit) for later calculation:
int width = SomeGroupBox.style.width.value;
Where the group box is acquired by SomeGroupBox = GetComponent<UIDocument>().rootVisualElement.Q<GroupBox>("NameOfGroupBox");
.
But Unity refuses to give me the width and showed:
Error CS0029 Cannot implicitly convert type 'UnityEngine.UIElements.Length' to 'int'
Explicitly casting it into int
also did not work, neither does the float
or long
type.
Is there a way to use the UnityEngine.UIElements.Length
type as int
?
To get the dimensions of a UI element it is recommented to use resolvedStyle
So in your case:
SomeGroupBox = GetComponent<UIDocument>().rootVisualElement.Q<GroupBox>("NameOfGroupBox");
int width = SomeGroupBox.resolvedStyle.width;