OS Font to Asset [SOLVED]

I’m trying to see if it’s possible to take an OS font and duplicate/create it into my project folder without manually copying and pasting it into my project.

I’m sorry if this has already been answered, but I couldn’t find anything like it on here, apart from my previous attempt:

If anyone can tell me if this is possible or knows of any way of a work around similar to what I’m trying to do I would be very grateful.

Thanks

Is this for use with the Legacy UI.Text system or for use with TextMesh Pro?

It’s currently setup for Legacy.

When using the GetOSInstalledFontNames() function, does the font you are trying to work with show up in the returned array on that particular platform?

Yes.

What font is it and on which platform?

I’ve just been trying Arial as a test for WebGL in the Editor.

Let’s go back a few steps so that I clearly understand your needs.

You are trying to avoid having to include a font in your project. I assume for build size reasons. As such you want to use one of the OS fonts already on the device itself.

You are able to get a reference to the names of the fonts on the device by using the GetOSInstalledFontNames() function. You are also able to create a font by using the CreateDynamicFontFromOSFont() function.

Once the font is created using CreateDynamicFontFromOSFont, you should be fine provided you hold on to a reference to this font. As far as I know, on the device, you cannot make this font persistent which means every time the app runs, you will need to create this font using the above mechanism.

Is the issue that you are unable to create / use an OS font or the you are unable to make that into a persistent asset?

Ideally I want the fonts from the OS in my project as it will only be a few but they will differ each time.

If I use CreateDynamicFontFromOSFont() I can’t build as WebGL requires the fonts as an asset. Error: “Need to include font data on WebGL”

My main problems are:

  • Getting the WebGL build to grab fonts from the OS. If it can’t then I will just refer it to Arial.
  • Using GetOSInstalledFontNames() gets the font names but I’m guessing it would be more helpful to get the font filenames.

Why do you want the font in the project as oppose to simply being able to access the given font to display text with it?

I added a new function which is GetPathsToOSFonts() that will return the file path of all the fonts on the device. This might be useful for you.

I don’t think I can build to WebGL without the fonts as assets when just using CreateDynamicFontFromOSFont(). I get the error “Need to include font data on WebGL”.

GetPathsToOSFonts(), that will be handy. Do you know what version and when it will be released?

That should be in the next Unity release(s). Ie the next Unity 2018.4 / 2019.1 / 2019.2 and 2019.3. The new function was added to all of those.

That’s great Stephan thank you.

Out of curiosity do you have the code available for that function?

The implementation of that function lives on the native side of Unity and relies on other internal stuff so nothing to share there unfortunately.

OK cool no worries, grabbed the update today. Many thanks.

Is it possible to have an option so GetPathsToOSFonts() array is in the same order as GetOSInstalledFontNames() or vice-versa?

…or if there is anyway of relating the font names with the file names. Thanks.

If anyone falls into this situation (PC) you can solve it with System.Drawing.Text.PrivateFontCollection