Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get UI element size property with Unity UI toolkit

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading