ObjectField 是一个仅限编辑器元素,允许用户从可用对象列表中选择一个 Unity 对象。您可以使用它来选择一个 Unity 对象作为参考,例如材质、纹理、脚本或GameObjectUnity 场景中的基本对象,可以表示角色、道具、场景、摄像机、路径点等等。GameObject 的功能由附加在其上的组件定义。 更多信息
参见 术语表。
注意:要将元素与检查器窗口中的其他字段对齐,只需将.unity-base-field__aligned
USS 类应用于它即可。有关更多信息,请参阅BaseField
。
您可以使用UI(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。 更多信息
参见 术语表 构建器、UXML 或 C# 创建 ObjectField。
要限制可以选择的对象类型,请使用type
属性。在 UXML 中,type
属性的格式为类型名称,类型程序集名称
。
提示:要查找类型的程序集名称,请使用Debug.Log(theType.AssemblyQualifiedName);
或在类型的脚本 API 页面中查找。
以下示例创建了一个用于选择 2D 纹理的 ObjectField
UXML:
<ObjectField type="UnityEngine.Texture2D, UnityEngine.CoreModule" label="Select an object:" />
C#:
using UnityEditor.UIElements;
...
var objectField = new ObjectField();
objectField.objectType = typeof(Texture2D);
objectField.label = "Select an object:";
以下 UXML 示例创建了一个 ObjectField
<UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<uie:ObjectField label="UXML Field" name="the-uxml-field" />
</UXML>
以下 C# 示例说明了 MinMaxSlider 的一些可自定义功能
/// <sample>
// Get a reference to the field from UXML and assign a value to it.
var uxmlField = container.Q<ObjectField>("the-uxml-field");
uxmlField.value = new Texture2D(10, 10) { name = "new_texture" };
// Create a new field, disable it, and give it a style class.
var csharpField = new ObjectField("C# Field");
csharpField.SetEnabled(false);
csharpField.AddToClassList("some-styled-field");
csharpField.value = uxmlField.value;
container.Add(csharpField);
// Mirror the value of the UXML field into the C# field.
uxmlField.RegisterCallback<ChangeEvent<Object>>((evt) =>
{
csharpField.value = evt.newValue;
});
/// </sample>
要在 Unity 中实时尝试此示例,请转到窗口 > UI 工具包 > 示例。
C# 类:ObjectField
命名空间:UnityEditor.UIElements
基类:BaseField_1
此元素具有以下成员属性
名称 | 类型 | 描述 |
---|---|---|
allow-scene-objects |
布尔值 |
允许将场景场景包含游戏环境和菜单。可以将每个唯一的场景文件视为一个独特的关卡。在每个场景中,您放置环境、障碍物和装饰,本质上是分段设计和构建您的游戏。 更多信息 参见 术语表对象分配给字段。 |
此元素从其基类继承以下属性
名称 | 类型 | 描述 |
---|---|---|
binding-path |
字符串 |
要绑定的目标属性的路径。 |
focusable |
布尔值 |
如果元素可以获得焦点,则为 True。 |
label |
字符串 |
表示将出现在字段旁边的标签的字符串。 |
tabindex |
整数 |
用于在焦点环中对可聚焦元素进行排序的整数。必须大于或等于零。 |
value |
对象 |
与字段关联的值。 |
此元素还从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 中受支持。 |
usage-hints |
UIElements.UsageHints |
指定VisualElement 的高级预期使用模式的提示值的组合。仅当VisualElement 尚未成为Panel 的一部分时,才能设置此属性。一旦成为Panel 的一部分,此属性实际上就成为只读属性,尝试更改它将引发异常。正确UsageHints 的规范使系统能够更好地决定如何根据预期的使用模式处理或加速某些操作。请注意,这些提示不会影响行为或视觉结果,只会影响面板及其内部元素的整体性能。建议始终考虑指定正确的UsageHints ,但请记住,在某些情况下,某些UsageHints 可能会在内部被忽略(例如,由于目标平台上的硬件限制)。 |
view-data-key |
字符串 |
用于视图数据持久性,例如树展开状态、滚动位置或缩放级别。 此键用于从视图数据存储中保存和加载视图数据。如果您不设置此键,则关联的 VisualElement 的持久性将被禁用。有关更多信息,请参阅视图数据持久性。 |
下表列出了所有 C# 公共属性名称及其相关的 USS 选择器。
C# 属性 | USS 选择器 | 描述 |
---|---|---|
ussClassName |
.unity-object-field |
此类型元素的 USS 类名称。 |
labelUssClassName |
.unity-object-field__label |
此类型元素中标签的 USS 类名称。 |
inputUssClassName |
.unity-object-field__input |
此类型元素中输入元素的 USS 类名称。 |
objectUssClassName |
.unity-object-field__object |
此类型元素中对象元素的 USS 类名称。 |
selectorUssClassName |
.unity-object-field__selector |
此类型元素中选择器元素的 USS 类名称。 |
ussClassName |
.unity-base-field |
此类型元素的 USS 类名称。 |
labelUssClassName |
.unity-base-field__label |
此类型元素中标签的 USS 类名称。 |
inputUssClassName |
.unity-base-field__input |
此类型元素中输入元素的 USS 类名称。 |
noLabelVariantUssClassName |
.unity-base-field--no-label |
当没有标签时,此类型元素的 USS 类名称。 |
labelDraggerVariantUssClassName |
.unity-base-field__label--with-dragger |
当标签上附加了拖动器时,此类型元素中标签的 USS 类名称。 |
mixedValueLabelUssClassName |
.unity-base-field__label--mixed-value |
显示混合值的元素的 USS 类名称 |
alignedFieldUssClassName |
.unity-base-field__aligned |
在检查器元素中对齐的元素的 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.