您当前的位置: 首页 >  unity

莉萝爱萝莉

暂无认证

  • 11浏览

    0关注

    58博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity-使用系统字体

莉萝爱萝莉 发布时间:2022-09-28 19:42:59 ,浏览量:11

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

必须是TMP 3.2以上版本

// 获得系统字体名称列表
string[] systemFontNames = Font.GetOSInstalledFontNames();
// 获得某种字体
into index = 0;
string systemFontName = systemFontNames[index];
// 创建字体文件
var fontAsset = TMP_FontAsset.CreateFontAsset(systemFontName, "");
GetComponent().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 _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  0xE001 && unicode  0xE101 && unicode  0xE201 && unicode  0xE301 && unicode  0xE401 && unicode  0xE501 && unicode             
关注
打赏
1663903574
查看更多评论
0.1523s