Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Loading fonts during runtime from streamingAssets folder

Discussion in 'Scripting' started by dullman, Apr 11, 2016.

  1. dullman

    dullman

    Joined:
    Jan 20, 2015
    Posts:
    29
    Hello i want to load a font during runtime from the "ttf" file provided by user, so it would be in streamingAsset folder, is there any way to do that ??
    In other way is there any method in unity which gets a provided filename and return font object?
     
  2. NKlawitter

    NKlawitter

    Joined:
    May 19, 2015
    Posts:
    5
    I'm also interested in this functionality. As far as I can tell, the best you can do for runtime fonts is to use the installed system fonts with... Font.GetOSInstalledFontNames() and Font.CreateDynamicFontFromOSFont().
    But if would seem a small leap from this to simply referencing a .ttf file.
     
  3. cowlinator

    cowlinator

    Joined:
    Mar 15, 2012
    Posts:
    69
    I need this functionality for my Unity-WebGL game.
    But Font.GetOSInstalledFontNames() and Font.CreateDynamicFontFromOSFont() do not work on WebGL. :(
    Anybody know how to load a font from streamingAssets (or even from any url) at runtime?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I'm not an expert in this, but I've only seen two ways to load fonts into the game from external files- one is through AssetBundles and the other is using WWW combined with the internal 2DToolkit font builder class. I'm uncertain is either has further restrictions when it comes to WebGL, as I've never built a WebGL application, and the latter option is not free (and not worth the cost if this is the only reason you'd need it).

    That's really all of the information I can offer on this subject, but good luck!
     
  5. HinxLai

    HinxLai

    Joined:
    Dec 31, 2017
    Posts:
    4
    Maybe like this, bro.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class DM_DynamicFontCraetion : MonoBehaviour
    6. {
    7.     private Text prefab;
    8.  
    9.     void Start()
    10.     {
    11.         prefab = GetComponentInChildren<Text>();
    12.         prefab.gameObject.SetActive(false);
    13.         StartCoroutine(GenerateFontAndTxt());
    14.     }
    15.  
    16.     int index;
    17.     private IEnumerator GenerateFontAndTxt()
    18.     {
    19.         var allInstalledFonts = Font.GetOSInstalledFontNames();
    20.         foreach (var fontName in allInstalledFonts)
    21.         {
    22.             index++;
    23.             CreateFontobj(fontName);
    24.             yield return new WaitForEndOfFrame();
    25.         }
    26.         print("Instantiate finished.");
    27.         Resources.UnloadUnusedAssets();
    28.         yield return null;
    29.         UnityEditor.EditorApplication.isPaused = true;
    30.     }
    31.  
    32.     private void CreateFontobj(string fontName)
    33.     {
    34.         var font = Font.CreateDynamicFontFromOSFont(fontName, 24);
    35.         var copy = Instantiate(prefab.gameObject, transform);
    36.         copy.name = index + " : " + fontName;
    37.         copy.SetActive(true);
    38.         var txt = copy.GetComponent<Text>();
    39.         txt.text = fontName;
    40.         txt.font = font;
    41.         var x = index / 20;
    42.         var z = index % 20;
    43.  
    44.         txt.transform.localPosition = new Vector3(250 * x, 0, 250 * z);
    45.     }
    46. }
    47.