1

This font needs to be dynamically changed so I can't put it in Resources.

I haven't found any resources or way to achieve this, all talk about Resources.

EDIT: I did look into AssetBunble but it looked overkill so I thought maybe requiring the user to install the font on his machine but Font.CreateDynamicFontFromOSFont does nothing. Whatever the given input (even invalid), it will always return the same "Arial" font and nothing visually change.

I might look into AssetBundles again but I don't want Unity installed on the target machine and if I understood right, you can't build an assetbundle without running Unity.

jeromej
  • 10,508
  • 2
  • 43
  • 62
  • You can load almost everything from Resources folder or AssetsBundle have you tried to do it like that? – buffalo94 Jun 04 '18 at 13:38
  • @buffalo94 I need it dynamic (user provided) so it can't be put into the Resources. I'm investigating the Asset bundle solution but it seems to require Unity to build one (and the user isn't expected to have Unity installed). Ideally, he should just provide the font file and voilà. – jeromej Jun 04 '18 at 13:55
  • I don't unity will let you load an arbitrary file and use it as font because almost all Unity asset are serialized at build time – buffalo94 Jun 04 '18 at 14:12
  • @buffalo94 Unity supports streaming audios and videos and I also managed to download sprites (for textures or not) albeit it was trickier. Giving the ability to the user to chose a font of his choice seem trivial? I don't expect it to be as optimized as it isn't included in the build. – jeromej Jun 04 '18 at 14:17
  • You're right for video, audio, textures but is have heard somewhere unity support loading font from an arbitrary file? I don't think so! So I doubt that it's feasible – buffalo94 Jun 04 '18 at 14:27
  • @buffalo94 There is still the custom fonts you can build from a png and map it tediously but i wouldn't be able to use any font I want without parsing it somehow anyway. – jeromej Jun 04 '18 at 15:55
  • Unfortunately Unity is source code closed you need to wait for Unity to implement it or find a workaround – buffalo94 Jun 04 '18 at 17:34

1 Answers1

2

There is no Unity API to load standard font from disk or web into Unity's Font.

If you use Font.CreateDynamicFontFromOSFont, the font must be installed on the device. This is easy to do on some platforms like Windows but extremely hard on devices like Android.


The recommended way to do this is to use AssetBundles. You will need the Unity Editor to create the AssetBundles but you don't need it to load the AssetBundles during run-time. Put the fonts in AssetBundles then build it and host it on the server. Use UnityWebRequest API to download the Assetbundle then extract the font from it. You can always update this Assetbundle on the server.

If you want the user to to able to load the font from any source, you can automate the building part by asking the user where the font is, take that font and send/upload it to your server with the UnityWebRequest API. On the server side, run Unity Editor in a headless mode, retrieve the font sent to it and build an Assetbundle from it then send it back to the player and load it on the client side.

So, UnityWebRequestAssetBundle.GetAssetBundle is used to download the AssetBundle and AssetBundle.LoadAssetAsync is used to the fonts in it.

There is no other way to do this unless you have the Unity source code which is really expensive. By doing it this way, you won't need the font to be installed and won't run into issues on mobile devices. This post shows how to build and load an Assetbundle but you have to do that in headless mode.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hard mode! It is true that this way you aren't requiring the user to have Unity installed to make the bundle himself. – jeromej Jun 05 '18 at 07:29
  • I'll let it sip for a while and accept your answer if nothing better comes up. – jeromej Jun 05 '18 at 07:30
  • No problem. I wish there was a better way to do this too but I don't think so. We hard to resort to this after researching and experimenting with Unity. – Programmer Jun 05 '18 at 07:33
  • If someone would host such service that could also be great. :) (Granted it doesn't already exist and granted that's a legal use of Unity to expose it publicly albeit I fail to see why not) – jeromej Jun 05 '18 at 07:47
  • I think it will likely go against Unity's terms of services if shared since you are doing the operation with the Editor and not the build. It should be fine when you are using it for your own game but not giving it as a service to others. If that's okay then people will also make a similar remote service that lets others to build Unity project with a pro version and remove the Unity splash screen. – Programmer Jun 05 '18 at 07:55
  • I haven't thought about it before but any idea if TextMeshPro could provide a solution here? – jeromej Jun 20 '18 at 09:35
  • Nope. I haven't used that before but it I don't think it has a function to load from a disc or memory. Your only hope is what's in this answer or make a feature request and ask Unity to add functions to be used to load font from a path or from the memory just like you can load images with the Texture2D class. – Programmer Jun 20 '18 at 10:37