Unity3D :向 UXML 和 UI 生成器公开自定义控件
推荐:将NSDT场景编辑器加入你的3D工具链
3D工具集:NSDT简石数字孪生
向 UXML 和 UI 生成器公开自定义控件
将自定义控件与 UXML 一起使用,并且用户界面
构建器,您必须公开它们。
定义工厂
若要定义新元素,请从 或其子类之一派生新类,然后在此新类中实现相应的功能。VisualElement
新类必须实现默认构造函数。例如:
class StatusBar : VisualElement
{
public StatusBar()
{
m_Status = String.Empty;
}
string m_Status;
public string status { get; set; }
}
为了使 UI 工具包在读取 UXML 文件时实例化新类,必须为类定义一个工厂。除非你的工厂需要做一些特别的事情,否则你可以从 中派生工厂。建议将工厂类放在组件类中。UxmlFactory<T>
例如,以下代码段定义了一个以类命名的工厂:UxmlFactoryStatusBar
class StatusBar : VisualElement
{
public new class UxmlFactory : UxmlFactory<StatusBar> {}
// ...
}
定义此工厂后,您可以在 UXML 文件中使用该元素。<StatusBar>
定义元素的属性
您可以为新类定义 UXML 特征,并将其工厂设置为使用这些特征。
例如,以下代码片段定义了一个 UXML 特征类,以将该属性初始化为该类的属性。状态属性从 UXML 数据初始化。statusStatusBar
class StatusBar : VisualElement
{
public new class UxmlFactory : UxmlFactory<StatusBar, UxmlTraits> {}
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription m_Status = new UxmlStringAttributeDescription { name = "status" };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((StatusBar)ve).status = m_Status.GetValueFromBag(bag, cc);
}
}
// ...
}
它有两个目的:UxmlTraits
- 工厂使用它来初始化新创建的对象。
- 架构生成过程会对其进行分析以获取有关元素的信息。此信息将转换为 XML 架构指令。
上面的代码示例执行以下操作:
- 的声明定义了一个名为 的 XML 属性。
m_Statusstatus
- 返回一个空,指示该元素没有子元素。
uxmlChildElementsDescriptionIEnumerableStatusBar
- 成员从 XML 分析器读取属性包中的属性值,并将属性设置为此值。
Init()statusStatusBar.status
- 类放置在类内。这允许该方法访问 的私有成员。
UxmlTraitsStatusBarInit()StatusBar
- 新类继承自基类,因此它共享基类的属性。
UxmlTraitsUxmlTraits
Init()
calls to initialize the base class properties.base.Init()
The code example above declares a string attribute with the UxmlStringAttributeDescription
class. UI Toolkit supports the following types of attributes and each links a C# type to a UMXL type:
Attribute | Attribute value |
---|---|
UxmlStringAttributeDescription | A string |
UxmlFloatAttributeDescription | A single precision floating point value in the range of the C# type.float |
UxmlDoubleAttributeDescription | C# 类型范围内的双精度浮点值。double |
UxmlIntAttributeDescription | C# 类型范围内的整数值。int |
UxmlLongAttributeDescription | C# 类型范围内的长整数值。long |
UxmlBoolAttributeDescription | true 或false |
UxmlColorAttributeDescription | 一个字符串,表示以 USS 格式定义的颜色。 |
UxmlEnumAttributeDescription<T> | 一个字符串,表示类型 .Enum T |
UxmlTypeAttributeDescription<T> | 一个字符串,表示类型的程序集限定名称。 |
在上面的代码示例中,返回一个空,指示元素不接受 XML 架构的子元素说明。uxmlChildElementsDescriptionIEnumerableStatusBar
若要使元素接受任何类型的子元素,必须重写该属性。例如,要使元素接受任何类型的子元素,必须按如下所示指定属性:uxmlChildElementsDescriptionStatusBaruxmlChildElementsDescription
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get
{
yield return new UxmlChildElementDescription(typeof(VisualElement));
}
}
定义命名空间前缀
在 C# 中定义新元素后,可以在 UXML 文件中使用该元素。若要对元素进行分类,请在命名空间中创建类。定义新命名空间时,可以为命名空间定义前缀。必须将命名空间前缀定义为根元素的属性,并在限定元素范围时替换完整的命名空间名称。<UXML>
若要定义命名空间前缀,请为每个命名空间前缀向程序集添加一个属性。例如:UxmlNamespacePrefix
[assembly: UxmlNamespacePrefix("My.First.Namespace", "first")]
[assembly: UxmlNamespacePrefix("My.Second.Namespace", "second")]
可以在程序集的任何 C# 文件的根级别(任何命名空间外部)执行此操作。
架构生成系统执行以下操作:
- 检查这些属性并使用它们生成架构。
- 将命名空间前缀定义添加为新创建的 UXML 文件中元素的属性。
<UXML>
- 在其属性中包含命名空间的架构文件位置。
xsi:schemaLocation
要确保您的文本编辑器识别新元素,请选择资产>更新 UXML 架构以更新架构定义。
要创建带有前缀的新 UXML 文档,请选择资产>创建> UI 工具包> UI 文档。
其他资源
- 创建自定义控件
- 创建具有两个属性的自定义控件
- 创建滑动切换自定义控件
- 创建径向进度指示器
- 创建可绑定的自定义控件
- 为自定义控件创建自定义样式
由3D建模学习工作室整理翻译,转载请注明出处!