获取Unity UI工具包中的UI元素大小属性。

huangapple go评论73阅读模式
英文:

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 也没有起作用,floatlong 类型也没有起作用。

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&lt;UIDocument&gt;().rootVisualElement.Q&lt;GroupBox&gt;(&quot;NameOfGroupBox&quot;); .

But Unity refuses to give me the width and showed:

Error CS0029 Cannot implicitly convert type &#39;UnityEngine.UIElements.Length&#39; to &#39;int&#39;

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
                  // -&gt; returns UIElements.IStyle
                  .style
                  // https://docs.unity3d.com/ScriptReference/UIElements.IStyle-width.html
                  // -&gt; returns UIElements.StyleLength
                  .width
                  // https://docs.unity3d.com/ScriptReference/UIElements.StyleLength-value.html
                  // -&gt; returns UIElements.Length
                  .value
                  // https://docs.unity3d.com/ScriptReference/UIElements.Length-value.html 
                  // -&gt; returns float
                  .value;  

答案2

得分: 1

获取 UI 元素的尺寸建议使用 resolvedStyle

所以在您的情况下:

SomeGroupBox = GetComponent&lt;UIDocument&gt;().rootVisualElement.Q&lt;GroupBox&gt;(&quot;NameOfGroupBox&quot;);

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&lt;UIDocument&gt;().rootVisualElement.Q&lt;GroupBox&gt;(&quot;NameOfGroupBox&quot;);

int width = SomeGroupBox.resolvedStyle.width;

huangapple
  • 本文由 发表于 2023年5月17日 22:10:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定