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