Unity使用TextMeshPro加载Emoji

前言

        我在网上搜索“TextMeshPro如何加载Emoji”,找到这篇文章Unity中使用TextMeshPro打出Emoji表情。文章中说要使用TextMeshProPre版本。但实际上我们可以不升级版本也能够让TextMeshPro加载Emoji。

环境

    Unity2022.3.8f1c1
    Windows10
    TextMeshPro 3.0.9(3.0.6版本也可以)

实现

        正如文章下面的评论所说,我们直接修改TextMeshPro的代码就可以实现一样的功能。但是如果你真的按照他说的方法做后,你会得到下面的警告:

1
2
3
4
5
The package cache was invalidated and rebuilt because the following immutable asset(s) were unexpectedly altered:
Packages/com.unity.textmeshpro/Scripts/Editor/TMP_SpriteAssetImporter.cs

Import Error Code:(4)
Message: Build asset version error: packages/com.unity.textmeshpro/scripts/editor/tmp_spriteassetimporter.cs in SourceAssetDB has modification time of '2024-05-31T07:49:00.8515411Z' while content on disk has modification time of '2024-05-17T04:35:48Z'

然后你就会发现,你修改的代码又被改回去了。我并没有去找如何修改这个的方法,但是我想大不了就按照这个思路重新写一份导入器算了。于是就有了下面这段代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using TMPro;
using TMPro.EditorUtilities;
using TMPro.SpriteAssetUtilities;
using UnityEditor;
using UnityEngine;

