@@ -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 ] ) ;
0 commit comments