Create TMPro Font asset at runtime

Hi,

I am looking to use the OS font for different devices and I’m struggling to understand how I can convert the Font data type into a TMPFont_Asset which I can assign to a TextMeshProUGUI field at runtime.

The font can only be retrieved at runtime and so I cannot pre-create the fonts using tmp’s font creator tools.

I have this so far:

private void Awake()
    {
        string[] systemFontNames = Font.GetOSInstalledFontNames();

        // Find the Roboto Font
        string fontName = "Roboto";

        foreach (string font in systemFontNames)
        {
            if (font.Contains(fontName))
            {
                fontName = font;
                break;
            }
        }

        Font systemFont = Font.CreateDynamicFontFromOSFont(fontName, 36);

        //MenuManager.instance.settingPanelTitle.font = systemFont;
    }

Thanks!

There are a few posts about this already. Here is one of those posts / threads .

// Get paths to OS Fonts
string[] fontPaths = Font.GetPathsToOSFonts();

// Create new font object from one of those paths
Font osFont = new Font(fontPaths[index]);

// Create new dynamic font asset
TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(osFont);

Having said that, Dynamic OS Font Asset support is coming which will make this process even easier. The post linked above also contains information about that.