{"id":5682,"date":"2024-05-31T16:58:34","date_gmt":"2024-05-31T08:58:34","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=5682"},"modified":"2024-05-31T16:58:50","modified_gmt":"2024-05-31T08:58:50","slug":"unity-shader-find-%e5%92%8c-ab","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=5682","title":{"rendered":"Unity Shader.Find \u548c AB"},"content":{"rendered":"\n<p>\u5c06Shader\u653e\u7f6e\u4e8eAB\u4e2d\uff0c\u7136\u540e\u5728\u8bbe\u5907\u4e0a\uff0c\u4f7f\u7528Shader.Find\u53bb\u627e\uff0c\u4f1a\u51fa\u73b0\u627e\u4e0d\u5230\u7684\u60c5\u51b5<\/p>\n\n\n\n<p>\u65b9\u6848\u4e00\uff1a<\/p>\n\n\n\n<p>\u5c06Shader\u6587\u4ef6\u653e\u7f6e\u4e8eResources\u76ee\u5f55\u4e2d<\/p>\n\n\n\n<p>\u65b9\u6848\u4e8c\uff1a<\/p>\n\n\n\n<p>\u5c06Shader\u6587\u4ef6\u653e\u7f6e\u5728Graphics Settings\u7684Always Include\u4e2d<\/p>\n\n\n\n<p>\u65b9\u6848\u4e09\uff1a<\/p>\n\n\n\n<p>\u4ee5\u4e0a2\u4e2a\u65b9\u6848\u90fd\u5bf9\u7edf\u4e00\u6253\u5305\u65b9\u6848\u4e0d\u53cb\u597d\uff0c\u6545\u53ef\u4ee5\u505a\u4e00\u4e2a\u8f6c\u6362<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static class ShaderHelper\r\n{\r\n    public delegate Shader FindShaderFunc(string shaderName);\r\n    private static FindShaderFunc mFindFunction = null;\r\n\r\n    public static void SetFindShaderFunc(FindShaderFunc f)\r\n    {\r\n        mFindFunction = f;\r\n    }\r\n\r\n    public static Shader Find(string shaderName)\r\n    {\r\n        if (mFindFunction != null)\r\n        {\r\n            var obj = mFindFunction(shaderName);\r\n            if (obj != null)\r\n            {\r\n                return obj;\r\n            }\r\n        }\r\n        return Shader.Find(shaderName);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ShaderLoader : MonoBehaviour\r\n{\r\n    public UnityEngine.Shader&#91;] mShader;\r\n\r\n    public void Start()\r\n    {      \r\n        ShaderHelper.SetFindShaderFunc( (shaderName) =>\r\n        {\r\n            if (mShader != null)\r\n            {\r\n                foreach(var shader in mShader)\r\n                {\r\n                    if (shader != null &amp;&amp; shader.name ==  shaderName)\r\n                    {\r\n                        return shader;\r\n                    }\r\n                }\r\n            }\r\n            return null;\r\n        });\r\n    }\r\n\r\n    void OnDestroy()\r\n    {\r\n        ShaderHelper.SetFindShaderFunc(null);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>1\u3001\u4ee3\u7801\u4e2d\u7684Shader.Find\u4fee\u6539\u4e3aShaderHelper \uff0c<strong>\u5305\u62ec\u7b2c3\u65b9\u63d2\u4ef6<\/strong><\/p>\n\n\n\n<p>2\u3001\u628aShaderLoader\u6253\u6210Prefab\uff0c\u5c06\u5e38\u7528\u7684Shader\u914d\u7f6e\u4e0a\u53bb\uff0c\u542f\u52a8\u65f6\u52a0\u8f7d\u5b83\u5373\u53ef<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06Shader\u653e\u7f6e\u4e8eAB\u4e2d\uff0c\u7136\u540e\u5728\u8bbe\u5907\u4e0a\uff0c\u4f7f\u7528Shader.Find\u53bb\u627e\uff0c\u4f1a\u51fa\u73b0\u627e\u4e0d\u5230\u7684\u60c5\u51b5 \u65b9\u6848\u4e00\uff1a \u5c06Sh [&hellip;]<\/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\/5682"}],"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=5682"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5682\/revisions"}],"predecessor-version":[{"id":5684,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/5682\/revisions\/5684"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}