{"id":2610,"date":"2023-05-10T19:32:33","date_gmt":"2023-05-10T11:32:33","guid":{"rendered":"https:\/\/www.clonefactor.com\/wordpress\/?p=2610"},"modified":"2023-05-10T19:32:34","modified_gmt":"2023-05-10T11:32:34","slug":"unity3d-event-dispatch-%e4%b8%8a%e5%85%b6%e4%bb%96-listener-%e7%9a%84-exception-%e8%99%95%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/c\/2610\/","title":{"rendered":"Unity3D : Event dispatch \u4e0a\u5176\u4ed6 listener \u7684 Exception \u8655\u7406"},"content":{"rendered":"\n<p>\u5728\u4e8b\u4ef6\u8abf\u5ea6\uff08Event Dispatch\uff09\u4e2d\uff0c\u7531\u65bc\u5176\u4ed6\u76e3\u807d\u5668\uff08listener\uff09\u5f15\u767c\u7570\u5e38\u7684\u60c5\u6cc1\u662f\u5f88\u5e38\u898b\u7684\u3002\u7576\u4e00\u500b\u4e8b\u4ef6\u89f8\u767c\u6642\uff0c\u5176\u4e2d\u4e00\u500b\u76e3\u807d\u5668\u62cb\u51fa\u7570\u5e38\u53ef\u80fd\u6703\u5c0e\u81f4\u6574\u500b\u4e8b\u4ef6\u93c8\u88ab\u4e2d\u65b7\uff0c\u5f71\u97ff\u5176\u4ed6\u76e3\u807d\u5668\u7684\u57f7\u884c\u3002\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u9069\u7576\u7684\u932f\u8aa4\u7ba1\u7406\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u78ba\u4fdd\u4e8b\u4ef6\u7684\u7a69\u5b9a\u8abf\u5ea6\u548c\u5176\u4ed6\u76e3\u807d\u5668\u7684\u6b63\u5e38\u57f7\u884c\u3002<\/p>\n<p>\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\u7ba1\u7406\u65b9\u6cd5\u662f\u4f7f\u7528try-catch\u584a\u4f86\u6355\u7372\u53ef\u80fd\u5f15\u767c\u7570\u5e38\u7684\u4ee3\u78bc\u6bb5\u3002\u5728\u4e8b\u4ef6\u8abf\u5ea6\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528MulticastDelegate.GetInvocationList()\u65b9\u6cd5\u7372\u53d6\u4e8b\u4ef6\u7684\u76e3\u807d\u5668\u5217\u8868\uff0c\u4e26\u5728\u5faa\u74b0\u4e2d\u9010\u500b\u8abf\u7528\u76e3\u807d\u5668\u3002\u901a\u904e\u5c07\u6bcf\u500b\u76e3\u807d\u5668\u7684\u8abf\u7528\u653e\u7f6e\u5728try-catch\u584a\u4e2d\uff0c\u5373\u4f7f\u67d0\u500b\u76e3\u807d\u5668\u5f15\u767c\u7570\u5e38\uff0c\u4e5f\u4e0d\u6703\u4e2d\u65b7\u6574\u500b\u4e8b\u4ef6\u7684\u8abf\u5ea6\u6d41\u7a0b\uff0c\u4fdd\u8b49\u5176\u4ed6\u76e3\u807d\u5668\u80fd\u5920\u7e7c\u7e8c\u57f7\u884c\u3002<\/p>\n<p>\u8b93\u6211\u5011\u770b\u4e0b\u9762\u7684\u793a\u4f8b\u4f86\u8aaa\u660e\u9019\u4e00\u9ede\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Tester\n{\n    public static event System.Action&lt;string&gt; EVENT_MyCallback;\n\n    public void Method01_TriggerEvent()\n    {\n        \/\/ Don't do this, the exception will cause the other event listener fail.\n        EVENT_MyCallback?.Invoke(\"Don't do this\");\n    }\n\n    public void Method02_TriggerEvent()\n    {\n        if (EVENT_MyCallback == null)\n            return;\n            \n        var invList = EVENT_SceneLoaded.GetInvocationList();\n        foreach(var dispatcher in invList)\n        {\n            try\n            {\n                dispatcher.DynamicInvoke(\"Better\");\n            }\n            catch(System.Exception ex)\n            {\n            }\n        }\n    }\n}<\/pre>\n<p>\u00a0<\/p>\n<p>Method01 \u662f\u5e38\u898b\u7684\u61f6\u4eba\u5beb\u6cd5, \u9700\u7136\u6b63\u5e38\u60c5\u6cc1\u662f\u6c92\u6709\u554f\u984c\u7684\u53ef\u662f\u7576 Event \u8d8a\u4f86\u8d8a\u591a listener \u76e3\u807d\u7684\u6642\u5019\u5c31\u958b\u59cb\u6709\u554f\u984c\u4e86.<\/p>\n<p>\u6709\u554f\u984c\u7684\u662f\u5730\u662f event dispatch \u7684\u9806\u5e8f\u662f\u4e0d\u88ab\u4fdd<\/p>\n<p>Method01_TriggerEvent\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u61f6\u60f0\u5beb\u6cd5\u3002\u5728\u6b63\u5e38\u60c5\u6cc1\u4e0b\u662f\u6c92\u6709\u554f\u984c\u7684\uff0c\u4f46\u7576\u4e8b\u4ef6\u7684\u76e3\u807d\u5668\u8d8a\u4f86\u8d8a\u591a\u6642\uff0c\u5c31\u6703\u51fa\u73fe\u554f\u984c\u3002\u554f\u984c\u51fa\u5728\u4e8b\u4ef6\u8abf\u5ea6\u7684\u9806\u5e8f\u662f\u4e0d\u88ab\u4fdd\u8b49\u7684\u3002<\/p>\n<p>\u8003\u616e\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public void OtherClass()\n{\n    public OtherClass()\n    {\n        Tester.EVENT_MyCallback += Receive01;\n        Tester.EVENT_MyCallback += Receive02;\n        Tester.EVENT_MyCallback += Receive03;\n    }\n    \n    private void Receive01(string msg)\t=&gt; Debug.Log(msg);\n    private void Receive02(string msg)\t=&gt; throw System.Exception();\n    private void Receive03(string msg)\t=&gt; Debug.Log(\"M03\"+ msg);\n}<\/pre>\n<p>\u00a0<\/p>\n<p>\u7576\u57f7\u884cEVENT_MyCallback\u6642\uff0c\u5982\u679c\u4f7f\u7528\u7684\u662fMethod01_TriggerEvent\uff0c\u7531\u65bcReceive02\u62cb\u51fa\u7570\u5e38\uff0c\u5176\u4ed6\u7684Receive01\u548cReceive03\u53ef\u80fd\u88ab\u5ffd\u7565\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528Method02_TriggerEvent\uff0c\u7531\u65bc\u7570\u5e38\u88abtry-catch\u6355\u7372\uff0c\u4e8b\u4ef6\u8abf\u5ea6\u7684foreach\u4ecd\u7136\u6703\u7e7c\u7e8c\u57f7\u884c\uff0c\u78ba\u4fdd\u5176\u4ed6\u7684Receive01\u548cReceive03\u80fd\u5920\u6b63\u78ba\u63a5\u6536\u8abf\u5ea6\u3002<\/p>\n<p>\u901a\u904e\u9069\u7576\u7684\u932f\u8aa4\u7ba1\u7406\uff0c\u6211\u5011\u80fd\u5920\u66f4\u597d\u5730\u8655\u7406\u7570\u5e38\u60c5\u6cc1\uff0c\u907f\u514d\u5176\u5c0d\u6574\u500b\u4e8b\u4ef6\u8abf\u5ea6\u7522\u751f\u8ca0\u9762\u5f71\u97ff\u3002\u9019\u7a2e\u505a\u6cd5\u6709\u52a9\u65bc\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u7a69\u5b9a\u6027\uff0c\u78ba\u4fdd\u4e8b\u4ef6\u7684\u9806\u5229\u57f7\u884c\u4ee5\u53ca\u5176\u4ed6\u76f8\u95dc\u64cd\u4f5c\u7684\u6b63\u5e38\u9032\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e8b\u4ef6\u8abf\u5ea6\uff08Event Dispatch\uff09\u4e2d\uff0c\u7531\u65bc\u5176\u4ed6\u76e3\u807d\u5668\uff08listener\uff09\u5f15\u767c\u7570\u5e38\u7684\u60c5\u6cc1\u662f\u5f88\u5e38 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,106],"tags":[],"class_list":["post-2610","post","type-post","status-publish","format-standard","hentry","category-c","category-devlog"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2610"}],"version-history":[{"count":4,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2610\/revisions"}],"predecessor-version":[{"id":2614,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2610\/revisions\/2614"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}