- Home /
Extend ui toolkit label control
I'm building a runtime gui with ui toolkit and i'd like to extend the standard label control for to add multilang behaviour. I have all necesary code for do it adding a single component in a standard text ot text mesh pro, I only need help for modify the ui toolkit label text when the scene starts.
I'd like doit without create a label ctrl since zero. Can i extend the standard label using direct inheritance? I tried adapting the manual example but i don't know how to modify the text of the label.
class Labelml : Label
{
public Labelml()
{
}
public string Status { get; set; } = "status";
public new class UxmlFactory : UxmlFactory<Labelml> { }
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription m_Status = new UxmlStringAttributeDescription { name = "labelmlName" };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((Labelml)ve).Status = m_Status.GetValueFromBag(bag, cc);
}
}
}
Answer by andrew-lukasik · Feb 13 at 06:52 PM
YesCan i extend the standard label using direct inheritance?
i don't know how to modify the text of the label
// @ constructor, for example
this.text = "text";
or
// @ UxmlTraits.Init()
var label = (MyCustomLabel) ve;
label.text = "text"
[UnityEngine.Scripting.Preserve]
public class MyCustomLabel : Label
{
public string LocalizationKey { get; set; }
public MyCustomLabel () {}
public new class UxmlFactory : UxmlFactory<MyCustomLabel,UxmlTraits> {}
public new class UxmlTraits : VisualElement.UxmlTraits
{
UxmlStringAttributeDescription LocalizationKeyAttr = new UxmlStringAttributeDescription{ name="Localization-Key" , defaultValue="none" };
public override void Init ( VisualElement ve , IUxmlAttributes attributes , CreationContext context )
{
base.Init( ve , attributes , context );
var instance = (MyCustomLabel) ve;
string locKey = LocalizationKeyAttr.GetValueFromBag( attributes , context );
instance.LocalizationKey = locKey;
instance.text = instance.LocalizeString( locKey );
}
}
public string LocalizeString ( string key )
{
return "implement localization method here";
}
}
But. I don't think custom label type is a good way to go about localizing UI. Why? Because you cannot override existing Label
implementations this way and many element types include them as child elements. For example, a Button
- it will require writing custom Button type.
Alternative approach would be to find all Label
s in a complete ui tree and modify their text values using official localization package.
Implementation example here: UIDocumentLocalization.cs.
thank you very much, I will follow your recommendation. I didn't take in consideration that buttons no longer have a text as a child as in the previous UI.
Your answer

Follow this Question
Related Questions
How could I make a “discovery” UI? 1 Answer
UiToolkit Transinition is not working 1 Answer
Best Way to handle different Menu BG images (Using UIToolKit) 0 Answers
Ui Builder error 0 Answers
About the shader of UIToolkit 0 Answers