我试图让应用程序基于平台设置一些资产. 第一步是使用基于操作系统的字体,适用于Android的Roboto和适用于iOS的SanFrancisco. 简单地说,Unity无法在运行时导入字体,与其他任何跨平台IDE相比都
第一步是使用基于操作系统的字体,适用于Android的Roboto和适用于iOS的SanFrancisco.
简单地说,Unity无法在运行时导入字体,与其他任何跨平台IDE相比都有一点缺点,但我们不在此判断.另一个解决方案是使用AssetBundle,由于无法解释的原因,我无法完成顶级工作……
最重要的是,没有广泛设想将字体等基本资产存储为字体以外的东西.
所以我的最终解决方案是这个丑陋的脚本:
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
}
}
}
只是想找到一种改进方法,从长远来看,这不是一个可行的解决方案.我甚至不能在引用上有宏,因为它们在切换时会丢失.
我在想一些预制脚本,基本上,你是怎么做到的?
实际上,通过使用Font.CreateDynamicFontFromOSFont,Unity可以在运行时导入字体而不使用AB,但只能导入OS的字体.在Android和旧金山iOS中加载Roboto字体的非常简单的脚本:
using UnityEngine;
public class LoadFontFromOS : MonoBehaviour {
const string ANDROID_FONT_NAME = "Roboto";
const string IOS_FONT_NAME = "SanFrancisco";
string[] OSFonts;
static Font selectedFont;
public static Font SelectedFont {
get {
return selectedFont;
}
}
static bool isFontFound;
public static bool IsFontFound {
get {
return isFontFound;
}
}
private void Awake() {
isFontFound = false;
OSFonts = Font.GetOSInstalledFontNames();
foreach (var font in OSFonts) {
#if UNITY_ANDROID
if (font == ANDROID_FONT_NAME) {
selectedFont = Font.CreateDynamicFontFromOSFont(ANDROID_FONT_NAME, 1);
isFontFound = true;
}
#elif UNITY_IOS
if (font == IOS_FONT_NAME) {
selectedFont = Font.CreateDynamicFontFromOSFont(IOS_FONT_NAME, 1);
isFontFound = true;
}
#endif
}
}
}
然后,您可以使用简单更改所有文本类型的字体
text.font = LoadFontFromOS.IsFontFound ? LoadFontFromOS.SelectedFont : text.font;
需要的地方.
我在Android上测试它并且它可以工作,我没有iOS设备来测试它,但它应该工作相同.
