Unity-使用系统字体

1. 注意点
  1. 系统字为一个库,并不是每一种字体都支持你需要的文本,因此需要自己寻找一个适合的文本
1. 在UGUI.Text中使用
// 获得系统字体名称列表
string[] systemFontNames = Font.GetOSInstalledFontNames();
// 获得某种字体
into index = 0string systemFontName = systemFontNames[index];
Font font = Font.CreateDynamicFontFromOSFont(systemFontName, 36);
GetComponent<Text>().font = font;
2. 在UGUI.TMP_Text中使用

必须是TMP 3.2以上版本

// 获得系统字体名称列表
string[] systemFontNames = Font.GetOSInstalledFontNames();
// 获得某种字体
into index = 0string systemFontName = systemFontNames[index];
// 创建字体文件
var fontAsset = TMP_FontAsset.CreateFontAsset(systemFontName, "");
GetComponent<TMP_Text>().font = fontAsset;
3. 一个自动寻找对应字体支持的fallback管理器
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Framework;
using UnityEngine;
using TMPro;
using FontWeight = TMPro.FontWeight;
using Object = UnityEngine.Object;

public static class TMProFallbackExtra
{
    private static List<TMP_FontAsset> _fallbackFonts = new();
    private static bool _init;
    private static Coroutine _coroutine;

	// 清理数据
    public static void ClearAllData()
    {
        if (_coroutine != null)
        {
            try
            {
                GameManager.Instance.StopCoroutine(_coroutine);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }

        foreach (var tmpFontAsset in _fallbackFonts)
        {
            if (tmpFontAsset != null)
            {
                tmpFontAsset.ClearFontAssetData();
                Object.Destroy(tmpFontAsset);
            }
        }

        _fallbackFonts.Clear();
        _coroutine = null;
        _init = false;
    }

	// 初始化,每组字符串为一种需要兼容的语言,如日语可写「アイウエオ」
    public static void Init(params string[] testTxts)
    {
        if (_init)
        {
            Debug.LogError("初始化已经完成,因此没必要进行再次初始化!");
            return;
        }

        _coroutine = GameManager.Instance.StartCoroutine(InitAsync(testTxts));
    }

    private static IEnumerator InitAsync(params string[] testTxts)
    {
        yield return null;

        var systemFontNames = Font.GetOSInstalledFontNames();
        foreach (var testTxt in testTxts)
        {
            foreach (var systemFontName in systemFontNames)
            {
                var fontAsset = TMP_FontAsset.CreateFontAsset(systemFontName, "");

                if (fontAsset == null) continue;

                fontAsset.name = systemFontName;
                var data = CharacterToCoding(testTxt);
                var over = true;

                foreach (var d in data)
                {
                    var unicode = uint.Parse(d, NumberStyles.HexNumber);
                    if (TMP_FontAssetUtilities.GetCharacterFromFontAsset(unicode, fontAsset, false,
                            FontStyles.Normal, FontWeight.Regular, out _) != null) continue;
                    over = false;
                    break;
                }

                if (!over)
                {
                    Object.Destroy(fontAsset);
                    continue;
                }

                fontAsset.isMultiAtlasTexturesEnabled = true;
                _fallbackFonts.Add(fontAsset);
                Debug.Log($"为兼容字符串 {testTxt} ,添加Fallback字体 {systemFontName}");

                yield return null;
                break;
            }
        }

        _coroutine = null;
        _init = true;
    }

	// 清除字符串中的emoji
    public static string ClearEmoji(string str)
    {
        var sb = new StringBuilder();
        var data = CharacterToCoding(str);
        for (var j = 0; j < data.Length; j++)
        {
            var unicode = uint.Parse(data[j], NumberStyles.HexNumber);
            if (!((unicode > 0xE001 && unicode < 0xE05A) ||
                  (unicode > 0xE101 && unicode < 0xE15A) ||
                  (unicode > 0xE201 && unicode < 0xE253) ||
                  (unicode > 0xE301 && unicode < 0xE34D) ||
                  (unicode > 0xE401 && unicode < 0xE44C) ||
                  (unicode > 0xE501 && unicode < 0xE537) || unicode == 0xfffd))
            {
                sb.Append(str[j]);
            }
        }

        return sb.ToString();
    }

	// 为指定字体文件追加fallback,可从textfield上直接获取
    public static void AddFallback(TMP_FontAsset fontAsset)
    {
        if (!_init)
        {
            Debug.LogWarning("初始化尚未完成,将在稍后为字体文件添加Fallback");
            return;
        }

        foreach (var font in _fallbackFonts)
        {
            fontAsset.fallbackFontAssetTable.Add(font);
        }

        Debug.Log($"为字体文件 {fontAsset.name} 添加了Fallback");
    }

    public static void AddInputFieldClearEmoji(TMP_InputField input)
    {
        input.onValueChanged.RemoveAllListeners();
        input.onValueChanged.AddListener(str => { input.SetTextWithoutNotify(ClearEmoji(str)); });
    }

    private static string[] CharacterToCoding(string character)
    {
        var codings = new string[character.Length];

        for (var i = 0; i < character.Length; i++)
        {
            var bytes = Encoding.Unicode.GetBytes(character.Substring(i, 1));

            //取出二进制编码内容
            var lowCode = Convert.ToString(bytes[0], 16);
            //取出低字节编码内容(两位16进制)
            if (lowCode.Length == 1)
            {
                lowCode = "0" + lowCode;
            }

            var hightCode = Convert.ToString(bytes[1], 16);

            //取出高字节编码内容(两位16进制)
            if (hightCode.Length == 1)
            {
                hightCode = "0" + hightCode;
            }

            codings[i] = hightCode + lowCode;
        }

        return codings;
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莉萝爱萝莉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值