Search Unity

Loading Fallback CJK font in webGL app from AssetBundle

Discussion in 'UGUI & TextMesh Pro' started by gashraf_hiperware, Jul 11, 2020.

  1. gashraf_hiperware

    gashraf_hiperware

    Joined:
    Jul 11, 2020
    Posts:
    1
    What is the best way to setup a fallback font that I do not want to include in the webGL build? We already have a mature app using legacy Text components. Upgrading all of these to TextMeshPro components is not a great option due to our tight deadline for next release. Our app was so far catering to an an English speaking audience, but now we need to localize it to many different languages (including Chinese, Japanese and Korean -- CJK). CJK fonts tend to be huge, and we do not want to package it in our app. Instead, we want to dynamically load it from a remote AssetBundle, and make it available as a fallback font to the main font used all over the app (in text components).

    Now, I have already tested the setting up of static fallback fonts that work nicely. However, when I use the name of the dynamically loaded font, e.g. Arial Unicode MS, I am seeing blanks being rendered in the text elements. I created an AssetBundle for the above font, uploaded it to a remote server, tested that I am fetching the AssetBundle correctly and loading the font... But I am stuck at this step as I see no concrete examples for loading fallback fonts, instead of explicitly setting each text element to the loaded font.

    I would really appreciate some helpful pointers on what I am missing out on. My main font for all text elements is set to Calibri, and in the setup of the Calibri font, I have added "Arial Unicode MS" as a font fallback name (but deleted the Arial font itself from the project otherwise the webgl build force includes it and bloats the build size). Here are the main steps of my font loading code, where I would like the Arial font to be loaded for language sessions that are Chinese, Japanese or Korean, and automatically fallback for the default Calibri style text elements:

    private static IEnumerator _coLoadFont()
    {
    string fontBundleName = "fontbundle_arialuni";
    string fontName = "ARIALUNI";
    string filePath = System.IO.Path.Combine(url, fontBundleName);​

    using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(filePath, 1, 0))​
    {
    Debug.Log("Waiting to load from " + filePath);
    yield return uwr.SendWebRequest();
    if (uwr.isNetworkError || uwr.isHttpError)
    {
    Debug.Log("Encountered font asset bundle fetch error for " + filePath);
    Debug.Log(uwr.error);
    yield break;​
    }
    else
    {
    // Get downloaded asset bundle
    assetBundle = DownloadHandlerAssetBundle.GetContent(uwr);
    Debug.Log("Got assetBundle from " + filePath);​
    }​
    }

    Debug.Log("Trying to load universal arial font!!!");
    AssetBundleRequest asset = assetBundle.LoadAssetAsync<Font>(fontName);
    yield return asset;//wait till loaded
    Debug.Log("Loaded universal arial font!!!");​
    }​
    }
     
    Last edited: Jul 11, 2020