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

Commit e8f1cad

Browse files
committed
完善超链接
1 parent 3cb092d commit e8f1cad

20 files changed

+73
-91
lines changed
Binary file not shown.

Assets/TextInlineSprite/Examples/Prefab/ChatItem.prefab.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Binary file not shown.

Assets/TextInlineSprite/Examples/Prefab/SpriteGraphic.prefab.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Binary file not shown.

Assets/TextInlineSprite/Examples/Prefab/Text.prefab.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Binary file not shown.

Assets/TextInlineSprite/Examples/Scene/ChattingDemo.unity.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Binary file not shown.

Assets/TextInlineSprite/Examples/Scene/ChattingDemo_InlineSprite.unity.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.

Assets/TextInlineSprite/Examples/Scene/TextDemo.unity.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.
Binary file not shown.

Assets/TextInlineSprite/Examples/Scene/Text_InlineSprite.unity.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Assets/TextInlineSprite/Examples/Scripts/ClickTest.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
/// ========================================================
2-
/// file:ClickTest.cs
3-
/// brief:
4-
/// author: coding2233
5-
/// date:
6-
/// version:v1.0
7-
/// ========================================================
8-
9-
using System.Collections;
10-
using System.Collections.Generic;
11-
using UnityEngine;
12-
1+
/// ========================================================
2+
/// file:ClickTest.cs
3+
/// brief:
4+
/// author: coding2233
5+
/// date:
6+
/// version:v1.0
7+
/// ========================================================
8+
9+
using System.Collections;
10+
using System.Collections.Generic;
11+
using UnityEngine;
12+
1313
public class ClickTest : MonoBehaviour {
1414

1515
private InlineText _text;
@@ -34,4 +34,4 @@ private void OnHrefClick(string hrefName)
3434
Debug.Log("点击了 " + hrefName);
3535
// Application.OpenURL("www.baidu.com");
3636
}
37-
}
37+
}

Assets/TextInlineSprite/Scripts/InlineText.cs

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
using System.Text;
1515
using UnityEngine.EventSystems;
1616
using UnityEngine.Events;
17+
using System;
1718

18-
public class InlineText : Text,IPointerClickHandler
19+
public class InlineText : Text, IPointerClickHandler
1920
{
2021
// 用正则取 [图集ID#表情Tag] ID值==-1 ,表示为超链接
2122
private static readonly Regex _InputTagRegex = new Regex(@"\[(\-{0,1}\d{0,})#(.+?)\]", RegexOptions.Singleline);
@@ -95,8 +96,8 @@ protected override void OnPopulateMesh(VertexHelper toFill)
9596
//计算quad占位的信息
9697
CalcQuadInfo(verts);
9798
//计算包围盒
98-
CalcBoundsInfo(toFill);
99-
99+
CalcBoundsInfo(verts,toFill,settings);
100+
100101
if (roundingOffset != Vector2.zero)
101102
{
102103
for (int i = 0; i < vertCount; ++i)
@@ -121,12 +122,12 @@ protected override void OnPopulateMesh(VertexHelper toFill)
121122
toFill.AddUIVertexQuad(m_TempVerts);
122123
}
123124
}
124-
125+
125126
m_DisableFontTextureRebuiltCallback = false;
126-
127+
127128
}
128129

129-
130+
130131
#region 计算Quad占位信息
131132

