Search Unity

Question How to "state" different font materials through code?

Discussion in 'UGUI & TextMesh Pro' started by Knightey, Jan 20, 2024.

  1. Knightey

    Knightey

    Joined:
    May 19, 2023
    Posts:
    4
    Hello there!
    So, I'm working on a localization modification for the game made in Unity (as I stated in one of my previous posts), and I'm still fighting with changing fonts in the game to ones in our asset bundle (because original assets do not have Cyrillic characters), and this time, I've tried to broaden the variety of font materials, which we could use as a substitute.
    Alas, my new code is not working as intended, and I need help.

    The code I updated in tryings to make more font materials work:
    Code (CSharp):
    1. public static bool GetCyrillicFonts(string fontname, out TMP_FontAsset fontAsset)
    2. {
    3.     fontAsset = null;
    4.     if (tmpcyrillicfonts.Count == 0)
    5.         return false;
    6.     if (fontname == "BebasKai SDF" || fontname == "Liberation Sans SDF")
    7.     {
    8.         fontAsset = GetCyrillicFonts(0);
    9.         if (fontAsset.material.name.Contains("BebasKai SDF Burning"))
    10.             fontAsset.material = GetCyrillicMats(0);
    11.         else if (fontAsset.material.name.Contains("BebasKai SDF UnderLine"))
    12.             fontAsset.material = GetCyrillicMats(1);
    13.         else
    14.             fontAsset.material = GetCyrillicMats(2);
    15.         return true;
    16.     }
    17.     if (fontname == "Caveat Semibold SDF")
    18.     {
    19.         fontAsset = GetCyrillicFonts(1);
    20.         fontAsset.material = GetCyrillicMats(3);
    21.         return true;
    22.     }
    23.     if (fontname == "ExcelsiorSans SDF")
    24.     {
    25.         fontAsset = GetCyrillicFonts(2);
    26.         if (fontAsset.material.name.Contains("ExcelsiorSans SDF UnderLine"))
    27.             fontAsset.material = GetCyrillicMats(4);
    28.         else
    29.             fontAsset.material = GetCyrillicMats(5);
    30.         return true;
    31.     }
    32.     if (fontname.StartsWith("Corporate-Logo-Bold") || fontname == "Mikodacs SDF" || fontname == "KOTRA_BOLD SDF")
    33.     {
    34.         fontAsset = GetCyrillicFonts(3);
    35.         if (fontAsset.material.name.Contains("Mikodacs SDF Burning") || fontAsset.material.name.Contains("KOTRA_BOLD SDF Burnning_ver_2"))
    36.             fontAsset.material = GetCyrillicMats(6);
    37.         else if (fontAsset.material.name.Contains("Mikodacs SDF Select") || fontAsset.material.name.Contains("KOTRA_BOLD SDF Gacksung 2"))
    38.             fontAsset.material = GetCyrillicMats(7);
    39.         else if (fontAsset.material.name.Contains("Mikodacs SDF UnderLine") || fontAsset.material.name.Contains("KOTRA_BOLD SDF Underline"))
    40.             fontAsset.material = GetCyrillicMats(8);
    41.         else
    42.             fontAsset.material = GetCyrillicMats(9);
    43.         return true;
    44.     }
    45.     if (fontname == "Pretendard-Regular SDF" || fontname.StartsWith("HigashiOme - Gothic - C") || fontname.StartsWith("SCDream"))
    46.     {
    47.         fontAsset = GetCyrillicFonts(4);
    48.         if (fontAsset.material.name.Contains("SCDream5 Atlas CommonGlow") || fontAsset.material.name.Contains("Pretendard-Regular SDF CommonGlow"))
    49.             fontAsset.material = GetCyrillicMats(10);
    50.         else if (fontAsset.material.name.Contains("Pretendard-Regular SDF UnderLine") || fontAsset.material.name.Contains("SCDream5 Atlas Underline"))
    51.             fontAsset.material = GetCyrillicMats(11);
    52.         else
    53.             fontAsset.material = GetCyrillicMats(12);
    54.         return true;
    55.     }
    56.     return false;
    57. }
    The reference for part of the code:
    Code (CSharp):
    1. public static TMP_FontAsset GetCyrillicFonts(int idx)
    2. {
    3.     int Count = tmpcyrillicfonts.Count - 1;
    4.     if (Count < idx)
    5.         idx = Count;
    6.     return tmpcyrillicfonts[idx];
    7. }
    8. public static Material GetCyrillicMats(int idx)
    9. {
    10.     int Count = tmpcyrillicmats.Count - 1;
    11.     if (Count < idx)
    12.         idx = Count;
    13.     return tmpcyrillicmats[idx];
    14. }
    And the part which I think may be the reason my "upgrade" doesn't work:
    Code (CSharp):
    1. private static bool UpdateTMP(TextMeshProLanguageSetter __instance, LOCALIZE_LANGUAGE lang)
    2. {
    3.  
    4.     FontInformation fontInformation = __instance._fontInformation.Count > 0 ? __instance._fontInformation[0] : null;
    5.     if (fontInformation == null)
    6.         return false;
    7.     if (fontInformation.fontAsset == null)
    8.         return false;
    9.     if (__instance._text == null)
    10.         return false;
    11.     var raw_fontAsset = fontInformation.fontAsset;
    12.     bool use_ru = GetCyrillicFonts(raw_fontAsset.name, out var ru_fontAsset);
    13.  
    14.     var fontAsset = use_ru ? ru_fontAsset : fontInformation.fontAsset;
    15.     var fontMaterial = use_ru ? ru_fontAsset.material : fontInformation.fontMaterial ?? fontInformation.fontAsset.material;
    16.  
    17.     __instance._text.font = fontAsset;
    18.     __instance._text.fontMaterial = fontMaterial;
    19.  
    20.     if (__instance._matSetter != null)
    21.     {
    22.         __instance._matSetter.defaultMat = fontMaterial;
    23.         __instance._matSetter.ResetMaterial();
    24.         return false;
    25.     }
    26.     __instance.gameObject.TryGetComponent(out TextMeshProMaterialSetter textMeshProMaterialSetter);
    27.     if (textMeshProMaterialSetter != null)
    28.     {
    29.         textMeshProMaterialSetter.defaultMat = fontMaterial;
    30.         textMeshProMaterialSetter.ResetMaterial();
    31.     }
    32.     return false;
    33. }
    The biggest issue is that the original code is not mine (I'm far from being "fluent" in C# and working with Unity at all), but its author refuses to work on fonts at all. I hope, you can help me with this.

    I also post full file with the code if you need any more info for it.
     

    Attached Files: