英文:
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:
我正在尝试获取并存储 UI 元素的宽度(使用 Unity UI 工具包)以供以后计算:
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:
但 Unity 拒绝给我宽度并显示:
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.
明确将其转换为 int
也没有起作用,float
或 long
类型也没有起作用。
Is there a way to use the UnityEngine.UIElements.Length
type as int
?
是否有办法将 UnityEngine.UIElements.Length
类型用作 int
?
英文:
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
?
答案1
得分: 1
我认为你只需要在那里添加另一个 value
。
英文:
I think you just need another value
there
float width = SomeGroupBox
// https://docs.unity3d.com/ScriptReference/UIElements.VisualElement-style.html
// -> returns UIElements.IStyle
.style
// https://docs.unity3d.com/ScriptReference/UIElements.IStyle-width.html
// -> returns UIElements.StyleLength
.width
// https://docs.unity3d.com/ScriptReference/UIElements.StyleLength-value.html
// -> returns UIElements.Length
.value
// https://docs.unity3d.com/ScriptReference/UIElements.Length-value.html
// -> returns float
.value;
答案2
得分: 1
获取 UI 元素的尺寸建议使用 resolvedStyle
所以在您的情况下:
SomeGroupBox = GetComponent<UIDocument>().rootVisualElement.Q<GroupBox>("NameOfGroupBox");
int width = SomeGroupBox.resolvedStyle.width;
英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论