99/// </summary>
1010using System ;
1111using System . Collections . Generic ;
12+ #if UNITY_2021_2_OR_NEWER
13+ using System . Buffers ;
14+ #endif
15+ #if UNITY_2021_1_OR_NEWER
16+ using UnityEngine . Pool ;
17+ #endif
1218
1319namespace UnityEngine . UI . Extensions
1420{
@@ -36,6 +42,9 @@ public class Gradient2 : BaseMeshEffect
3642 [ SerializeField ]
3743 UnityEngine . Gradient _effectGradient = new UnityEngine . Gradient ( ) { colorKeys = new GradientColorKey [ ] { new GradientColorKey ( Color . black , 0 ) , new GradientColorKey ( Color . white , 1 ) } } ;
3844
45+ private GradientColorKey [ ] _colorKeys ;
46+ private GradientAlphaKey [ ] _alphaKeys ;
47+
3948 #region Properties
4049 public Blend BlendMode
4150 {
@@ -103,7 +112,11 @@ public override void ModifyMesh(VertexHelper helper)
103112 if ( ! IsActive ( ) || helper . currentVertCount == 0 )
104113 return ;
105114
115+ #if UNITY_2021_1_OR_NEWER
116+ List < UIVertex > _vertexList = ListPool < UIVertex > . Get ( ) ;
117+ #else
106118 List < UIVertex > _vertexList = new List < UIVertex > ( ) ;
119+ #endif
107120
108121 helper . GetUIVertexStream ( _vertexList ) ;
109122
@@ -218,7 +231,7 @@ public override void ModifyMesh(VertexHelper helper)
218231
219232 helper . AddVert ( centralVertex ) ;
220233
221- for ( int i = 1 ; i < steps ; i ++ ) helper . AddTriangle ( i - 1 , i , steps ) ;
234+ for ( int i = 1 ; i < steps ; i ++ ) helper . AddTriangle ( i , i - 1 , steps ) ;
222235 helper . AddTriangle ( 0 , steps - 1 , steps ) ;
223236 }
224237
@@ -238,6 +251,10 @@ public override void ModifyMesh(VertexHelper helper)
238251 }
239252 break ;
240253 }
254+
255+ #if UNITY_2021_1_OR_NEWER
256+ ListPool < UIVertex > . Release ( _vertexList ) ;
257+ #endif
241258 }
242259
243260 Rect GetBounds ( List < UIVertex > vertices )
@@ -264,18 +281,35 @@ Rect GetBounds(List<UIVertex> vertices)
264281
265282 void SplitTrianglesAtGradientStops ( List < UIVertex > _vertexList , Rect bounds , float zoomOffset , VertexHelper helper )
266283 {
267- List < float > stops = FindStops ( zoomOffset , bounds ) ;
284+ #if UNITY_2021_1_OR_NEWER
285+ List < float > stops = FindStops ( zoomOffset , bounds , ListPool < float > . Get ( ) ) ;
286+ #else
287+ List < float > stops = FindStops ( zoomOffset , bounds , new List < float > ( ) ) ;
288+ #endif
268289 if ( stops . Count > 0 )
269290 {
270291 helper . Clear ( ) ;
271292
272293 int nCount = _vertexList . Count ;
273294 for ( int i = 0 ; i < nCount ; i += 3 )
274295 {
275- float [ ] positions = GetPositions ( _vertexList , i ) ;
296+ #if UNITY_2021_2_OR_NEWER
297+ var positions = ArrayPool < float > . Shared . Rent ( 3 ) ;
298+ #else
299+ var positions = new float [ 3 ] ;
300+ #endif
301+
302+ GetPositions ( _vertexList , i , ref positions ) ;
303+
304+ #if UNITY_2021_1_OR_NEWER
305+ List < int > originIndices = ListPool < int > . Get ( ) ;
306+ List < UIVertex > starts = ListPool < UIVertex > . Get ( ) ;
307+ List < UIVertex > ends = ListPool < UIVertex > . Get ( ) ;
308+ #else
276309 List < int > originIndices = new List < int > ( 3 ) ;
277310 List < UIVertex > starts = new List < UIVertex > ( 3 ) ;
278311 List < UIVertex > ends = new List < UIVertex > ( 2 ) ;
312+ #endif
279313
280314 for ( int s = 0 ; s < stops . Count ; s ++ )
281315 {
@@ -403,13 +437,24 @@ void SplitTrianglesAtGradientStops(List<UIVertex> _vertexList, Rect bounds, floa
403437 int vertexCount = helper . currentVertCount ;
404438 helper . AddTriangle ( vertexCount - 3 , vertexCount - 2 , vertexCount - 1 ) ;
405439 }
440+
441+ #if UNITY_2021_2_OR_NEWER
442+ ArrayPool < float > . Shared . Return ( positions ) ;
443+ #endif
444+ #if UNITY_2021_1_OR_NEWER
445+ ListPool < int > . Release ( originIndices ) ;
446+ ListPool < UIVertex > . Release ( starts ) ;
447+ ListPool < UIVertex > . Release ( ends ) ;
448+ #endif
406449 }
407450 }
451+ #if UNITY_2021_1_OR_NEWER
452+ ListPool < float > . Release ( stops ) ;
453+ #endif
408454 }
409455
410- float [ ] GetPositions ( List < UIVertex > _vertexList , int index )
456+ void GetPositions ( List < UIVertex > _vertexList , int index , ref float [ ] positions )
411457 {
412- float [ ] positions = new float [ 3 ] ;
413458 if ( GradientType == Type . Horizontal )
414459 {
415460 positions [ 0 ] = _vertexList [ index ] . position . x ;
@@ -422,24 +467,27 @@ float[] GetPositions(List<UIVertex> _vertexList, int index)
422467 positions [ 1 ] = _vertexList [ index + 1 ] . position . y ;
423468 positions [ 2 ] = _vertexList [ index + 2 ] . position . y ;
424469 }
425- return positions ;
426470 }
427-
428- List < float > FindStops ( float zoomOffset , Rect bounds )
471+
472+ List < float > FindStops ( float zoomOffset , Rect bounds , List < float > stops )
429473 {
430- List < float > stops = new List < float > ( ) ;
431474 var offset = Offset * ( 1 - zoomOffset ) ;
432475 var startBoundary = zoomOffset - offset ;
433476 var endBoundary = ( 1 - zoomOffset ) - offset ;
434477
435- foreach ( var color in EffectGradient . colorKeys )
478+ if ( _colorKeys == null ) _colorKeys = EffectGradient . colorKeys ;
479+
480+ foreach ( var color in _colorKeys )
436481 {
437482 if ( color . time >= endBoundary )
438483 break ;
439484 if ( color . time > startBoundary )
440485 stops . Add ( ( color . time - startBoundary ) * Zoom ) ;
441486 }
442- foreach ( var alpha in EffectGradient . alphaKeys )
487+
488+ if ( _alphaKeys == null ) _alphaKeys = _effectGradient . alphaKeys ;
489+
490+ foreach ( var alpha in _alphaKeys )
443491 {
444492 if ( alpha . time >= endBoundary )
445493 break ;
@@ -455,7 +503,13 @@ List<float> FindStops(float zoomOffset, Rect bounds)
455503 size = bounds . height ;
456504 }
457505
458- stops . Sort ( ) ;
506+ stops . Sort ( ( x , y ) =>
507+ {
508+ if ( x > y ) return 1 ;
509+ if ( x == y ) return 0 ;
510+ return - 1 ;
511+ } ) ;
512+
459513 for ( int i = 0 ; i < stops . Count ; i ++ )
460514 {
461515 stops [ i ] = ( stops [ i ] * size ) + min ;
0 commit comments