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

Commit d271716

Browse files
committed
fix autosize && width
1 parent 72a599f commit d271716

File tree

4 files changed

+92
-25
lines changed

4 files changed

+92
-25
lines changed
0 Bytes
Binary file not shown.
-1.28 KB
Binary file not shown.

Assets/TextInlineSprite/Scripts/InlineRender/EmojiRenderGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ void SetAllGroupDirty(bool value)
661661
CanvasGraphicGroup group = GraphicTasks[i];
662662
if (group != null)
663663
{
664-
group.isDirty = true;
664+
group.isDirty = value;
665665
}
666666
}
667667
}

Assets/TextInlineSprite/Scripts/InlineText.cs

Lines changed: 91 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class InlineText : Text, IPointerClickHandler
2525
private static StringBuilder _textBuilder;
2626
private static UIVertex[] m_TempVerts = new UIVertex[4];
2727
private static TextGenerator _UnderlineText;
28+
private TextGenerator _SpaceGen;
2829
private List<Vector3> _listVertsPos;
2930
private InlineManager _InlineManager;
3031
//文本表情管理器
@@ -118,8 +119,45 @@ public override float preferredWidth
118119
_pw = (maxx - minx);
119120
}
120121
else
121-
{
122-
_pw = cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
122+
{
123+
//_pw = cachedTextGeneratorForLayout.GetPreferredWidth(_OutputText, settings) / pixelsPerUnit;
124+
float? minx = null;
125+
float? maxx = null;
126+
IList<UIVertex> verts = cachedTextGenerator.verts;
127+
int maxIndex = cachedTextGenerator.characterCount;
128+
129+
for (int i = 0, index = 0; i < verts.Count; i += 4, index++)
130+
{
131+
UIVertex v0 = verts[i];
132+
UIVertex v2 = verts[i + 1];
133+
float min = v0.position.x;
134+
float max = v2.position.x;
135+
136+
if (minx.HasValue == false)
137+
{
138+
minx = min;
139+
}
140+
else
141+
{
142+
minx = Mathf.Min(minx.Value, min);
143+
}
144+
145+
if (maxx.HasValue == false)
146+
{
147+
maxx = max;
148+
}
149+
else
150+
{
151+
maxx = Mathf.Max(maxx.Value, max);
152+
}
153+
154+
if (index > maxIndex)
155+
{
156+
break;
157+
}
158+
}
159+
160+
_pw = (maxx - minx);
123161
}
124162

125163
}
@@ -369,20 +407,27 @@ void CalcQuadInfo()
369407
if ((pos + 4) > _listVertsPos.Count)
370408
continue;
371409

372-
for (int m = pos; m < pos + 4; m++)
373-
{
374-
info._Pos[m - pos] = _listVertsPos[m];
375-
}
410+
Vector3 p1 = _listVertsPos[pos];
411+
412+
info._Pos[0] = p1 ;
413+
info._Pos[1] = _listVertsPos[pos+1];
414+
info._Pos[2] = _listVertsPos[pos+2];
415+
info._Pos[3] = _listVertsPos[pos+3];
416+
417+
//info._Pos[0] = p1 + new Vector3(0, info._Size.y, 0);
418+
//info._Pos[1] = p1 + new Vector3(info._Size.x, info._Size.y, 0);
419+
//info._Pos[2] = p1 + new Vector3(info._Size.x, 0, 0);
420+
//info._Pos[3] = p1;
421+
422+
376423
}
377424
}
378425

379426
}
380427

381-
382-
#region 处理超链接的包围盒
383428
void CalcBoundsInfo(VertexHelper toFill, TextGenerationSettings settings)
384429
{
385-
#region 包围框
430+
386431
if(_ListHrefInfos != null && _ListHrefInfos.Count >0)
387432
{
388433

@@ -424,7 +469,7 @@ void CalcBoundsInfo(VertexHelper toFill, TextGenerationSettings settings)
424469
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
425470
}
426471

427-
#region 添加下划线
472+
428473
if(_UnderlineText == null)
429474
{
430475
_UnderlineText = new TextGenerator();
@@ -468,17 +513,8 @@ void CalcBoundsInfo(VertexHelper toFill, TextGenerationSettings settings)
468513

469514
}
470515
}
471-
472-
#endregion
473-
}
474-
475-
#endregion
476-
477-
516+
}
478517
}
479-
#endregion
480-
481-
#region 根据正则规则更新文本
482518

