VisualElement 是您可以用作其他元素容器的基本元素。您可以使用 VisualElement 将元素组合在一起并为它们应用共同的样式。例如,您可以使用 VisualElement 将标签和文本字段组合在一起,然后为 VisualElement 应用共同的样式。
您可以使用 UI(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。 更多信息
请参阅 术语表 Builder、UXML 或 C# 创建 VisualElement。以下 C# 示例创建了一个带有标签和文本字段的 VisualElement
var myElement = new VisualElement();
myElement.Add(new Label("Hello World"));
myElement.Add(new TextField());
myElement.style.backgroundColor = Color.blue;
myElement.style.width = 200;
myElement.style.height = 200;
C# 类: VisualElement
命名空间: UnityEngine.UIElements
基类: Focusable
此元素具有以下成员属性
名称 | 类型 | 描述 |
---|---|---|
content-container |
字符串 |
子元素将添加到其中,通常与元素本身相同。 |
data-source |
对象 |
将数据源分配给此 VisualElement,这将覆盖任何继承的数据源。所有子元素都将继承此数据源。 |
data-source-path |
字符串 |
从数据源到值的路径。 |
data-source-type |
System.Type |
可分配给此 VisualElement 的数据源的可能类型。 此信息仅供 UI Builder 使用,作为提示,在设计时无法指定有效数据源时,为数据源路径字段提供一些补全。 |
language-direction |
UIElements.LanguageDirection |
指示元素文本的方向。该值将传播到元素的子元素。 将 languageDirection 设置为 RTL 将通过反转文本并适当地处理断行和换行来添加对从右到左 (RTL) 的基本支持。但是,它不提供全面的 RTL 支持,因为这将需要文本整形,包括字符的重新排序和 OpenType 字体功能支持。全面的 RTL 支持计划在未来的更新中提供,这将涉及处理语言、脚本和字体功能规范的更多 API。 为了增强此属性的 RTL 功能,用户可以探索 Unity Asset Store 中可用的第三方插件,并使用 ITextElementExperimentalFeatures.renderedText |
name |
字符串 |
此 VisualElement 的名称。 使用此属性编写针对特定元素的 USS 选择器。标准做法是为元素指定唯一的名称。 |
picking-mode |
UIElements.PickingMode |
确定此元素在鼠标事件或 IPanel.Pick 查询期间是否可以被选中。 |
style |
字符串 |
设置 VisualElement 样式值。 |
tooltip |
字符串 |
在用户将鼠标悬停在元素上一段时间后,在信息框内显示的文本。这仅在编辑器 UI 中受支持。 |
usage-hints |
UIElements.UsageHints |
指定 VisualElement 的高级预期使用模式的提示值的组合。此属性只能在 VisualElement 尚未成为 Panel 的一部分时设置。一旦成为 Panel 的一部分,此属性将实际上变为只读,尝试更改它将抛出异常。正确 UsageHints 的规范会驱动系统根据预期使用模式做出更好的决策,关于如何处理或加速某些操作。请注意,这些提示不会影响行为或视觉结果,而只会影响面板及其内部元素的整体性能。建议始终考虑指定正确的 UsageHints ,但请记住,某些 UsageHints 在某些情况下可能会在内部被忽略(例如,由于目标平台上的硬件限制)。 |
view-data-key |
字符串 |
用于视图数据持久性,例如树展开状态、滚动位置或缩放级别。 此键用于从视图数据存储中保存和加载视图数据。如果您没有设置此键,则关联的 VisualElement 将禁用持久性。有关更多信息,请参阅 视图数据持久性。 |
此元素从其基类继承以下属性
名称 | 类型 | 描述 |
---|---|---|
focusable |
布尔值 |
如果元素可以获得焦点,则为真。 |
tabindex |
整数 |
用于在焦点环中排序可获得焦点的整数。必须大于或等于零。 |
下表列出了所有 C# 公共属性名称及其相关的 USS 选择器。
C# 属性 | USS 选择器 | 描述 |
---|---|---|
disabledUssClassName |
.unity-disabled |
本地禁用元素的 USS 类名。 |
您也可以使用 检查器或 UI 工具包调试器中的匹配选择器部分 来查看哪些 USS 选择器影响了 VisualElement
层次结构中每个级别的组件。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.