unity3d - 如何在运行时加载平台特定字体

标签 unity3d mobile cross-platform

我正在尝试让应用程序设置一些基于平台的 Assets 。

第一步是拥有基于操作系统的字体,Android 的 Roboto 和 iOS 的 SanFrancisco。

简单来说,Unity无法在运行时导入字体,相对于其他跨平台IDE来说是一个很大的缺点,但我们不在这里评判。另一种解决方案是使用 AssetBundle,由于无法解释的原因,它无法为我完成最佳工作...

最重要的是,人们并未广泛设想将字体等基本 Assets 存储为字体以外的其他内容。

所以我的最终解决方案是这个丑陋的脚本:

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
         }
     }
 }

只是在寻找一种改进方法,因为从长远来看,这不是一个可行的解决方案。我什至不能在引用上使用宏,因为它们在切换时会丢失。

我在想一些预构建脚本,基本上,你是怎么做到的?

最佳答案

实际上,Unity 可以在运行时导入字体而不使用 AB,但只能使用操作系统的字体,方法是使用 Font.CreateDynamicFontFromOSFont

在 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 类型的字体

text.font = LoadFontFromOS.IsFontFound ? LoadFontFromOS.SelectedFont : text.font;

在需要的地方。

我在 Android 上测试过它,它可以工作,我没有 iOS 设备来测试它,但它应该可以正常工作。

关于unity3d - 如何在运行时加载平台特定字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49559491/

相关文章:

android - 可伸缩手机背景

java - J2ME 应用程序中 .jar 和 .jad 文件的统一

C++ 非平台相关的声音

c++ - C++如何绘制复杂的用户控件?

cross-platform - bazel 中依赖于平台的链接器标志(用于过剩)

visual-studio - 与 Unity 3D 一起使用时,Visual Studio 中缺少添加引用 - 需要 Npgsql.dll

c# - 如何停止 Vuforia 行为

c# - 我使用rigidbody2D.velocity.y时出现语法错误

javascript - Css - 移动位置固定,最终解决方案

macos - 安装 Unity 和 Android SDK for Mac 时,adb 在哪里?