{"id":5313,"date":"2023-11-03T16:08:34","date_gmt":"2023-11-03T08:08:34","guid":{"rendered":"http:\/\/blog.coolcoding.cc\/?p=5313"},"modified":"2023-11-05T12:04:42","modified_gmt":"2023-11-05T04:04:42","slug":"unity-canvasmask%e5%b5%8c%e5%a5%97%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=5313","title":{"rendered":"Unity Canvas+Mask\u5d4c\u5957\u95ee\u9898"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>Canvas+Mask\u5d4c\u5957\n\u5982\u679c\u5b50\u7ea7Canvas\u4e0a\u9762\u4f7f\u7528\u4e86Override Sorting\uff0c\u4f1a\u5bfc\u81f4\u7236\u7ea7\u7684Mask\uff0c\u65e0\u6cd5\u5f71\u54cd\u5b50\u7ea7\n\n\u539f\u56e0\uff1a\nMask\u5728\u5b50\u7ea7Canvas\u4e0a\uff0c\u8fdb\u884c\u4e86\u6a21\u677f\u91cd\u7f6e\uff0c\u6240\u4ee5\u589e\u52a0\u4e00\u4e2aMask\u5b50\u7c7bMaskAAA\uff1a<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing UnityEngine;\nusing UnityEngine.EventSystems;\nusing UnityEngine.Rendering;\nusing UnityEngine.Serialization;\n\nnamespace UnityEngine.UI\n{\n    [AddComponentMenu(\"UI\/MaskAAA\", 13)]\n    [ExecuteAlways]\n    [RequireComponent(typeof(RectTransform))]\n    [DisallowMultipleComponent]\n    \/\/\/ &lt;summary>\n    \/\/\/ A component for masking children elements.\n    \/\/\/ &lt;\/summary>\n    \/\/\/ &lt;remarks>\n    \/\/\/ By using this element any children elements that have masking enabled will mask where a sibling Graphic would write 0 to the stencil buffer.\n    \/\/\/ &lt;\/remarks>\n    public class MaskAAA : UnityEngine.UI.Mask\n    {\n        public bool m_ShowMaskGraphic { get { return showMaskGraphic; } }\n        private Material m_MaskMaterial;\n        private Material m_UnmaskMaterial;\n\n        \/\/\/ Stencil calculation time!\n        public override Material GetModifiedMaterial(Material baseMaterial)\n        {\n            if (!MaskEnabled())\n                return baseMaterial;\n\n            var rootSortCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);\n            var stencilDepth = MaskUtilities.GetStencilDepth(transform, rootSortCanvas);\n            if (stencilDepth >= 8)\n            {\n                Debug.LogWarning(\"Attempting to use a stencil mask with depth > 8\", gameObject);\n                return baseMaterial;\n            }\n\n            int desiredStencilBit = 1 &lt;&lt; stencilDepth;\n\n            \/\/ if we are at the first level...\n            \/\/ we want to destroy what is there\n            if (desiredStencilBit == 1)\n            {\n                var maskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Replace, CompareFunction.Always, m_ShowMaskGraphic ? ColorWriteMask.All : 0);\n                StencilMaterial.Remove(m_MaskMaterial);\n                m_MaskMaterial = maskMaterial;\n\n                \/\/ var unmaskMaterial = StencilMaterial.Add(baseMaterial, 1, StencilOp.Zero, CompareFunction.Always, 0);\n                \/\/ StencilMaterial.Remove(m_UnmaskMaterial);\n                \/\/ m_UnmaskMaterial = unmaskMaterial;\n                \/\/ graphic.canvasRenderer.popMaterialCount = 1;\n                \/\/ graphic.canvasRenderer.SetPopMaterial(m_UnmaskMaterial, 0);\n\n                return m_MaskMaterial;\n            }\n\n            \/\/otherwise we need to be a bit smarter and set some read \/ write masks\n            var maskMaterial2 = StencilMaterial.Add(baseMaterial, desiredStencilBit | (desiredStencilBit - 1), StencilOp.Replace, CompareFunction.Equal, m_ShowMaskGraphic ? ColorWriteMask.All : 0, desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));\n            StencilMaterial.Remove(m_MaskMaterial);\n            m_MaskMaterial = maskMaterial2;\n\n            graphic.canvasRenderer.hasPopInstruction = true;\n            var unmaskMaterial2 = StencilMaterial.Add(baseMaterial, desiredStencilBit - 1, StencilOp.Replace, CompareFunction.Equal, 0, desiredStencilBit - 1, desiredStencilBit | (desiredStencilBit - 1));\n            StencilMaterial.Remove(m_UnmaskMaterial);\n            m_UnmaskMaterial = unmaskMaterial2;\n            graphic.canvasRenderer.popMaterialCount = 1;\n            graphic.canvasRenderer.SetPopMaterial(m_UnmaskMaterial, 0);\n\n            return m_MaskMaterial;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u589e\u52a0\u4e00\u4e2a\u65b0\u7684UI\u6750\u8d28<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"380\" src=\"http:\/\/blog.coolcoding.cc\/wp-content\/uploads\/2023\/11\/image.png\" alt=\"\" class=\"wp-image-5314\" srcset=\"https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2023\/11\/image.png 534w, https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2023\/11\/image-300x213.png 300w, https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2023\/11\/image-70x50.png 70w\" sizes=\"(max-width: 534px) 100vw, 534px\" \/><\/figure>\n\n\n\n<p>\u628aStencil Comparison\u8bbe\u7f6e\u4e3a6\uff0c\u653e\u5230\u5b50Canvas\u7684Image\u4e0a\uff0c\u5373\u53ef<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7136\u540e\u589e\u52a0\u4e00\u4e2a\u65b0\u7684UI\u6750\u8d28 \u628aStencil Comparison\u8bbe\u7f6e\u4e3a6\uff0c\u653e\u5230\u5b50Canvas\u7684Image\u4e0a\uff0c\u5373 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5313"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5313"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5313\/revisions"}],"predecessor-version":[{"id":5315,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5313\/revisions\/5315"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}