{"id":5938,"date":"2025-01-02T13:49:24","date_gmt":"2025-01-02T05:49:24","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=5938"},"modified":"2025-01-02T13:50:01","modified_gmt":"2025-01-02T05:50:01","slug":"unity%e5%88%b6%e4%bd%9c%e6%96%87%e5%ad%97%e5%85%89%e6%99%95%e8%a1%ac%e5%ba%95%e6%95%88%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=5938","title":{"rendered":"Unity\u5236\u4f5c\u6587\u5b57\u5149\u6655\u886c\u5e95\u6548\u679c"},"content":{"rendered":"\n<p>\u6dfb\u52a0\u4e00\u4e2a\u6587\u5b57\u4fee\u6539\u5668\uff0c\u628auv\u548cposition\u90fd\u4fee\u6539\u6389<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine.UI;\r\nusing UnityEngine;\r\n\r\npublic class UIFontBackground : BaseMeshEffect\r\n{\r\n    private static readonly Vector2&#91;] mVertexUvs = new Vector2&#91;]\r\n    {\r\n        new Vector2(0, 0),\r\n        new Vector2(0, 1),\r\n        new Vector2(1, 1),\r\n        new Vector2(1, 0),\r\n    };\r\n\r\n    &#91;SerializeField]\r\n    private float mExpandVal = 16.0f;\r\n\r\n    public override void ModifyMesh(VertexHelper vh)\r\n    {\r\n        Vector3&#91;] mPosOffset = new Vector3&#91;]\r\n        {\r\n            new Vector3(-mExpandVal, mExpandVal,  0),\r\n            new Vector3(mExpandVal\/2, mExpandVal, 0),\r\n            new Vector3(mExpandVal\/2, -mExpandVal, 0),\r\n            new Vector3(-mExpandVal, -mExpandVal, 0),\r\n        };\r\n\r\n        var vsize = mVertexUvs.Length;\r\n        UIVertex vert = new UIVertex();\r\n        int size = vh.currentVertCount \/ vsize;\r\n        for (int loop = 0; loop &lt; size; loop++)\r\n        {\r\n            for(int vidx=0; vidx&lt; vsize; ++vidx)\r\n            {\r\n                int idx = loop*vsize+vidx;\r\n                vh.PopulateUIVertex(ref vert, idx);\r\n                vert.uv0 = mVertexUvs&#91;vidx];               \r\n                vert.position += mPosOffset&#91;vidx];\r\n                vh.SetUIVertex(vert, idx);\r\n            }\r\n        }\r\n    }    \r\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Shader\u91cc\u9762\u628a\u989c\u8272\u5168\u90e8\u4fee\u6539\u4e3a\u9ed1\u8272\uff0c\u5e76\u4e14\u5236\u4f5c\u4e00\u5f20\u72ec\u7acb\u7684\u56db\u5468\u865a\u5316\u7684\u7eb9\u7406\nhalf4 color = tex2D(_MainTex2, IN.texcoord);\r\nreturn half4(0,0,0, color.a);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"138\" height=\"134\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image-1.png\" alt=\"\" class=\"wp-image-5942\" srcset=\"https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image-1.png 138w, https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image-1-93x90.png 93w\" sizes=\"(max-width: 138px) 100vw, 138px\" \/><\/figure>\n\n\n\n<p>\u7136\u540e\u628a\u8fd9\u4e2a\u5e95\u548c\u6587\u5b57\u653e\u5728\u4e00\u8d77\uff0c\u6587\u5b57\u53e0\u5728\u4e0a\u9762\u5c31\u884c\u4e86<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"179\" src=\"http:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image.png\" alt=\"\" class=\"wp-image-5940\" srcset=\"https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image.png 763w, https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image-512x120.png 512w, https:\/\/blog.coolcoding.cn\/wp-content\/uploads\/2025\/01\/image-160x38.png 160w\" sizes=\"(max-width: 763px) 100vw, 763px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6dfb\u52a0\u4e00\u4e2a\u6587\u5b57\u4fee\u6539\u5668\uff0c\u628auv\u548cposition\u90fd\u4fee\u6539\u6389 \u7136\u540e\u628a\u8fd9\u4e2a\u5e95\u548c\u6587\u5b57\u653e\u5728\u4e00\u8d77\uff0c\u6587\u5b57\u53e0\u5728\u4e0a\u9762\u5c31\u884c\u4e86<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5938"}],"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=5938"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5938\/revisions"}],"predecessor-version":[{"id":5943,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5938\/revisions\/5943"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}