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

Commit ce989c2

Browse files
committed
增加超链接
1 parent 37ae7ff commit ce989c2

File tree

3 files changed

+95
-24
lines changed

3 files changed

+95
-24
lines changed

Assets/TextInlineSprite/Examples/Scene/Text 1.unity

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,23 +426,23 @@ MonoBehaviour:
426426
m_BestFit: 0
427427
m_MinSize: 10
428428
m_MaxSize: 40
429-
m_Alignment: 0
429+
m_Alignment: 1
430430
m_AlignByGeometry: 1
431431
m_RichText: 1
432432
m_HorizontalOverflow: 0
433433
m_VerticalOverflow: 0
434434
m_LineSpacing: 1
435435
m_Text: "<color=black>\u9759\u6001\u8868\u60C5:</color>\nNew Text<quad size=24 width=1
436-
/>\nNew Text<quad size=24 width=1 />\nNew Text<quad size=24 width=1 />\nNew Text<quad
437-
size=24 width=1 />\nNew Text<quad size=24 width=1 />0\nNew Text<quad size=24 width=1
438-
/>00000000"
436+
/>\nNew Text<quad size=24 width=1 />\nNew T0000000ext<quad size=24 width=1 />\nNew
437+
Text<quad size=24 width=1 />\nNew Text<quad size=24 width=1 />0\nNew Text<quad
438+
size=24 width=1 />00000000"
439439
OnHrefClick:
440440
m_PersistentCalls:
441441
m_Calls: []
442442
m_TypeName: InlineText+HrefClickEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral,
443443
PublicKeyToken=null
444444
_text: "<color=black>\u9759\u6001\u8868\u60C5:</color>\nNew Text[#emoji_0]\nNew
445-
Text[#emoji_1]\nNew Text[#emoji_2]\nNew Text[#emoji_3]\nNew Text[#emoji_4]0\nNew
445+
Text[#emoji_1]\nNew T0000000ext[#emoji_2]\nNew Text[#emoji_3]\nNew Text[#emoji_4]0\nNew
446446
Text[#emoji_5]00000000"
447447
--- !u!222 &1366302172
448448
CanvasRenderer:
@@ -563,8 +563,6 @@ MonoBehaviour:
563563
m_Script: {fileID: 11500000, guid: 0ae7019e071d5fe4e9d442887a3ac0de, type: 3}
564564
m_Name:
565565
m_EditorClassIdentifier:
566-
_isStatic: 0
567-
_animationSpeed: 5
568566
_spriteGraphics:
569567
- {fileID: 1546337849}
570568
--- !u!222 &1670842538
@@ -641,13 +639,14 @@ MonoBehaviour:
641639
m_HorizontalOverflow: 0
642640
m_VerticalOverflow: 0
643641
m_LineSpacing: 1
644-
m_Text: 00000<quad size=24 width=1 />0000<quad size=24 width=1 />00000
642+
m_Text: 0000000000000<color=blue>[00000000000000000000000]</color>00<quad size=24
643+
width=1 />0000<quad size=24 width=1 />00000
645644
OnHrefClick:
646645
m_PersistentCalls:
647646
m_Calls: []
648647
m_TypeName: InlineText+HrefClickEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral,
649648
PublicKeyToken=null
650-
_text: 00000[#emoji_2]0000[#emoji_6]00000
649+
_text: 0000000000000[-1#00000000000000000000000]00[#emoji_2]0000[#emoji_6]00000
651650
--- !u!222 &1707421984
652651
CanvasRenderer:
653652
m_ObjectHideFlags: 0

Assets/TextInlineSprite/Scripts/InlineText.cs

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ protected override void OnPopulateMesh(VertexHelper toFill)
111111
//更新顶点位置&去掉乱码uv
112112
m_DisableFontTextureRebuiltCallback = true;
113113
int index = -1;
114+
//emoji
114115
for (int i = 0; i < _spriteInfo.Count; i++)
115116
{
116117
index = _spriteInfo[i].Index;
@@ -128,6 +129,39 @@ protected override void OnPopulateMesh(VertexHelper toFill)
128129

129130
}
130131
}
132+
// 处理超链接包围框
133+
for (int i = 0; i < _listHrefInfos.Count; i++)
134+
{
135+
_listHrefInfos[i].Boxes.Clear();
136+
if (_listHrefInfos[i].StartIndex >= toFill.currentVertCount)
137+
continue;
138+
139+
toFill.PopulateUIVertex(ref _tempVertex, _listHrefInfos[i].StartIndex);
140+
// 将超链接里面的文本顶点索引坐标加入到包围框
141+
var pos = _tempVertex.position;
142+
var bounds = new Bounds(pos, Vector3.zero);
143+
for (int j = _listHrefInfos[i].StartIndex+1; j < _listHrefInfos[i].EndIndex; j++)
144+
{
145+
if (j >= toFill.currentVertCount)
146+
{
147+
break;
148+
}
149+
toFill.PopulateUIVertex(ref _tempVertex, j);
150+
pos = _tempVertex.position;
151+
if (pos.x < bounds.min.x)
152+
{
153+
// 换行重新添加包围框
154+
_listHrefInfos[i].Boxes.Add(new Rect(bounds.min, bounds.size));
155+
bounds = new Bounds(pos, Vector3.zero);
156+
}
157+
else
158+
{
159+
bounds.Encapsulate(pos); // 扩展包围框
160+
}
161+
}
162+
//添加包围盒
163+
_listHrefInfos[i].Boxes.Add(new Rect(bounds.min, bounds.size));
164+
}
131165
m_DisableFontTextureRebuiltCallback = false;
132166

133167
UpdateDrawnSprite();
@@ -207,8 +241,8 @@ void CalcBoundsInfo(List<Vector3> listVertsPos, VertexHelper toFill,TextGenerati
207241
pos = listVertsPos[i];
208242
if (pos.x < bounds.min.x)
209243
{
210-
// 换行重新添加包围框
211-
hrefInfo.Boxes.Add(new Rect(bounds.min, bounds.size));
244+
// 换行重新添加包围框
245+
hrefInfo.Boxes.Add(new Rect(bounds.min, bounds.size));
212246
bounds = new Bounds(pos, Vector3.zero);
213247
}
214248
else
@@ -259,8 +293,10 @@ private string GetOutputText(string inputText)
259293
if (string.IsNullOrEmpty(inputText))
260294
return "";
261295

296+
//回收各种对象
262297
ReleaseSpriteTageInfo();
263-
_textBuilder.Remove(0, _textBuilder.Length);
298+
ReleaseHrefInfos();
299+
_textBuilder.Remove(0, _textBuilder.Length);
264300
int textIndex = 0;
265301

266302
foreach (Match match in _inputTagRegex.Matches(inputText))
@@ -276,16 +312,15 @@ private string GetOutputText(string inputText)
276312
_textBuilder.Append("<color=blue>");
277313
int startIndex = _textBuilder.Length * 4;
278314
_textBuilder.Append("[" + match.Groups[2].Value + "]");
279-
int endIndex = _textBuilder.Length * 4 - 2;
315+
int endIndex = _textBuilder.Length * 4 - 1;
280316
_textBuilder.Append("</color>");
281317

282-
var hrefInfo = new HrefInfo
283-
{
284-
Id = Mathf.Abs(tempId),
285-
StartIndex = startIndex, // 超链接里的文本起始顶点索引
286-
EndIndex = endIndex,
287-
Name = match.Groups[2].Value
288-
};
318+
319+
var hrefInfo = Pool<HrefInfo>.Get();
320+
hrefInfo.Id = Mathf.Abs(tempId);
321+
hrefInfo.StartIndex = startIndex;// 超链接里的文本起始顶点索引
322+
hrefInfo.EndIndex = endIndex;
323+
hrefInfo.Name = match.Groups[2].Value;
289324
_listHrefInfos.Add(hrefInfo);
290325

291326
}
@@ -322,7 +357,7 @@ private string GetOutputText(string inputText)
322357
#endregion
323358

324359
#region 超链接信息类
325-
private class HrefInfo
360+
public class HrefInfo
326361
{
327362
public int Id;
328363

@@ -358,6 +393,7 @@ public void OnPointerClick(PointerEventData eventData)
358393
}
359394
#endregion
360395

396+
//回收SpriteTagInfo
361397
private void ReleaseSpriteTageInfo()
362398
{
363399
//记录之前的信息
@@ -368,13 +404,49 @@ private void ReleaseSpriteTageInfo()
368404
}
369405
_spriteInfo.Clear();
370406
}
407+
//回收超链接的信息
408+
private void ReleaseHrefInfos()
409+
{
410+
for (int i = 0; i < _listHrefInfos.Count; i++)
411+
{
412+
Pool<HrefInfo>.Release(_listHrefInfos[i]);
413+
}
414+
_listHrefInfos.Clear();
415+
}
371416

372417
#if UNITY_EDITOR
418+
Vector3[] _textWolrdVertexs = new Vector3[4];
373419
private void OnDrawGizmos()
374420
{
375-
Gizmos.color = Color.yellow;
376-
377-
for (int i = 0; i < _spriteInfo.Count; i++)
421+
//text
422+
Gizmos.color = Color.white;
423+
rectTransform.GetWorldCorners(_textWolrdVertexs);
424+
Gizmos.DrawLine(_textWolrdVertexs[0], _textWolrdVertexs[1]);
425+
Gizmos.DrawLine(_textWolrdVertexs[1], _textWolrdVertexs[2]);
426+
Gizmos.DrawLine(_textWolrdVertexs[2], _textWolrdVertexs[3]);
427+
Gizmos.DrawLine(_textWolrdVertexs[3], _textWolrdVertexs[0]);
428+
429+
//href
430+
Gizmos.color = Color.green;
431+
for (int i = 0; i < _listHrefInfos.Count; i++)
432+
{
433+
for (int j = 0; j < _listHrefInfos[i].Boxes.Count; j++)
434+
{
435+
Rect rect = _listHrefInfos[i].Boxes[j];
436+
Vector3 point00 = Utility.TransformPoint2World(transform,rect.position);
437+
Vector3 point01 = Utility.TransformPoint2World(transform, new Vector3(rect.x+rect.width,rect.y));
438+
Vector3 point02 = Utility.TransformPoint2World(transform, new Vector3(rect.x+rect.width,rect.y+rect.height));
439+
Vector3 point03 = Utility.TransformPoint2World(transform, new Vector3(rect.x,rect.y+rect.height));
440+
Gizmos.DrawLine(point00, point01);
441+
Gizmos.DrawLine(point01, point02);
442+
Gizmos.DrawLine(point02, point03);
443+
Gizmos.DrawLine(point03, point00);
444+
}
445+
}
446+
447+
//sprite
448+
Gizmos.color = Color.yellow;
449+
for (int i = 0; i < _spriteInfo.Count; i++)
378450
{
379451
Gizmos.DrawLine(_spriteInfo[i].Pos[0], _spriteInfo[i].Pos[1]);
380452
Gizmos.DrawLine(_spriteInfo[i].Pos[1], _spriteInfo[i].Pos[2]);

obj/Debug/Assembly-CSharp.pdb

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)