Skip to content

Commit 3d474fa

Browse files
committed
Fixed emoji length not match bug.
1 parent 70cbc31 commit 3d474fa

1 file changed

Lines changed: 77 additions & 1 deletion

File tree

Assets/GText/GText.cs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,44 @@ public class GText : Text, IPointerClickHandler
2525
Dictionary<int, SpriteInfo> emojis = new Dictionary<int, SpriteInfo>();
2626
List<HrefInfo> hrefs = new List<HrefInfo>();
2727
readonly UIVertex[] m_TempVerts = new UIVertex[4];
28+
string m_outputText = "";
2829

2930
public delegate void HrefClickHandler(string arg);
3031
private event HrefClickHandler m_OnHrefClick = null;//can use toLua directly, so not UnityEvent.
3132
#endregion
3233

34+
#region Accerssor
35+
public override float preferredWidth
36+
{
37+
get
38+
{
39+
var settings = GetGenerationSettings(Vector2.zero);
40+
return cachedTextGeneratorForLayout.GetPreferredWidth(m_outputText, settings) / pixelsPerUnit;
41+
}
42+
}
43+
public override float preferredHeight
44+
{
45+
get
46+
{
47+
var settings = GetGenerationSettings(new Vector2(rectTransform.rect.size.x, 0.0f));
48+
return cachedTextGeneratorForLayout.GetPreferredHeight(m_outputText, settings) / pixelsPerUnit;
49+
}
50+
}
51+
52+
public override string text
53+
{
54+
get { return m_Text; }
55+
set
56+
{
57+
m_Text = value;
58+
if(emojis == null)
59+
LoadEmojiData();
60+
ParseTextTag(value);
61+
}
62+
}
63+
64+
#endregion
65+
3366
#region Private
3467
protected override void OnPopulateMesh(VertexHelper toFill)
3568
{
@@ -50,7 +83,7 @@ protected override void OnPopulateMesh(VertexHelper toFill)
5083
Vector2 extents = rectTransform.rect.size;
5184

5285
var settings = GetGenerationSettings(extents);
53-
cachedTextGenerator.Populate(text, settings);
86+
cachedTextGenerator.Populate(m_outputText, settings);
5487

5588
Rect inputRect = rectTransform.rect;
5689

@@ -270,6 +303,7 @@ string ParseTextInTag(string inputText)
270303
}
271304
stringBuilder.Append(inputText.Substring(indexText, inputText.Length - indexText));
272305
hrefBuilder.Append(inputText.Substring(indexText, inputText.Length - indexText));
306+
273307
return hrefBuilder.ToString();
274308
}
275309

@@ -283,6 +317,9 @@ void ParseTextTag(string inputText)
283317
emojis.Clear();
284318

285319
MatchCollection matchs = Regex.Matches(inputText, "\\[[a-z0-9A-Z]+\\]");
320+
321+
#region Fixed Bug Tag 's Length More Then 3 Can't Get Right Width or Height
322+
/*
286323
for (int i = 0; i < matchs.Count; i++)
287324
{
288325
var match = matchs[i];
@@ -306,6 +343,45 @@ void ParseTextTag(string inputText)
306343
};
307344
hrefs.Add(hrefInfo);
308345
}
346+
*/
347+
#endregion
348+
349+
if (matchs.Count > 0)
350+
{
351+
StringBuilder builder = new StringBuilder();
352+
int textIndex = 0;
353+
for (int i = 0; i < matchs.Count; i++)
354+
{
355+
var match = matchs[i];
356+
SpriteInfo info;
357+
if (EmojiIndex.TryGetValue(match.Value, out info))
358+
{
359+
builder.Append(inputText.Substring(textIndex, match.Index - textIndex));
360+
int temIndex = builder.Length;
361+
builder.Append("[$]");
362+
info.len = 3;
363+
emojis.Add(temIndex, info);
364+
textIndex = match.Index + match.Length;
365+
}
366+
}
367+
builder.Append(inputText.Substring(textIndex, inputText.Length - textIndex));
368+
m_outputText = builder.ToString();
369+
}
370+
else
371+
m_outputText = inputText;
372+
373+
matchs = Regex.Matches(m_outputText, @"<material=([^>]*)>(.+?)</material>");
374+
for (int i = 0; i < matchs.Count; i++)
375+
{
376+
var match = matchs[i];
377+
var hrefInfo = new HrefInfo
378+
{
379+
startIndex = match.Index * 4, // hyperLink vecs start index
380+
endIndex = match.Index * 4 + (match.Length - 1) * 4 + 3,
381+
url = match.Groups[1].Value
382+
};
383+
hrefs.Add(hrefInfo);
384+
}
309385
}
310386

311387
// Hyper Bounds

0 commit comments

Comments
 (0)