132133
void CalcQuadInfo(IList<UIVertex> verts)
@@ -170,7 +171,7 @@ void UpdateDrawnSprite()
170171
}
171172
_listSpriteInfo.Add(item.Value);
172173
}
173-
174+
174175
foreach (var item in _DrawSpriteInfo)
175176
{
176177
_InlineManager.UpdateTextInfo(item.Key, this, item.Value);
@@ -180,33 +181,35 @@ void UpdateDrawnSprite()
180181
#endregion
181182

182183
#region 处理超链接的包围盒
183-
void CalcBoundsInfo(VertexHelper toFill)
184+
void CalcBoundsInfo(IList<UIVertex> verts, VertexHelper toFill,TextGenerationSettings settings)
184185
{
186+
#region 包围框
185187
// 处理超链接包围框
186188
UIVertex vert = new UIVertex();
187189
foreach (var hrefInfo in _ListHrefInfos)
188190
{
189191
hrefInfo.boxes.Clear();
190-
if (hrefInfo.startIndex >= toFill.currentVertCount)
192+
if (hrefInfo.startIndex >= verts.Count)
191193
{
192194
continue;
193195
}
194196

195197
// 将超链接里面的文本顶点索引坐标加入到包围框
196-
toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex);
198+
vert = verts[hrefInfo.startIndex];
197199
var pos = vert.position;
198200
var bounds = new Bounds(pos, Vector3.zero);
199201
for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++)
200202
{
201-
if (i >= toFill.currentVertCount)
203+
if (i >= verts.Count)
202204
{
203205
break;
204206
}
205207

206-
toFill.PopulateUIVertex(ref vert, i);
208+
vert = verts[i];
207209
pos = vert.position;
208-
if (pos.x < bounds.min.x) // 换行重新添加包围框
210+
if (pos.x < bounds.min.x)
209211
{
212+
// 换行重新添加包围框
210213
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
211214
bounds = new Bounds(pos, Vector3.zero);
212215
}
@@ -215,8 +218,40 @@ void CalcBoundsInfo(VertexHelper toFill)
215218
bounds.Encapsulate(pos); // 扩展包围框
216219
}
217220
}
221+
//添加包围盒
218222
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
219223
}
224+
#endregion
225+
226+
#region 添加下划线
227+
TextGenerator _UnderlineText = new TextGenerator();
228+
_UnderlineText.Populate("_", settings);
229+
IList<UIVertex> _TUT = _UnderlineText.verts;
230+
foreach (var item in _ListHrefInfos)
231+
{
232+
for (int i = 0; i < item.boxes.Count; i++)
233+
{
234+
//计算下划线的位置
235+
Vector3[] _ulPos = new Vector3[4];
236+
_ulPos[0] = item.boxes[i].position + new Vector2(0.0f, fontSize * 0.2f);
237+
_ulPos[1] = _ulPos[0]+new Vector3(item.boxes[i].width,0.0f);
238+
_ulPos[2] = item.boxes[i].position + new Vector2(item.boxes[i].width, 0.0f);
239+
_ulPos[3] =item.boxes[i].position;
240+
//绘制下划线
241+
for (int j = 0; j < 4; j++)
242+
{
243+
m_TempVerts[j] = _TUT[j];
244+
m_TempVerts[j].color = Color.blue;
245+
m_TempVerts[j].position = _ulPos[j];
246+
if (j == 3)
247+
toFill.AddUIVertexQuad(m_TempVerts);
248+
}
249+
250+
}
251+
}
252+
253+
#endregion
254+
220255
}
221256
#endregion
222257

@@ -226,28 +261,31 @@ private string GetOutputText()
226261
_SpriteInfo = new Dictionary<int, SpriteTagInfo>();
227262
StringBuilder _textBuilder = new StringBuilder();
228263
int _textIndex = 0;
229-
264+
230265
foreach (Match match in _InputTagRegex.Matches(text))
231266
{
232267
int _tempID = 0;
233268
if (!string.IsNullOrEmpty(match.Groups[1].Value))
234269
_tempID = int.Parse(match.Groups[1].Value);
235-
string _tempTag= match.Groups[2].Value;
270+
string _tempTag = match.Groups[2].Value;
236271
//更新超链接
237272
if (_tempID == -1)
238273
{
239274
_textBuilder.Append(text.Substring(_textIndex, match.Index - _textIndex));
240275
_textBuilder.Append("<color=blue>");
276+
int _startIndex = _textBuilder.Length * 4;
277+
_textBuilder.Append("[" + match.Groups[2].Value + "]");
278+
int _endIndex = _textBuilder.Length * 4 - 2;
279+
_textBuilder.Append("</color>");
241280

242281
var hrefInfo = new HrefInfo
243282
{
244-
startIndex = _textBuilder.Length * 4, // 超链接里的文本起始顶点索引
245-
endIndex = (_textBuilder.Length + match.Groups[2].Length - 1) * 4 + 3,
246-
name = match.Groups[2].Value
283+
startIndex = _startIndex, // 超链接里的文本起始顶点索引
284+
endIndex = _endIndex,
285+
name = match.Groups[2].Value
247286
};
248287
_ListHrefInfos.Add(hrefInfo);
249288

250-
_textBuilder.Append("[" + match.Groups[2].Value + "]</color>");
251289
}
252290
//更新表情
253291
else
@@ -256,7 +294,7 @@ private string GetOutputText()
256294
|| !_InlineManager._IndexSpriteInfo[_tempID].ContainsKey(_tempTag))
257295
continue;
258296
SpriteInforGroup _tempGroup = _InlineManager._IndexSpriteInfo[_tempID][_tempTag];
259-
297+
260298
_textBuilder.Append(text.Substring(_textIndex, match.Index - _textIndex));
261299
int _tempIndex = _textBuilder.Length * 4;
262300
_textBuilder.Append(@"<quad size=" + _tempGroup.size + " width=" + _tempGroup.width + " />");
@@ -316,12 +354,12 @@ public void OnPointerClick(PointerEventData eventData)
316354
}
317355
}
318356
}
357+
358+
319359
#endregion
320360

321-
322361
}
323362

324-
325363
public class SpriteTagInfo
326364
{
327365
//图集ID
0 Bytes
Binary file not shown.
-3.82 KB
Binary file not shown.

0 commit comments

Comments
 (0)