Search Unity

Question Changing TMPro Font while Game is Paused Makes All Letters be White Squares

Discussion in 'UGUI & TextMesh Pro' started by RedVonix, Feb 26, 2023.

  1. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    I'm working on a project and we've discovered that if the user changes the language while the game is paused, then unpauses, any text that was on the screen when the language changed happen will show as all white boxes. I understand this is because of a material mismatch, but looking at the objects the material is applied just fine. This more seems to be an issue that happens if the Timescale is set to 0, and TMPro fails to properly update the material.

    Here's the code we're using to perform this. Keep in mind, the issue only happens if the game is paused (Timescale is set to 0), and works totally fine if the timescale is not set to 0. (Setting the timescale to 1 while the game is paused just to change the font is a bit weird as it causes everything to progress for a moment so that's not an option here):

    Code (CSharp):
    1.            
    2.         private void Localize()
    3.         {
    4.             var tmpro = GetComponent<TextMeshProUGUI>();
    5.             tmpro.font = LocalizationManager.GetFont();
    6.  
    7.             InitDefaultFontSize();
    8.  
    9.             float multiplier = LocalizationManager.GetTextSizeMultiplier();
    10.             tmpro.fontSize = _defaultFontSize.Value * multiplier;
    11.             tmpro.fontSizeMin = _defaultFontSizeMin.Value * multiplier;
    12.             tmpro.fontSizeMax = _defaultFontSizeMax.Value * multiplier;
    13.             tmpro.text = LocalizationManager.Localize(LocalizationIdentifierReference.Id);
    14.         }
    15.  
    16.         private void InitDefaultFontSize()
    17.         {
    18.             if (!_defaultFontSize.HasValue)
    19.             {
    20.                 var tmpro = GetComponent<TextMeshProUGUI>();
    21.                 _defaultFontSize = tmpro.fontSize;
    22.                 _defaultFontSizeMin = tmpro.fontSizeMin;
    23.                 _defaultFontSizeMax = tmpro.fontSizeMax;
    24.             }
    25.         }
    26.  
    Is there an update function or something that will properly force TMPro to update its values, or is there a way to make it work regardless of the timescale? I've been stuck on this one for a bit so any help would be appreciated. Thanks!
     
  2. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    Finally resolved this. The issue is that TextMeshPro is not properly refreshing its material, and no rebuild command will allow it to rebuild. So - you just have to nullify then re-set the material on the TMPro object. (I'd still however call this a bug in TMPro.)

    Code (CSharp):
    1.             var tmpro = GetComponent<TextMeshProUGUI>();
    2.             TMP_FontAsset font = tmpro.font;
    3.             tmpro.font = null;          
    4.             tmpro.font = font;