Search Unity

Resolved How to preserve sprites in TMP text component?

Discussion in 'Localization Tools' started by c-Row, Jan 10, 2023.

  1. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    853
    Recently I have started looking into the official Localization package and started converting standalone texts to being localized which works like a charm. However, I have a couple of Text (UI) components in which I am using the sprite tag to add images to buttons.

    upload_2023-1-10_10-44-9.png

    upload_2023-1-10_10-43-31.png

    Now obviously I could make the tag part of the translation string but I would like to avoid keeping redunant information around, considering I might want to change the sprite further down the road. What would be the recommenced approach if I wanted to have the text translated but the sprite tag preserved at the same time?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Hmm. If the issue is that you want to be able to easily change the sprite, e.g the index then you could make it a smart string and place the index as a persistent variable like so:

    "<sprite={spriteId}> Upgrade"
    sprite.png


    This does mean you keep the sprite in the localized string though. An alternative would be to write a script that adds the sprite on before displaying it. Something like:

    Code (csharp):
    1. public class SpriteExample : MonoBehaviour
    2. {
    3.     public LocalizedString localizedString;
    4.     public string spriteString = "<sprite=32>";
    5.     public TMP_Text tMP_Text;
    6.  
    7.     void OnEnable()
    8.     {
    9.         localizedString.StringChanged += LocalizedString_StringChanged;
    10.     }
    11.  
    12.     void OnDisable()
    13.     {
    14.         localizedString.StringChanged -= LocalizedString_StringChanged;
    15.     }
    16.  
    17.     void LocalizedString_StringChanged(string value)
    18.     {
    19.         tMP_Text.text = $"{spriteString} {value}";
    20.     }
    21. }
     
    c-Row likes this.
  3. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    853
    The second option sounds more helpful in that case - it's a bit of extra work but at least nobody needs to worry about the sprite tag in the translation table. :D
     
    karl_jones likes this.