Change font using tags using fonts that are loaded at runtime

Hello,

I’m working on a system that can load in fonts and text at runtime that comes from a content management system. I would like the text to be able to handle to many different tags that are available in TMP, such as , , , and . I have them all working except for , which might just be a limitation of how the tag works.

Here is my code that I use to load in the fonts:

Font loadedFont = new Font(fontFilePath);
TMP_FontAsset conduit = TMP_FontAsset.CreateFontAsset(loadedFont);
// note, I don't actually know what fonts are coming from the CMS, this is just an example of how I load them
// do again to load font Argent

Then, as an example, I have a TextMeshProUGUI component that has it’s normal font set to “conduit” and somewhere in the text I want it to switch to “Argent” just for a word, I would do something like this:

myTmpfield.text = "This font is conduit, but <font="Argent">this font is Argent.</font>";

Normally, “Argent” has to be a font found in the Resources folder (and named exactly that so that it finds it), but this font is coming in at runtime - with the idea that our clients can change the font via the CMS, so it won’t always be Conduit and Argent, it might be two completely different fonts. Is this possible or is the tag limited to only fonts that are in the resources folder?

Thanks!

Currently, the <font=“Name of Font Asset”> uses Resource.Load() to try to load this resource which has to be contained in a Resources folder at the location specific in the TMP Settings which by default is “Resources/Fonts & Materials”.

A few releases ago I added

/// <summary>
/// Event delegate to allow custom loading of TMP_FontAsset when using the <font="Font Asset Name"> tag.
/// </summary>
public static event Func<int, string, TMP_FontAsset> OnFontAssetRequest;

Using this would allow to hook into this where it is called before TMP tries to load a font asset. This way you can handle the loading however you need and then provide the resources to TMP.

Awesome - where can I find this setting, I thought it would be in the TMP_FontAsset class or the TextMeshProUGUI class but I can’t seem to locate it. I’m using Unity 2018.4.23 and TMP 1.4.1.

TMP_Text base class.

Thank you, seems to only be in TMP 1.5.0 for Unity 2018.4.x which is why I was having a hard time finding it.

Indeed. This was recently added :wink:

Hello,

Long years later I need to do the exact same thing as the author.
I have added to the OnFontAssetRequest event to return always the same font with name “MyFont” which is initialized and used widely already in the game.
I’m using the tag <font=“MyFont”>xxxxx in a TextMeshProUGUI text property.

The event is properly fired when the TextMeshProUGUI is rendered; and provides as name the “MyFont”.

TMP_Text.OnFontAssetRequest += (pIndex, pName) => {
                TMP_FontAsset lResult = this.currentFontAsset;
                Debug.Log(string.Format("Received index:{0}, name:{1}, current Font Asset:{2}. Always returning current font.", pIndex, pName, lResult?.name));
                return lResult;
            };

But the font is not loaded; Would you know what is needed in that scenario ?

My guess;
I would have expect that the event is allowing to return a customized font depending the given parameters. I’ve simplified the implementation to return always the same font to verify if it’s working properly.

Thanks, regards.