Specifically it's Font.CreateDynamicFontFromOSFont(), use this script attached to an empty GameObject in the scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestFontTexture : MonoBehaviour {
void Start ()
{
var font = Font.CreateDynamicFontFromOSFont("Consolas", 12);
font.RequestCharactersInTexture("HelloWorld", 20);
SaveTexture2PNG("./fontTexture.png", (Texture2D)font.material.mainTexture);
}
void Update ()
{
}
void SaveTexture2PNG(string path, Texture2D tex)
{
byte[] bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(path, bytes);
}
}
After click play it should create a fontTexture.png file containing character glyphs in the Assets folder, but only in editor though. In the built game it just created a completely black image.
Since it's OS concerned, I'm using Windows 7 with font Consolas installed and Unity 5.6.5f.
How to make it work in a built game?
CreateDynamicFontFromOSFont
. Your game won't work if the player doesn't have that font installed. Different operating systems have different fonts, so you will unnecessarily limit the platforms you can release for. It is usually better to add any fonts you want to use as assets. But keep the license conditions in mind. Not all fonts included in Windows allow redistribution. \$\endgroup\$