Font file >> external import and Assignement in runtime.

Hello,

Made a tool for my team that need to deal with font files.
The font files can’t be drop into the Resources folder since it is for a stand alone build tool and they are free to load the font they want.

Searched on the web and didn’t found anything, most of the topic empty.
Even using the www method then casting Font doesn’t make it work…

Any idea if this can be achieved? any code sample?

As always thanks by advance!

Cheers :slight_smile:

Did you manage to find a solution for this?

Nothing!
Still no solution! :slight_smile:

loading fonts kind of works (using system.drawing), but how to convert System.Drawing.Font into UnityEngineFont or get the font data there…?

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using UnityEngine;
using UnityEngine.UI;
using Font = UnityEngine.Font;

public class LoadFont : MonoBehaviour
{
    public Text text; // testing with ui text

    // Use this for initialization
    void Start()
    {
        // 'PrivateFontCollection' is in the 'System.Drawing.Text' namespace
        var privateFontCollection = new PrivateFontCollection();
        // Provide the path to the font on the filesystem
        privateFontCollection.AddFontFile(@"C:\Windows\Fonts\comic.ttf");

        var myCustomFont = new System.Drawing.Font((FontFamily)privateFontCollection.Families[0], 36f);

        Debug.Log(myCustomFont);

        // Get the array of FontFamily objects.
        var fontFamilies = privateFontCollection.Families;

        // How many objects in the fontFamilies array?
        var count = fontFamilies.Length;

        // Display the name of each font family in the private collection
        // along with the available styles for that font family.
        for (int j = 0; j < count; ++j)
        {
            // Get the font family name.
            var familyName = fontFamilies[j].Name;

            // Is the regular style available?
            if (fontFamilies[j].IsStyleAvailable(System.Drawing.FontStyle.Regular))
            {
                string familyNameAndStyle = "";
                familyNameAndStyle = familyNameAndStyle + familyName;
                familyNameAndStyle = familyNameAndStyle + " Regular";

                System.Drawing.Font regFont = new System.Drawing.Font(
                   familyName,
                   16,
                   System.Drawing.FontStyle.Regular,
                   GraphicsUnit.Pixel);

                //e.Graphics.DrawString(familyNameAndStyle, regFont, solidBrush, 16);

                var f = new UnityEngine.Font(familyName);
                // Font f = (Font)regFont;

                // get font data ?
//                f.characterInfo

                text.font = f;
            }
        }

    }

}

In my situation I need a solution that’ll work for Android and iOS. As far as I know, System.Drawing isn’t compatible.

This can only read installed system font files, and if you just wanna to load system font, just use Font.GetOSInstalledFontNames(); to load All fonts installed . this can not load and local font file.

Hi,
Tried your script but it throws me error saying
ERROR : The type or namespace name ‘Text’ does not exist in the namespace ‘System.Drawing’ .
I’m using unity 2019.2.6 with .net 4.x for Android