Search Unity

How to know if the text has been changed?

Discussion in 'Localization Tools' started by imiotis, Feb 27, 2021.

  1. imiotis

    imiotis

    Joined:
    Dec 22, 2018
    Posts:
    35
    Hello!
    I want the button width to match the text width.
    To do this, I need to know the preferred width of the text. Since the width of the text in different languages is different, I have to catch the moment when the text has changed, and at that moment get "text.preferredWidth" property.
    Is there any event of this?
    2iCj03OSFq.gif
     
  2. imiotis

    imiotis

    Joined:
    Dec 22, 2018
    Posts:
    35
    It seems "yield return LocalizationSettings.InitializationOperation" after changing the language is working as I need. But can I be sure that this means that the all texts have changed?
    Code (CSharp):
    1. LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[0];
    2. yield return LocalizationSettings.InitializationOperation;
    3. // Can I get the "preferredWidth" of the text now??? Or should I wait a little longer?
    4. //float width = text.preferredWidth;
    5. //Vector2 sizeDelta = buttonRectTransform.sizeDelta;
    6. //sizeDelta.x = width;
    7. //buttonRectTransform.sizeDelta = width;
    8.  
     
    Last edited: Feb 27, 2021
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Hi,
    Take a look at the Loading Strings Samples, you can get to them in the package manager.
    You can get the string in the update event, do what you need to and then pass it to the Text Component yourself.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4. using UnityEngine.UI;
    5.  
    6. public class LocalizedStringWithChangeHandlerExample : MonoBehaviour
    7. {
    8.     public LocalizedString stringRef = new LocalizedString() { TableReference = "My String Table", TableEntryReference = "Hello World" };
    9.     public Text text;
    10.  
    11.     void OnEnable()
    12.     {
    13.         stringRef.StringChanged += UpdateString;
    14.     }
    15.  
    16.     void OnDisable()
    17.     {
    18.         stringRef.StringChanged -= UpdateString;
    19.     }
    20.  
    21.     void UpdateString(string translatedValue)
    22.     {
    23.         // Do something here
    24.         text.text = translatedValue;
    25.     }
    26. }
    27.  
    Alternatively you could just add an extra event to the Localized String Event Update String callbacks. This will get called every time the value changes.
     
  4. imiotis

    imiotis

    Joined:
    Dec 22, 2018
    Posts:
    35
    It seems I need that alternative way, because I want to make a separate component to change the width.
    I do not know the events very well yet. Did I write the code correctly:
    Code (CSharp):
    1. GetComponent<LocalizeStringEvent>().OnUpdateString.AddListener(TextWasChanged);
    ?
    This is how it completely:
    mspaint_6KOXoVC3DV.png
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    That looks correct. Does it work?
     
  6. imiotis

    imiotis

    Joined:
    Dec 22, 2018
    Posts:
    35
    Yes, thank you!
     
    karl_jones likes this.