namespace SelfEditorTool
{
public class SelfTMPEditor : EditorWindow
{
// Create Sprite Asset Editor Window
[MenuItem("SelfEditorTool/TextMeshPro/Sprite Importer", false, 2026)]
public static void ShowFontAtlasCreatorWindow()
{
var window = GetWindow<SelfTMPEditor>();
window.titleContent = new GUIContent("Sprite Importer");
window.Focus();
}

Texture2D m_SpriteAtlas;
SpriteAssetImportFormats m_SpriteDataFormat = SpriteAssetImportFormats.TexturePackerJsonArray;
TextAsset m_JsonFile;

string m_CreationFeedback;

TMP_SpriteAsset m_SpriteAsset;

/// <summary>
///
/// </summary>
void OnEnable()
{
// Set Editor Window Size
SetEditorWindowSize();
}

/// <summary>
///
/// </summary>
public void OnGUI()
{
DrawEditorPanel();
}

/// <summary>
///
/// </summary>
private void OnDisable()
{
// Clean up sprite asset object that may have been created and not saved.
if (m_SpriteAsset != null && !EditorUtility.IsPersistent(m_SpriteAsset))
DestroyImmediate(m_SpriteAsset);
}

/// <summary>
///
/// </summary>
void DrawEditorPanel()
{
// label
GUILayout.Label("Import Settings", EditorStyles.boldLabel);

EditorGUI.BeginChangeCheck();

// Sprite Texture Selection
m_JsonFile = EditorGUILayout.ObjectField("Sprite Data Source", m_JsonFile, typeof(TextAsset), false) as TextAsset;

m_SpriteDataFormat = (SpriteAssetImportFormats)EditorGUILayout.EnumPopup("Import Format", m_SpriteDataFormat);

// Sprite Texture Selection
m_SpriteAtlas = EditorGUILayout.ObjectField("Sprite Texture Atlas", m_SpriteAtlas, typeof(Texture2D), false) as Texture2D;

if (EditorGUI.EndChangeCheck())
{
m_CreationFeedback = string.Empty;
}

GUILayout.Space(10);

GUI.enabled = m_JsonFile != null && m_SpriteAtlas != null && m_SpriteDataFormat != SpriteAssetImportFormats.None;

// Create Sprite Asset
if (GUILayout.Button("Create Sprite Asset"))
{
m_CreationFeedback = string.Empty;

// Clean up sprite asset object that may have been previously created.
if (m_SpriteAsset != null && !EditorUtility.IsPersistent(m_SpriteAsset))
DestroyImmediate(m_SpriteAsset);

// Read json data file
if (m_JsonFile != null)
{
switch (m_SpriteDataFormat)
{
case SpriteAssetImportFormats.TexturePackerJsonArray:
TexturePacker_JsonArray.SpriteDataObject jsonData = null;
try
{
jsonData = JsonUtility.FromJson<TexturePacker_JsonArray.SpriteDataObject>(m_JsonFile.text);
}
catch
{
m_CreationFeedback = "The Sprite Data Source file [" + m_JsonFile.name + "] appears to be invalid or incorrectly formatted.";
}

if (jsonData != null && jsonData.frames != null && jsonData.frames.Count > 0)
{
int spriteCount = jsonData.frames.Count;

// Update import results
m_CreationFeedback = "<b>Import Results</b>\n--------------------\n";
m_CreationFeedback += "<color=#C0ffff><b>" + spriteCount + "</b></color> Sprites were imported from file.";

// Create new Sprite Asset
m_SpriteAsset = CreateInstance<TMP_SpriteAsset>();

// Assign sprite sheet / atlas texture to sprite asset
m_SpriteAsset.spriteSheet = m_SpriteAtlas;

List<TMP_SpriteGlyph> spriteGlyphTable = new List<TMP_SpriteGlyph>();
List<TMP_SpriteCharacter> spriteCharacterTable = new List<TMP_SpriteCharacter>();

PopulateSpriteTables(jsonData, spriteCharacterTable, spriteGlyphTable);

var curType = m_SpriteAsset.GetType();
FieldInfo fieldInfo = curType.GetField("m_SpriteCharacterTable", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(m_SpriteAsset, spriteCharacterTable);
//m_SpriteAsset.spriteCharacterTable = spriteCharacterTable;
fieldInfo = curType.GetField("m_SpriteGlyphTable", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(m_SpriteAsset, spriteGlyphTable);
//m_SpriteAsset.spriteGlyphTable = spriteGlyphTable;
}
break;
}
}
}

GUI.enabled = true;

// Creation Feedback
GUILayout.Space(5);
GUILayout.BeginVertical(EditorStyles.helpBox, GUILayout.Height(60));
{
EditorGUILayout.TextArea(m_CreationFeedback, TMP_UIStyleManager.label);
}
GUILayout.EndVertical();

GUILayout.Space(5);
GUI.enabled = m_JsonFile != null && m_SpriteAtlas && m_SpriteAsset != null;
if (GUILayout.Button("Save Sprite Asset") && m_JsonFile != null)
{
string filePath = EditorUtility.SaveFilePanel("Save Sprite Asset File", new FileInfo(AssetDatabase.GetAssetPath(m_JsonFile)).DirectoryName, m_JsonFile.name, "asset");

if (filePath.Length == 0)
return;

SaveSpriteAsset(filePath);
}
GUI.enabled = true;
}

/// <summary>
///
/// </summary>
/// <param name="spriteDataObject"></param>
/// <param name="spriteCharacterTable"></param>
/// <param name="spriteGlyphTable"></param>
private static void PopulateSpriteTables(TexturePacker_JsonArray.SpriteDataObject spriteDataObject, List<TMP_SpriteCharacter> spriteCharacterTable, List<TMP_SpriteGlyph> spriteGlyphTable)
{
List<TexturePacker_JsonArray.Frame> importedSprites = spriteDataObject.frames;

float atlasHeight = spriteDataObject.meta.size.h;

for (int i = 0; i < importedSprites.Count; i++)
{
TexturePacker_JsonArray.Frame spriteData = importedSprites[i];

TMP_SpriteGlyph spriteGlyph = new TMP_SpriteGlyph();
spriteGlyph.index = (uint)i;

spriteGlyph.metrics = new UnityEngine.TextCore.GlyphMetrics((int)spriteData.frame.w, (int)spriteData.frame.h, -spriteData.frame.w * spriteData.pivot.x, spriteData.frame.h * spriteData.pivot.y, (int)spriteData.frame.w);
spriteGlyph.glyphRect = new UnityEngine.TextCore.GlyphRect((int)spriteData.frame.x, (int)(atlasHeight - spriteData.frame.h - spriteData.frame.y), (int)spriteData.frame.w, (int)spriteData.frame.h);
spriteGlyph.scale = 1.0f;

spriteGlyphTable.Add(spriteGlyph);

TMP_SpriteCharacter spriteCharacter = new TMP_SpriteCharacter(0, spriteGlyph);
spriteCharacter.name = spriteData.filename.Split('.')[0];
spriteCharacter.unicode = (uint)TMP_TextUtilities.StringHexToInt(spriteCharacter.name);
spriteCharacter.scale = 1.0f;

spriteCharacterTable.Add(spriteCharacter);
}
}

/// <summary>
///
/// </summary>
/// <param name="filePath"></param>
void SaveSpriteAsset(string filePath)
{
filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath.

string dataPath = Application.dataPath;

if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1)
{
Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\"");
return;
}

string relativeAssetPath = filePath.Substring(dataPath.Length - 6);
string dirName = Path.GetDirectoryName(relativeAssetPath);
string fileName = Path.GetFileNameWithoutExtension(relativeAssetPath);
string pathNoExt = dirName + "/" + fileName;

// Save Sprite Asset
AssetDatabase.CreateAsset(m_SpriteAsset, pathNoExt + ".asset");

// Set version number
//m_SpriteAsset.version = "1.1.0";
var curType = m_SpriteAsset.GetType();
FieldInfo fieldInfo = curType.GetField("m_Version", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(m_SpriteAsset, "1.1.0");

// Compute the hash code for the sprite asset.
m_SpriteAsset.hashCode = TMP_TextUtilities.GetSimpleHashCode(m_SpriteAsset.name);

// Add new default material for sprite asset.
AddDefaultMaterial(m_SpriteAsset);
}

/// <summary>
/// Create and add new default material to sprite asset.
/// </summary>
/// <param name="spriteAsset"></param>
static void AddDefaultMaterial(TMP_SpriteAsset spriteAsset)
{
Shader shader = Shader.Find("TextMeshPro/Sprite");
Material material = new Material(shader);
material.SetTexture(ShaderUtilities.ID_MainTex, spriteAsset.spriteSheet);

spriteAsset.material = material;
material.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset(material, spriteAsset);
}

/// <summary>
/// Limits the minimum size of the editor window.
/// </summary>
void SetEditorWindowSize()
{
EditorWindow editorWindow = this;

Vector2 currentWindowSize = editorWindow.minSize;

editorWindow.minSize = new Vector2(Mathf.Max(230, currentWindowSize.x), Mathf.Max(300, currentWindowSize.y));
}
}
}

我几乎照搬了TextMeshPro的实现,某些变量因为是访问权限的问题我改成了反射赋值,然后我还修改了其unicode的赋值。最终我尝试了一下,在PC出包是可以正常打印出Emoji表情。但是其他平台的情况我就不知道了。

一些Emoji相关的网址地址

推特的Emoji图片资源下载 EmojiAll中文官方网站 文章中提供的Emoji图片资源下载

一点吐槽

        我在五月初就想这篇文章了,但是因为一些原因我拖到了五月底才开始写。没想到Unity竟然将TextMeshPro的版本更新到了3.09。我本以为它终于更新这个导入器,结果我看了一下更新日志根本就没有提及。还有在TextMeshPro-Pre版本中,它的导入器写法就是我上述的写法。但是现在这个版本中其导入器的写法竟然是直接赋值一个一定会被他们忽略的定值。而他们给的例子中的Emoji竟然又没有这样的问题。这个真是让我有些疑惑。

        而且无论是哪个版本在Emoji做组合时,它总是将这组合拆开来显示。比如这个Emoji🏇🏻(如果你的电脑显示不了,可以去看一下这个网站)。