1. 注意点
- 系统字为一个库,并不是每一种字体都支持你需要的文本,因此需要自己寻找一个适合的文本
// 获得系统字体名称列表
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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?