@@ -21,7 +21,6 @@ public class GoWrapper : DisplayObject
2121 protected Canvas _canvas ;
2222 protected bool _cloneMaterial ;
2323 protected bool _shouldCloneMaterial ;
24- protected bool _supportMask ;
2524
2625 protected struct RendererInfo
2726 {
@@ -85,7 +84,6 @@ public void SetWrapTarget(GameObject target, bool cloneMaterial)
8584
8685 _canvas = null ;
8786 _wrapTarget = target ;
88- _supportMask = false ;
8987 _shouldCloneMaterial = false ;
9088 _renderers . Clear ( ) ;
9189
@@ -176,10 +174,6 @@ void CloneMaterials()
176174 if ( shouldSetRQ && mat . renderQueue != 3000 ) //Set the object rendering in Transparent Queue as UI objects
177175 mat . renderQueue = 3000 ;
178176
179- if ( mat . HasProperty ( ShaderConfig . ID_Stencil ) || mat . HasProperty ( ShaderConfig . ID_Stencil2 )
180- || mat . HasProperty ( ShaderConfig . ID_ClipBox ) )
181- _supportMask = true ;
182-
183177 //确保相同的材质不会复制两次
184178 Material newMat ;
185179 if ( ! _materialsBackup . TryGetValue ( mat , out newMat ) )
@@ -291,31 +285,48 @@ override public void Update(UpdateContext context)
291285 if ( _shouldCloneMaterial )
292286 CloneMaterials ( ) ;
293287
294- if ( _supportMask )
288+ ApplyClipping ( context ) ;
289+
290+ base . Update ( context ) ;
291+ }
292+
293+ private List < Material > helperMaterials = new List < Material > ( ) ;
294+ virtual protected void ApplyClipping ( UpdateContext context )
295+ {
296+ #if UNITY_2018_2_OR_NEWER
297+ int cnt = _renderers . Count ;
298+ for ( int i = 0 ; i < cnt ; i ++ )
295299 {
296- int cnt = _renderers . Count ;
297- for ( int i = 0 ; i < cnt ; i ++ )
300+ _renderers [ i ] . renderer . GetMaterials ( helperMaterials ) ;
301+
302+ int cnt2 = helperMaterials . Count ;
303+ for ( int j = 0 ; j < cnt2 ; j ++ )
298304 {
299- RendererInfo ri = _renderers [ i ] ;
300- Material [ ] mats = ri . materials ;
301- if ( mats != null )
302- {
303- int cnt2 = mats . Length ;
304- for ( int j = 0 ; j < cnt2 ; j ++ )
305- {
306- Material mat = mats [ j ] ;
307- if ( mat != null )
308- context . ApplyClippingProperties ( mat , false ) ;
309- }
305+ Material mat = helperMaterials [ j ] ;
306+ if ( mat != null )
307+ context . ApplyClippingProperties ( mat , false ) ;
308+ }
310309
311- if ( cnt2 > 0 && _cloneMaterial && ri . renderer != null
312- && ! Material . ReferenceEquals ( ri . renderer . sharedMaterial , mats [ 0 ] ) )
313- ri . renderer . sharedMaterials = mats ;
310+ helperMaterials . Clear ( ) ;
311+ }
312+ #else
313+ int cnt = _renderers . Count ;
314+ for ( int i = 0 ; i < cnt ; i ++ )
315+ {
316+ RendererInfo ri = _renderers [ i ] ;
317+ Material [ ] mats = ri . materials ;
318+ if ( mats != null )
319+ {
320+ int cnt2 = mats . Length ;
321+ for ( int j = 0 ; j < cnt2 ; j ++ )
322+ {
323+ Material mat = mats [ j ] ;
324+ if ( mat != null )
325+ context . ApplyClippingProperties ( mat , false ) ;
314326 }
315327 }
316328 }
317-
318- base . Update ( context ) ;
329+ #endif
319330 }
320331
321332 public override void Dispose ( )
0 commit comments