Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question (Bug?) LocalizedFontEvent NOT Applying Font on Startup for IL2CPP Build

Discussion in 'Localization Tools' started by Reimirno7, Jun 16, 2023.

  1. Reimirno7

    Reimirno7

    Joined:
    Nov 25, 2021
    Posts:
    51
    We are using LocalizedFontEvent from this thread: https://forum.unity.com/threads/localize-font.898421/ (the relevant script is also included at the end of this post.)

    We are having a weird issue where in IL2CPP build (for windows), LocalizedFontEvent won't apply font to TMP component on start up.That is, when you start the game, the TMP component it controls is still using the default TMP font asset. Correct font will be only applied after you switch to another locale (correct font for that locale would be applied. If you switch back, correct font for this locale would be applied. So we know that font asset is included in the build.)

    I have tried to bring LocalizedFontEvent.cs before default in script execution order but that does not help.

    Font is the only thing we are localizing with Unity Localization package. Not localizing strings or other assets using Localization package.

    This bug behaviour is only present in IL2CPP builds. It works fine in editor and in mono builds.

    This is preventing us from releasing the product. Please advise. Thank you!

    Unity Version: 2021.3.27f1
    Localization Package: 1.3.2

    Code (CSharp):
    1. /*
    2. LocalizeTMPFontEvent.cs
    3.  
    4. Description: Modified from https://forum.unity.com/threads/localize-font.898421/
    5. Created: Monday, December 27 2021
    6. Unity Version: 2021.3.27f1
    7. */
    8.  
    9. using System;
    10. using TMPro;
    11. using UnityEngine;
    12. using UnityEngine.Events;
    13. using UnityEngine.Localization;
    14. using UnityEngine.Localization.Components;
    15.  
    16. namespace Epicomic.Reimirno.PlayUtilities.I18N
    17. {
    18.     [Serializable]
    19.     public class LocalizedTMPFont : LocalizedAsset<TMP_FontAsset> { }
    20.  
    21.     [Serializable]
    22.     public class UnityEventTMPFont : UnityEvent<TMP_FontAsset> { }
    23.  
    24.     [AddComponentMenu("Localization/Asset/Localize TMP Font Event")]
    25.     public class LocalizeTMPFontEvent
    26.         : LocalizedAssetEvent<TMP_FontAsset, LocalizedTMPFont, UnityEventTMPFont> { }
    27.  
    28.     [Serializable]
    29.     internal class UnityEventMaterial : UnityEvent<Material> { }
    30.  
    31.     internal class LocalizeFontMaterialEvent
    32.         : LocalizedAssetEvent<Material, LocalizedMaterial, UnityEventMaterial> { }
    33. }
    34.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
  3. Reimirno7

    Reimirno7

    Joined:
    Nov 25, 2021
    Posts:
    51

    What do you mean by "the log file from the player build"? Editor.log when I build IL2cpp?

    - I opened a fresh unity editor.
    - I built addressable using default build script.
    - I triggered a il2cpp clean build.
    - I then go to console, and "open editor log"

    Is this `Editor.log` what you are looking for?
     

    Attached Files:

  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    The log file from the il2cpp player build, when the font is not being applied, I want to check There's no errors or unexpected messages that may explain why it's not working.
     
  5. Reimirno7

    Reimirno7

    Joined:
    Nov 25, 2021
    Posts:
    51
    I see.

    - I cleaned up my custom Debug.Logs and rebuilt IL2CPP.
    - I started the built game, and was taken to the menu scene. It shows Chinese locale on menu scene. On menu scene, font is not applied.
    - I navigate to in-game setting panel, switch to English locale, and now font for English is applied.
    - I close the game, and get this `Player.log`.

    I don't see any error related to font. The only warning that is related to font is
    ```
    The character with Unicode value \u2468 was not found in the [优设标题圆 SDF] font asset or any potential fallbacks. It was replaced by Unicode character \u25A1 in text object [ButtonText].
    ```

    But "优设标题圆 SDF" is the font serialized on button prefab. It is missing a character but that's not the point. The problem is that LocalizeFontEvent should have replaced it with another font asset already.
     

    Attached Files:

  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    I can see a NullReferenceException is occurring in a TMP component during Update material.
    What version of TMP are you using, is there an update you can try?
     
  7. Reimirno7

    Reimirno7

    Joined:
    Nov 25, 2021
    Posts:
    51
    I am on 3.0.6 which is the latest available as shown in pacakge manager. It seems there are later version (after 3.0.6 we have 4.0.0) available by looking at the changelog but those are still in preview and not meant for unity 2021.

    The nullreference happens only when I closes the application, not when game loads. So maybe it is not related?
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    I'm not sure what it could be then. Does the issue happen in a new project? Can you share a project with the issue so I can try and debug it?
     
  9. Reimirno7

    Reimirno7

    Joined:
    Nov 25, 2021
    Posts:
    51
    Thank you so much.

    Yup, I will try reproducing it from a fresh project.

    For those who encountered the same issue: I am currently working around this by including a script that changes and changes back locale after `await
    LocalizationSettings.InitializationOperation`. Something like this:

    Code (CSharp):
    1.  
    2.         private void IL2CPPLocaleHotFix()
    3.         {
    4.             var curLocaleIdx = LocalizationSettings.AvailableLocales.Locales.IndexOf(
    5.                 LocalizationSettings.SelectedLocale
    6.             );
    7.             if (curLocaleIdx == -1)
    8.             {
    9.                 curLocaleIdx = 0;
    10.             }
    11.             var localCount = LocalizationSettings.AvailableLocales.Locales.Count;
    12.             var nextLocaleIdx = (curLocaleIdx + 1) % localCount;
    13.             LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[
    14.                 nextLocaleIdx
    15.             ];
    16.             LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[
    17.                 curLocaleIdx
    18.             ];
    19.         }
    20.  
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Have you tried the latest version of the package 1.4.3? If it's not visible in the package manager you can edit the manifest.json file in the package folder

    We fixed a bug with asset tables preloading that would immediately release the asset causing it to unload, that sounds like it could be it.