Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jdbenitez · Feb 07 at 12:08 AM · uitoolkit

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);
         }
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by andrew-lukasik · Feb 13 at 06:52 PM

Can i extend the standard label using direct inheritance?

Yes

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 Labels in a complete ui tree and modify their text values using official localization package.

Implementation example here: UIDocumentLocalization.cs.


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jdbenitez · Feb 13 at 07:06 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to put downloaded images into UIButton using UIToolkit 0 Answers

How can you use UQuery to access child nodes? 1 Answer

The "Event Trigger" Component makes more process to the game that use a collider and use this in the script when I use "onMouseDown" function? 0 Answers

InputField caret too big in world space canvas 0 Answers

UI Builder is missing several standard library options 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges