HelpBox 用于向用户显示消息。您可以设置要显示的消息类型,例如警告或错误。您还可以将消息设置为以富文本格式显示。
您可以使用 UXML 和 C# 创建 HelpBox。以下 C# 示例创建了一个带有“帮助文本”消息和信息图标的 HelpBox
HelpBox helpBox = new HelpBox("Help text", HelpBoxMessageType.Info);
以下 UXML 示例创建了一个带有警告消息的 HelpBox
<UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<HelpBox text="UXML Help Box" message-type="Info" name="the-uxml-help-box" />
</UXML>
以下 C# 示例说明了 HelpBox 的一些可自定义功能
/// <sample>
// Get a reference to the help box from UXML and update its text.
var uxmlHelpBox = container.Q<HelpBox>("the-uxml-help-box");
uxmlHelpBox.text += " (Updated in C#)";
// Create a new help box and give it a style class.
var csharpHelpBox = new HelpBox("This is a help box", HelpBoxMessageType.Warning);
csharpHelpBox.AddToClassList("some-styled-help-box");
container.Add(csharpHelpBox);
/// </sample>
要在 Unity 中实时尝试此示例,请转到窗口 > UI 工具包 > 示例。
C# 类:HelpBox
命名空间:UnityEngine.UIElements
基类:VisualElement
此元素具有以下成员属性
名称 | 类型 | 描述 |
---|---|---|
message-type |
UIElements.HelpBoxMessageType |
消息类型。 |
text |
字符串 |
消息文本。 |
此元素从其基类继承以下属性
名称 | 类型 | 描述 |
---|---|---|
focusable |
布尔值 |
如果元素可以获得焦点,则为 True。 |
tabindex |
整数 |
用于在焦点环中对可聚焦元素进行排序的整数。必须大于或等于零。 |
此元素还从 VisualElement
继承以下属性
名称 | 类型 | 描述 |
---|---|---|
content-container |
字符串 |
子元素添加到其中,通常与元素本身相同。 |
data-source |
对象 |
为该 VisualElement 分配数据源,覆盖任何继承的数据源。此数据源由所有子元素继承。 |
data-source-path |
字符串 |
从数据源到值的路径。 |
data-source-type |
System.Type |
可分配给此 VisualElement 的数据源的可能类型。 此信息仅供 UI 构建器使用,作为提示,在设计时无法指定有效数据源时,为数据源路径字段提供一些自动完成。 |
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(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。 更多信息 参见 词汇表中受支持。 |
usage-hints |
UIElements.UsageHints |
指定 VisualElement 高级预期使用模式的提示值组合。仅当 VisualElement 尚未成为 Panel 的一部分时,才能设置此属性。一旦成为 Panel 的一部分,此属性便有效地变为只读,尝试更改它将引发异常。正确 UsageHints 的规范会驱动系统根据预期的使用模式做出更好的决策,以处理或加速某些操作。请注意,这些提示不会影响行为或视觉结果,只会影响面板及其内部元素的整体性能。建议始终考虑指定正确的 UsageHints ,但请记住,某些 UsageHints 在某些情况下可能会被内部忽略(例如,由于目标平台上的硬件限制)。 |
view-data-key |
字符串 |
用于视图数据持久性,例如树展开状态、滚动位置或缩放级别。 此键用于从视图数据存储中保存和加载视图数据。如果您未设置此键,则关联的 VisualElement 的持久性将被禁用。有关更多信息,请参阅 视图数据持久性。 |
下表列出了所有 C# 公共属性名称及其相关的 USS 选择器。
C# 属性 | USS 选择器 | 描述 |
---|---|---|
ussClassName |
.unity-help-box |
此类型元素的 USS 类名称。 |
labelUssClassName |
.unity-help-box__label |
此类型元素中标签的 USS 类名称。 |
iconUssClassName |
.unity-help-box__icon |
此类型元素中图像的 USS 类名称。 |
iconInfoUssClassName |
.unity-help-box__icon--info |
此类型元素中 HelpBoxMessageType.Info 状态的 USS 类名称。 |
iconwarningUssClassName |
.unity-help-box__icon--warning |
此类型元素中 HelpBoxMessageType.Warning 状态的 USS 类名称。 |
iconErrorUssClassName |
.unity-help-box__icon--error |
此类型元素中 HelpBoxMessageType.Error 状态的 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.