483519
public List<SpriteTagInfo> PopEmojiData()
484520
{
@@ -538,7 +574,39 @@ bool ParseEmoji(string newInfo,int Index, int Id, string TagName, Match match, r
538574
_textBuilder.Append(newInfo.Substring(_textIndex, match.Index - _textIndex));
539575
int _tempIndex = _textBuilder.Length * 4;
540576

541-
float autosize = Mathf.Min(tagSprites.size, this.rectTransform.rect.height);
577+
float autosize = Mathf.Min(tagSprites.size, this.rectTransform.rect.height-8);
578+
579+
if(_SpaceGen == null)
580+
{
581+
Vector2 extents = rectTransform.rect.size;
582+
583+
TextGenerationSettings settings = GetGenerationSettings(extents);
584+
585+
_SpaceGen = new TextGenerator();
586+
//two sapceing
587+
_SpaceGen.Populate(" ", settings);
588+
}
589+
590+
//IList<UIVertex> spaceverts = _SpaceGen.verts;
591+
//float spacewid = spaceverts[1].position.x - spaceverts[0].position.x;
592+
//float spaceheight = spaceverts[0].position.y - spaceverts[3].position.y;
593+
//float deltawid = spaceverts[4].position.x - spaceverts[1].position.x;
594+
//float spacesize = Mathf.Max(spacewid, spaceheight);
595+
596+
//int fillspacecnt = Mathf.RoundToInt( autosize / spacesize);
597+
//if(autosize > spacesize)
598+
//{
599+
// fillspacecnt = Mathf.RoundToInt((autosize + deltawid) / (spacesize+ deltawid));
600+
//}
601+
//else
602+
//{
603+
// fillspacecnt = Mathf.RoundToInt(autosize / spacesize);
604+
//}
605+
//for(int i =0; i < fillspacecnt;i++)
606+
//{
607+
// _textBuilder.Append(" ");
608+
//}
609+
542610
_textBuilder.AppendFormat("<quad material=0 x={0} y={1} size={2} width={3} />", tagSprites.x, tagSprites.y, autosize, tagSprites.width);
543611

544612
if (RenderTagList.Count > Index)
@@ -548,7 +616,7 @@ bool ParseEmoji(string newInfo,int Index, int Id, string TagName, Match match, r
548616
{
549617
_tempSpriteTag._ID = Id;
550618
_tempSpriteTag._Tag = TagName;
551-
_tempSpriteTag._Size = new Vector2(tagSprites.size * tagSprites.width, tagSprites.size);
619+
_tempSpriteTag._Size = new Vector2(autosize * tagSprites.width, autosize);
552620
_tempSpriteTag._Position = _tempIndex;
553621
_tempSpriteTag._UV = tagSprites.spritegroups[0].uv;
554622
}
@@ -559,7 +627,7 @@ bool ParseEmoji(string newInfo,int Index, int Id, string TagName, Match match, r
559627
{
560628
_ID = Id,
561629
_Tag = TagName,
562-
_Size = new Vector2(tagSprites.size * tagSprites.width, tagSprites.size),
630+
_Size = new Vector2(autosize * tagSprites.width, autosize),
563631
_Pos = new Vector3[4],
564632
_Position = _tempIndex,
565633
_UV = tagSprites.spritegroups[0].uv
@@ -652,7 +720,6 @@ private string GetOutputText(string newinfo)
652720
_textBuilder.Append(newinfo.Substring(_textIndex, newinfo.Length - _textIndex));
653721
return _textBuilder.ToString();
654722
}
655-
#endregion
656723

657724

658725
#region 点击事件检测是否点击到超链接文本

0 commit comments

Comments
 (0)