当前位置:网站首页>Ugui source code analysis - maskablegraphic

Ugui source code analysis - maskablegraphic

2022-07-24 02:48:00 Hello Bug.

One : Preface

seeing the name of a thing one thinks of its function ,MaskableGraphic Is an image that can be masked , Strictly speaking, it should be an image that can be masked and cropped , But it inherits from Graphic Classes and IClippable、IMaskable、IMaterialModifier Interface , stay Graphic It realizes the functions of clipping and masking
Image、RawImage、Text Inherit from MaskableGraphic class


Two : Source code analysis —— Class head


MaskableGraphic Inherited from Graphic class ,IClippable、IMaskable、IMaterialModifier Interface , Inherited from Graphic Class means that it is responsible for the display and update of images
Image、RawImage、Text Inherit from MaskableGraphic class


3、 ... and : The source code parsing —— Inherited from UIBehaviour Methods

——OnEnable

The first set m_ShouldRecalculateStencil( Whether the template material needs to be recalculated ) by true, Then call UpdateClipParent Update the corresponding RectMask2D object , And then call SetMaterialDirty Update material , If it is an image that can be masked, call MaskUtilities.NotifyStencilStateChanged Update material
UpdateClipParent The method is to realize RectMask2D, Other methods are to achieve Mask


——OnDisable

The first set m_ShouldRecalculateStencil( Whether the template material needs to be recalculated ) by true, Then call SetMaterialDirty Update material , And then call UpdateClipParent Update the corresponding RectMask2D object , Then remove the mask material and set it empty , If it is an image that can be masked, call MaskUtilities.NotifyStencilStateChanged Update material
UpdateClipParent The method is to realize RectMask2D, Other methods are to achieve Mask


Four : The source code parsing —— Inherited from IClippable Methods (RectMask2D Component related )

——RecalculateClipping

call UpdateClipParent Method , Update the corresponding RectMask2D object
stay MaskUtilities.Notify2DMaskStateChanged Call in method


——Cull

If validRect by false Or input clipRect And belong to Canvas The rectangular areas of do not coincide , call UpdateCull Method to add this object to the image reconstruction sequence
stay RectMask2D Of PerformClipping Call in method


——SetClipRect

according to validRect Parameters , by canvasRenderer Turn rectangle clipping on or off ( Pass in Shader)
stay RectMask2D Of PerformClipping Call in method


——SetClipSoftness

Set gradient  
stay RectMask2D Of PerformClipping Call in method


5、 ... and : The source code parsing —— Inherited from IMaskable Methods

——RecalculateMasking

Remove the mask material and leave it empty , Set up m_ShouldRecalculateStencil( Whether the template material needs to be recalculated ) by true, Last call SetMaterialDirty Update material
stay MaskUtilities.NotifyStencilStateChanged Call in method


6、 ... and : The source code parsing —— Inherited from IMaterialModifier Methods

——GetModifiedMaterial

Get material ( Subclasses will be called when updating materials Graphic Of materialForRendering Get the currently rendered material and assign it to canvasRenderer Rendering )

If you need to recalculate the template material, first find the root canvas of the object , Then calculate the distance between itself and the root canvas Mask The number of , If there is only one oneself Mask be m_StencilValue by 0,m_StencilValue To express with Mask Of desiredStencilBit Reference value for comparison , If m_StencilValue Greater than 0 And this Graphic Not a mask image , adopt StencilMaterial.Add Get a new material (StencilOp.Replace-2,CompareFunction.Equal-3) And return this material to

Graphic There are three kinds of materials in :defaultMaterial( Default material )、material( texture of material ) and materialForRendering( Render material ), Give priority to rendering materials , If there is no implementation IMaterialModifier The components of the interface are rendered with the same material , If no material is assigned , Then use the default material
defaultMaterial:defaultGraphicMaterial
material: Assigned material , Generally, this material is used
materialForRendering: If there is an implementation IMaterialModifier The components of the interface will modify the material to achieve the mask effect , If there is no implementation IMaterialModifier The components of the interface are rendered with the same material


7、 ... and : The source code parsing ——MaskableGraphic Built-in methods

——UpdateClipParent

to update m_ParentMask( In the parent object RectMask2D Components )
First, find the parent object RectMask2D Components (RectMask2D Components can be based on RectTransform Prune sub objects , The child object exceeds the parent RectTransform The part of the scope will be cut out )
If m_ParentMask Not empty and new RectMask2D Just like before m_ParentMask Different , Just take yourself from RectMask2D Of m_ClipTargets Remove from the crop target , And in RemoveClippable In the method , call clippable.SetClipRect(new Rect(), false), Turn off rectangle clipping
If the new RectMask2D Not empty and active , Just add yourself to RectMask2D Of m_ClipTargets Clipping target
Last , Put the new RectMask2D Assign a value to m_ParentMask


——UpdateCull

If canvasRenderer.cull When something changes , Send events m_OnCullStateChanged, And call the OnCullingChanged Method to add this object to the image reconstruction sequence

原网站

版权声明
本文为[Hello Bug.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230016078379.html