Search Unity

dynamic font in input font tag

Discussion in 'UGUI & TextMesh Pro' started by roimil, Dec 7, 2020.

  1. roimil

    roimil

    Joined:
    Nov 17, 2017
    Posts:
    7
    Hi, if i create a dynamic font at runtime by an OS font, can it be used in an input field tag ?
    for example :
    <font="myDynamicFont">some text</font>
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    The newly created font asset will not be found.

    However, you could use the OnFontAssetRequest delegate to get a callback whenever TMP is trying to load a font asset via <font> tag where you could return this new font asset.

    Alternatively, after you create this font asset, you could use the following function to register your font asset.
    Code (csharp):
    1.  
    2. MaterialReferenceManager.AddFontAsset(newFontAsset);
    3.  
    Note: I do have plans to add a Resource Manager to TMP which will simplify this process. I am also planning on adding a new font asset type which will be "Dynamic OS" which I have described in a few posts on the forum already where this new font asset type will also simplify this process.
     
  3. roimil

    roimil

    Joined:
    Nov 17, 2017
    Posts:
    7
    thank you for replying
    i tried :
    Code (CSharp):
    1. MaterialReferenceManager.AddFontAsset(newFontAsset);
    but it did not work, in an input field i tried the <font="theFontName"> but it was not recognized.

    maybe i am doing something wrong
    this is the way i create the font asset :
    Code (CSharp):
    1. Font osFont = new Font(fontPaths[i]);
    2. TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(osFont, 90, 10, UnityEngine.TextCore.LowLevel.GlyphRenderMode.SDFAA, 1024, 1024, AtlasPopulationMode.Dynamic);
    3. fontAsset.name = "newfont";
    4.  
    5. MaterialReferenceManager.AddFontAsset(fontAsset);
    can you please make a short example of how to use the : OnFontAssetRequest delegate ?
    thank you very much
     
    dr4 likes this.
  4. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    108
    This shows up on google when looking for examples, so to help future generations:

    Code (CSharp):
    1.        
    2.        // assign your font
    3.         public TMP_FontAsset font;
    4.  
    5.         private void Start()
    6.         {
    7.             TMP_Text.OnFontAssetRequest += TMP_Text_OnFontAssetRequest;
    8.  
    9.         }
    10.  
    11.         private TMP_FontAsset TMP_Text_OnFontAssetRequest(int arg1, string arg2)
    12.         {
    13.              // args2 is the name that you are using on <font="this name"> if you need it
    14.             return font;
    15.         }