{"id":1241,"date":"2022-06-20T10:56:52","date_gmt":"2022-06-20T02:56:52","guid":{"rendered":"https:\/\/www.mapleriver.cn\/?p=1241"},"modified":"2022-06-25T16:05:12","modified_gmt":"2022-06-25T08:05:12","slug":"unity-delegete%e5%a7%94%e6%89%98%e5%af%bc%e8%87%b4%e7%9a%84gc","status":"publish","type":"post","link":"https:\/\/www.mapleriver.cn\/?p=1241","title":{"rendered":"Unity Delegete\u59d4\u6258\u5bfc\u81f4\u7684GC"},"content":{"rendered":"<p>&emsp;\u00a0&emsp;\u4e4b\u524d\u505a\u591a\u8bed\u8a00\u76f8\u5173\u9700\u6c42\u7684\u65f6\u5019\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u6bcf\u6b21\u5207\u6362\u8bed\u8a00\uff0c\u6240\u6709\u7684\u63a7\u4ef6\u90fd\u53ef\u4ee5\u76d1\u542c\u5230\u8bed\u8a00\u53d8\u5316\u4e8b\u4ef6\uff0c\u6211\u5728\u591a\u8bed\u8a00\u7ec4\u4ef6\u4e0a\u6dfb\u52a0\u4e86\u4e00\u7ec4\u6ce8\u518c\u548c\u79fb\u9664\u4e8b\u4ef6\u7684\u76d1\u542c\uff1a<\/p>\n<pre><code class=\"language-C#\">protected virtual void OnDestroy()\n{\n    if (listenerOpen)\n    {\n        LanguageManager.LocalizationChangeEvent -= UpdateContent;\n    }\n}\nprotected void OpenLanguageListener() {\n    if (!listenerOpen)\n    {\n        LanguageManager.LocalizationChangeEvent += UpdateContent;\n    }\n}<\/code><\/pre>\n<p>&emsp;\u00a0&emsp;\u4f46\u662f\u6700\u8fd1\u5728uwa\u6d4b\u8bd5\u7684\u65f6\u5019\u53d1\u73b0\u8fd9\u4e2aOnDestory\u51fd\u6570\u5c45\u7136\u4f1a\u5bfc\u81f4\u8d85\u8d85\u8d85\u9ad8\u7684GC\u3002<br \/>\n<a href=\"https:\/\/www.mapleriver.cn\/wp-content\/uploads\/2022\/06\/\u56de\u8c03GC.png\"><img decoding=\"async\" src=\"https:\/\/www.mapleriver.cn\/wp-content\/uploads\/2022\/06\/\u56de\u8c03GC.png\" alt=\"\" \/><\/a><br \/>\n&emsp;\u00a0&emsp;\u4e3b\u8981\u539f\u56e0\u8fd8\u662f\u5728\u5c06\u51fd\u6570\u6ce8\u518c\u5230\u56de\u8c03\u7684\u65f6\u5019\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2aMulticastDelegate\u7684\u5bf9\u8c61\uff0c\u800c\u8fd9\u4e2a\u5bf9\u8c61\u7684\u9891\u7e41\u521b\u5efa\u4e0e\u79fb\u9664\uff0c\u5c31\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684GC\u3002\u5c24\u5176\u662f\u50cf\u591a\u8bed\u8a00\u7ec4\u4ef6\u8fd9\u79cd\u6bcf\u4e2aText\u548c\u6bcf\u4e2aImage\u7ec4\u4ef6\u90fd\u4f1a\u9644\u5e26\u7684\u7ec4\u4ef6\u6765\u8bf4\uff0c\u5728\u5173\u95edUI\u754c\u9762\u7684\u65f6\u5019\uff0c\u4f1a\u6709\u5927\u91cf\u7684\u591a\u8bed\u8a00\u7ec4\u4ef6\u53d6\u6d88\u56de\u8c03\uff0c\u5c31\u4f1a\u5bfc\u81f4GC\u8fc7\u9ad8\u3002<br \/>\n<a href=\"https:\/\/www.mapleriver.cn\/wp-content\/uploads\/2022\/06\/\u8c03\u7528GC.png\"><img decoding=\"async\" src=\"https:\/\/www.mapleriver.cn\/wp-content\/uploads\/2022\/06\/\u8c03\u7528GC.png\" alt=\"\" \/><\/a><br \/>\n&emsp;\u00a0&emsp;\u8c03\u75283\u6b21\u5c31\u67093.7KB\u7684GC\u4e86~\uff0c\u6bcf\u6b21\u8c03\u7528\u90fd\u6709\u8d85\u8fc71KB\u7684GC\uff01<br \/>\n&emsp;\u00a0&emsp;\u89e3\u51b3\u65b9\u6848\uff1a\u5bf9\u4e8b\u4ef6\u56de\u8c03\u8fdb\u884c\u7f13\u5b58\uff0c\u901a\u8fc7\u81ea\u5df1\u624b\u52a8\u8c03\u7528\u6765\u907f\u514d\u9891\u7e41\u7684\u589e\u5220\uff1a<\/p>\n<pre><code class=\"language-C#\">\/\/----------------------before----------------------\n\/\/ Before: You have a reference to a delegate\nMyDelegate myDelegate;\n\u00a0\/\/ Before: Combine\/Remove delegates\nmyDelegate\u00a0+=\u00a0MyFunction;\nmyDelegate\u00a0-=\u00a0MyFunction;\n\/\/ Before: Call delegate\nmyDelegate();\n\/\/----------------------after----------------------\n\/\/ After: You have a list of delegate references\nList&lt;MyDelegate&gt;\u00a0myDelegateList\u00a0=\u00a0new\u00a0List&lt;MyDelegate&gt;();\n\u00a0\n\/\/ After: Add\/Remove cached delegate references from the list\nmyDelegateList.Add(cachedDelegateOfMyFunction);\nmyDelegateList.Remove(cachedDelegateOfMyFunction);\n\u00a0\n\/\/ After: Call all delegates in list\nfor\u00a0(int\u00a0i\u00a0=\u00a00;\u00a0i\u00a0&lt;\u00a0myDelegateList.Count;\u00a0++i)\n{\n\u00a0 myDelegateList[i]();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;\u00a0&emsp;\u4e4b\u524d\u505a\u591a\u8bed\u8a00\u76f8\u5173\u9700\u6c42\u7684\u65f6\u5019\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u6bcf\u6b21\u5207\u6362\u8bed\u8a00\uff0c\u6240\u6709\u7684\u63a7\u4ef6\u90fd\u53ef\u4ee5\u76d1\u542c\u5230\u8bed\u8a00\u53d8\u5316\u4e8b\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1241","post","type-post","status-publish","format-standard","hentry","category-unity-"],"_links":{"self":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/1241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1241"}],"version-history":[{"count":1,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/1241\/revisions"}],"predecessor-version":[{"id":1244,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/1241\/revisions\/1244"}],"wp:attachment":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}