【发布时间】:2022-11-04 20:57:45
【问题描述】:
我正在为我的 UI 使用 UI Builder,并且正在创建一些自定义控件。我已经设法制作了一个可以正常工作的自定义控件。但是第二个有一些我无法理解的问题。
问题: 我可以将自定义控件放入 UI Builder。从一开始,“状态”属性中就没有默认值,它只是空白。当我手动输入一个值并单击离开时,“状态”值将重置为空白。 在控制台中,我从构造函数中收到消息“null”,这意味着我输入的值没有设置。
附加信息: 当我使用 UxmlIntAttributeDescription 类时,第一次出现问题。我有一个带有 UxmlStringAttributeDescription 和 UxmlIntAttributeDescription 的类。我能够设置字符串属性,但不能设置 int 属性。我一直在简化我的代码,所以我可以发布这个问题,然后甚至字符串属性也坏了。 我真的不知道我搞砸了,希望有人可以帮助我解决这个问题。
这是我的代码。它主要是从https://docs.unity3d.com/Manual/UIE-UXML.html 复制的。
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class TestElement : VisualElement {
public new class UxmlFactory : UxmlFactory<TestElement, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits {
UxmlStringAttributeDescription m_status = new UxmlStringAttributeDescription { name = "status", defaultValue = "TestElementString" };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription {
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) {
base.Init(ve, bag, cc);
var ate = ve as TestElement;
ate._status = m_status.GetValueFromBag(bag, cc);
}
}
private string _status;
public TestElement() {
Debug.Log(_status);
}
}
【问题讨论】:
标签: c# unity3d ui-toolkit