- Home /
Platform dependent asset reference
I am trying to make the app sets some of the assets based on platform.
First step is to have the font based on OS, Roboto for Android and SanFrancisco for iOS.
Simply said, Unity cannot import font at runtime, quite a bit of a drawback compared to any other cross-platform IDE but we not here to judge. The other solution is to use AssetBundle which for unexplanable reasons fails top work for me...
On top of that, it is not widely conceived to store basic assets like font as something else than font.
So my final solution is this ugly script:
class FontFamily
{
[Header("Android")]
[SerializeField]
private Font regularAndroid = null;
[SerializeField]
private Font boldAndroid = null;
[SerializeField]
private Font italicAndroid = null;
[Header("iOS")]
[SerializeField]
private Font regularIOS = null;
[SerializeField]
private Font boldIOS = null;
[SerializeField]
private Font italicIOS = null;
public Font Regular
{
get
{
#if UNITY_ANDROID
return this.regularAndroid;
#elif UNITY_IOS
return this.regularIOS;
#endif
}
}
public Font Bold
{
get
{
#if UNITY_ANDROID
return this.boldAndroid;
#elif UNITY_IOS
return this.boldIOS;
#endif
}
}
public Font Italic
{
get
{
#if UNITY_ANDROID
return this.italicAndroid;
#elif UNITY_IOS
return this.italicIOS;
#endif
}
}
}
Just looking for a way to improve that, coz in the long run this is not a viable solution. I can't even have the macros on the references as they get lost while switching.
I was thinking of some prebuild script maybe, basically, how do you do that?
Cheers
It may look complicated, but this kind of technique is used all the time, I think this is one of the quickest solutions
I doubt it is used all the time since that would mean you get duplicated items that are never used on the other platforms.
There is an actual solution I got from SO.
For icons, I can easily download them as png so the issue is not so problematic.
For the font, which was the initial problem since Unity cannot load them at runtime, there is a solution I got on stack overflow:
using:
https://docs.unity3d.com/ScriptReference/Font.CreateDynamicFontFromOSFont.html