Hi all! Trying to create my own custom control for UI Toolkit. Almost finished but got stucked with these warnings, they infinitely asserts.
A little bit about the control though: it’s basically TextField, with image icon and prebuilt style, so the most trouble was to make it possible to set the image by UxmlAttributeDescriprion, since there are just basic types it seems impossible. So I make a string attributeDesc with url to image and set it in code.
It works fine, but warnings don’t think so =D, and I don’t know what I’ve done wrong.
internal class MDT_TextField : VisualElement
{
private TextField _textField;
private VisualElement _body;
private VisualElement _icon;
private string _sampleString;
private string _imageUrl;
public MDT_TextField() : base()
{
_body = new VisualElement();
_icon = new VisualElement();
_textField = new TextField();
SetupAll();
BuildAll();
}
private void SetupAll()
{
SetupBody();
SetupIcon();
SetupTextField();
}
private void SetupBody()
{
_body.name = "body";
_body.style.flexDirection = FlexDirection.Row;
_body.style.paddingBottom = 8f;
_body.style.paddingLeft = 12f;
_body.style.paddingRight = 12f;
_body.style.paddingTop = 8f;
}
private void SetupIcon()
{
_icon.name = "icon";
_icon.style.height = Length.Percent(100);
_icon.style.width = 24f;
}
private void SetupTextField()
{
_textField.name = "textField";
_textField.style.marginLeft = 12f;
_textField.style.marginRight = 0f;
_textField.style.marginBottom = 0f;
_textField.style.marginTop = 0f;
_textField.style.flexBasis = new StyleLength(StyleKeyword.Auto);
var _textInput = _textField.Q("unity-text-input");
_textInput.style.backgroundColor= new Color(0.0f, 0.0f, 0.0f, 0.0f);
_textInput.style.borderBottomWidth = 0f;
_textInput.style.borderLeftWidth = 0f;
_textInput.style.borderRightWidth = 0f;
_textInput.style.borderTopWidth = 0f;
_textField.style.textOverflow = TextOverflow.Ellipsis;
}
private void BuildAll()
{
this.Add(_body);
_body.Add(_icon);
_body.Add(_textField);
}
public new class UxmlFactory : UxmlFactory<MDT_TextField, UxmlTraits> { }
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription attribute_SampleString =
new UxmlStringAttributeDescription { name = "SampleString", defaultValue = "Пример" };
UxmlStringAttributeDescription attribute_ImageUrl =
new UxmlStringAttributeDescription { name = "Image URL", defaultValue = "" };
private string _imageUrl;
[Obsolete]
public override void Init(VisualElement visual, IUxmlAttributes bag, CreationContext cc)
{
base.Init(visual, bag, cc);
var textField = visual as MDT_TextField;
textField._sampleString = attribute_SampleString.GetValueFromBag(bag, cc);
textField._textField.value = textField._sampleString;
_imageUrl = attribute_ImageUrl.GetValueFromBag(bag, cc);
try
{
if (String.IsNullOrEmpty(_imageUrl)) return;
var rawData = System.IO.File.ReadAllBytes(_imageUrl);
Texture2D image = new Texture2D(24, 24);
image.LoadImage(rawData);
if (String.IsNullOrEmpty(_imageUrl) || image == null)
{
textField._icon.style.backgroundImage = null;
}
else
{
var imageStyle = new StyleBackground(image);
textField._icon.style.backgroundImage = imageStyle;
//textField._icon.style.unityBackgroundScaleMode = new StyleEnum<ScaleMode>(ScaleMode.ScaleToFit);
}
}
catch (Exception e)
{
}
}
}
}
Thanks in advance!
