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