Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 0347cf9

Browse files
committed
重构第一版
1 parent 918b81a commit 0347cf9

File tree

6 files changed

+160
-7
lines changed

6 files changed

+160
-7
lines changed

Assets/Examples/Scene/Text.unity

264 Bytes
Binary file not shown.

Assets/TextInlineSprite/Scripts/InlineRender/EmojiRenderGroup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ void RenderRebuild()
368368
for (int j = 0; j < emojidata.Count; ++j)
369369
{
370370
IFillData taginfo = emojidata[j];
371+
if (taginfo == null || taginfo.ignore)
372+
continue;
371373
Graphic job = Parse(text, taginfo, joblist);
372374
if (job)
373375
{
@@ -427,6 +429,8 @@ void PlayAnimation()
427429
for (int j = 0; j < emojidata.Count; ++j)
428430
{
429431
IFillData taginfo = emojidata[j];
432+
if (taginfo == null || taginfo.ignore)
433+
continue;
430434
SpriteAsset asset = null;
431435
SpriteInfoGroup groupinfo = manager.FindSpriteGroup(taginfo.Tag, out asset);
432436
if (groupinfo != null && groupinfo.spritegroups.Count > 1)

Assets/TextInlineSprite/Scripts/InlineRender/UnitRender.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ void RenderRebuild()
256256
for (int j = 0; j < emojidata.Count; ++j)
257257
{
258258
IFillData taginfo = emojidata[j];
259+
if (taginfo == null || taginfo.ignore)
260+
continue;
259261
SpriteGraphic job = Parse(text, taginfo, joblist);
260262
if (job)
261263
joblist.Add(job);
@@ -328,6 +330,9 @@ void PlayAnimation()
328330
for (int j = 0; j < emojidata.Count; ++j)
329331
{
330332
IFillData taginfo = emojidata[j];
333+
if (taginfo == null || taginfo.ignore)
334+
continue;
335+
331336
SpriteAsset asset = null;
332337
SpriteInfoGroup groupinfo = manager.FindSpriteGroup(taginfo.Tag, out asset);
333338
if (groupinfo != null && groupinfo.spritegroups.Count > 1)

Assets/TextInlineSprite/Scripts/InlineText.cs

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ public class InlineText : Text
2323
private static StringBuilder _textBuilder = new StringBuilder();
2424
private static UIVertex[] m_TempVerts = new UIVertex[4];
2525
private static Vector3[] m_TagVerts = new Vector3[2];
26+
/// <summary>
27+
/// Security usually means additional performance overhead. If you control the bounding box yourself, this calculation may be redundant.
28+
/// </summary>
29+
public static bool safeMode = true;
2630

27-
private TextGenerator _SpaceGen;
2831
private InlineManager _InlineManager;
2932
//文本表情管理器
3033
public InlineManager Manager
@@ -46,6 +49,120 @@ public InlineManager Manager
4649
List<IFillData> _renderTagList;
4750
private string _lasttext ;
4851
private string _outputText = "";
52+
private float? _pw;
53+
54+
public override float preferredWidth
55+
{
56+
get
57+
{
58+
if (_pw == null)
59+
{
60+
//its override from uGUI Code ,but has bug?
61+
62+
//var settings = GetGenerationSettings(Vector2.zero);
63+
//return cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
64+
65+
//next idea
66+
Vector2 extents = rectTransform.rect.size;
67+
68+
var settings = GetGenerationSettings(extents);
69+
cachedTextGenerator.Populate(_outputText, settings);
70+
71+
if (cachedTextGenerator.lineCount > 1)
72+
{
73+
float? minx = null;
74+
float? maxx = null;
75+
IList<UIVertex> verts = cachedTextGenerator.verts;
76+
int maxIndex = cachedTextGenerator.lines[1].startCharIdx;
77+
78+
for (int i = 0, index = 0; i < verts.Count; i += 4, index++)
79+
{
80+
UIVertex v0 = verts[i];
81+
UIVertex v2 = verts[i + 1];
82+
float min = v0.position.x;
83+
float max = v2.position.x;
84+
85+
if (minx.HasValue == false)
86+
{
87+
minx = min;
88+
}
89+
else
90+
{
91+
minx = Mathf.Min(minx.Value, min);
92+
}
93+
94+
if (maxx.HasValue == false)
95+
{
96+
maxx = max;
97+
}
98+
else
99+
{
100+
maxx = Mathf.Max(maxx.Value, max);
101+
}
102+
103+
if (index > maxIndex)
104+
{
105+
break;
106+
}
107+
}
108+
109+
_pw = (maxx - minx);
110+
}
111+
else
112+
{
113+
//_pw = cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
114+
float? minx = null;
115+
float? maxx = null;
116+
IList<UIVertex> verts = cachedTextGenerator.verts;
117+
int maxIndex = cachedTextGenerator.characterCount;
118+
119+
for (int i = 0, index = 0; i < verts.Count; i += 4, index++)
120+
{
121+
UIVertex v0 = verts[i];
122+
UIVertex v2 = verts[i + 1];
123+
float min = v0.position.x;
124+
float max = v2.position.x;
125+
126+
if (minx.HasValue == false)
127+
{
128+
minx = min;
129+
}
130+
else
131+
{
132+
minx = Mathf.Min(minx.Value, min);
133+
}
134+
135+
if (maxx.HasValue == false)
136+
{
137+
maxx = max;
138+
}
139+
else
140+
{
141+
maxx = Mathf.Max(maxx.Value, max);
142+
}
143+
144+
if (index > maxIndex)
145+
{
146+
break;
147+
}
148+
}
149+
150+
_pw = (maxx - minx);
151+
}
152+
153+
}
154+
return _pw.Value;
155+
}
156+
}
157+
158+
public override float preferredHeight
159+
{
160+
get
161+
{
162+
var settings = GetGenerationSettings(new Vector2(GetPixelAdjustedRect().size.x, 0.0f));
163+
return cachedTextGeneratorForLayout.GetPreferredHeight(_outputText, settings) / pixelsPerUnit;
164+
}
165+
}
49166

50167
void OnDrawGizmos()
51168
{
@@ -70,7 +187,7 @@ protected override void Start()
70187
public override void SetVerticesDirty()
71188
{
72189
base.SetVerticesDirty();
73-
if (Application.isPlaying)
190+
if (Application.isPlaying && this.isActiveAndEnabled)
74191
{
75192
if (!Manager)
76193
{
@@ -156,7 +273,6 @@ protected override void OnPopulateMesh(VertexHelper toFill)
156273
roundingOffset = PixelAdjustPoint(roundingOffset) - roundingOffset;
157274
toFill.Clear();
158275

159-
160276
int nextfilldata = -1;
161277
int fillindex = -1;
162278
int startfilldata = -1;
@@ -250,9 +366,35 @@ protected override void OnPopulateMesh(VertexHelper toFill)
250366
}
251367

252368
m_DisableFontTextureRebuiltCallback = false;
369+
//
370+
371+
if(safeMode)
372+
{
373+
CalBoundsInSafe();
374+
}
253375

254376
}
255377

378+
void CalBoundsInSafe()
379+
{
380+
if(_renderTagList != null && _renderTagList.Count>0)
381+
{
382+
Rect rect = rectTransform.rect;
383+
for (int i = _renderTagList.Count - 1; i >= 0; i--)
384+
{
385+
IFillData data = _renderTagList[i];
386+
if (rect.Contains(data.pos[1]) && rect.Contains(data.pos[3]))
387+
{
388+
data.ignore = false;
389+
}
390+
else
391+
{
392+
data.ignore = true;
393+
}
394+
}
395+
}
396+
}
397+
256398
void FillNextTag(ref int startfilldata,ref int nextfilldata,ref int fillindex)
257399
{
258400
if(_renderTagList != null && fillindex >=0)

Assets/TextInlineSprite/Scripts/ParseData.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public interface IFillData
1515
Vector3[] pos { get; set; }
1616
Vector2[] uv { get; set; }
1717

18+
bool ignore { get; set; }
19+
1820
int Fill(Vector3 start, Vector3 end);
1921

2022
int GetFillCnt();
@@ -40,6 +42,8 @@ public class SpriteTagInfo:IFillData
4042

4143
public Vector2 Size { get; set; }
4244

45+
public bool ignore { get; set; }
46+
4347
public string Tag { get; set; }
4448

4549
public void FillIdxAndPlaceHolder(int idx, int tagidx,int fillcnt)
@@ -67,8 +71,6 @@ public int Fill(Vector3 start,Vector3 end)
6771
int index = GetPositionIdx();
6872
if (index >= 0)
6973
{
70-
float w = Mathf.Abs(end.x - start.x);
71-
float h = Mathf .Abs( end.y - start.y);
7274
Vector3 center = (start + end) / 2;
7375
pos[3] = center + new Vector3(-Size.x / 2,-Size.y / 2, 0);
7476
pos[2] = center + new Vector3(Size.x / 2, -Size.y / 2, 0);

Assets/TextInlineSprite/Scripts/Parser/EmojiParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public bool ParsetContent(InlineText text,StringBuilder textfiller, Match data,i
105105
if (index != -1)
106106
{
107107
string subId = value.Substring(1, index - 1);
108-
if (subId.Length > 0 && !int.TryParse(subId, out atlasId))
108+
if (subId.Length > 0 && int.TryParse(subId, out atlasId))
109109
{
110-
Debug.LogErrorFormat("{0} convert failed ", subId);
110+
//Debug.LogErrorFormat("{0} convert success ", subId);
111111
}
112112
else if (subId.Length > 0)
113113
{

0 commit comments

Comments
 (0)