{"id":648,"date":"2013-06-26T13:24:23","date_gmt":"2013-06-26T05:24:23","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=648"},"modified":"2013-08-17T23:17:01","modified_gmt":"2013-08-17T15:17:01","slug":"%e5%85%a9%e7%a8%ae%e6%96%bc-unity3d-c-%e4%b8%ad%e9%80%b2%e8%a1%8c-eventhandler-%e7%9a%84%e7%b7%a8%e7%a2%bc%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/648\/","title":{"rendered":"\u5169\u7a2e\u65bc Unity3d C# \u4e2d\u9032\u884c EventHandler \u7684\u7de8\u78bc\u65b9\u5f0f."},"content":{"rendered":"<p>example : \u7a0b\u5f0f\u5efa\u69cb<\/p>\n<ul>\n<li>MyEventPackage \u70ba Event \u5c01\u5305.<\/li>\n<li>MyObject \u6bcf 3 \u500b\u5faa\u74b0\u9032\u884c\u4e00\u6b21 Event Dispatch.<\/li>\n<li>MyListener \u65bc\u63a5\u6536\u5230\u4e8b\u4ef6\u5f8c\u9032\u884c Debug.Log \u5beb\u5165.<\/li>\n<\/ul>\n<p>For Dispatch Side :<\/p>\n<p>[csharp]public class MyEventPackage<\/p>\n<p>{<br \/>\n public string MyData{ get; private set; }<br \/>\n public MyEventPackage( string _data ) : this()<br \/>\n {<br \/>\n this.MyData = _data;<br \/>\n }<br \/>\n public MyEventPackage()<br \/>\n {<br \/>\n this.MyData = null;<br \/>\n }<br \/>\n }<\/p>\n<p>public class MyObject : MonoBehaviour<br \/>\n {<br \/>\n public event EventHandler MyEvent;<br \/>\n private int cnt;<br \/>\n void Start()<br \/>\n {<br \/>\n cnt=0;<br \/>\n }<br \/>\n void Update()<br \/>\n {<br \/>\n if( cnt&gt;=3 )<br \/>\n {<br \/>\n cnt=0;<br \/>\n if( MyEvent != null )<br \/>\n {<br \/>\n \/\/ when someone listening, kick their ass to move.<br \/>\n MyEvent( this, _EventSocket );<br \/>\n }<br \/>\n }<br \/>\n else<br \/>\n {<br \/>\n cnt++;<br \/>\n }<br \/>\n }<br \/>\n }[\/csharp]<\/p>\n<p>For Listener Side :<\/p>\n<p>\u7b2c\u4e00\u7a2e<\/p>\n<p>[csharp]public class MyListener : MonoBehaviour<\/p>\n<p>{<br \/>\n \/\/ the object to listening, setup in Editor<br \/>\n public MyObject MyLittleObject;<\/p>\n<p>public void Start()<br \/>\n {<br \/>\n MyLittleObject.MyEvent += (sender, e) {<br \/>\n \/\/ when every time MyLittleObject dispatch event<br \/>\n \/\/ run following coding&#8230;<br \/>\n MyEventPackage _tmp = (MyEventPackage) e;<br \/>\n Debug.Log(&quot;Event received : &quot;+ _tmp.MyData.ToString() );<br \/>\n }<br \/>\n }<br \/>\n }[\/csharp]<\/p>\n<p>\u9019\u7a2e\u7684\u597d\u8655\u662f\u7c21\u77ed\u6e05\u695a, \u7f3a\u9ede\u662f\u4e0d\u80fd\u522a\u9664 Listener. (\u6709\u65b9\u6cd5\u55ce)<\/pre>\n<p>\u7b2c\u4e8c\u7a2e<\/p>\n<p>[csharp]public class MyListener : MonoBehaviour<br \/>\n {<br \/>\n \/\/ the object to listening, setup in Editor<br \/>\n public MyObject MyLittleObject;<\/p>\n<p>public void StartListening()<br \/>\n { \/\/ start listening to object<br \/>\n MyLittleObject.MyEvent += HandlerMyEvent;<br \/>\n }<br \/>\n public void StopListening()<br \/>\n { \/\/ stop listening.<br \/>\n MyLittleObject.MyEvent -= HandlerMyEvent;<br \/>\n }<br \/>\n private void HandlerMyEvent(object sender, EventArgs e)<br \/>\n {<br \/>\n \/\/ when every time MyLittleObject dispatch event<br \/>\n \/\/ run following coding&#8230;<br \/>\n MyEventPackage _tmp = (MyEventPackage) e;<br \/>\n Debug.Log(&quot;Event received : &quot;+ _tmp.MyData.ToString() );<br \/>\n }<br \/>\n }[\/csharp]<\/p>\n<p>\u9019\u7a2e\u7684\u597d\u8655\u662f\u751a\u9ebc\u529f\u80fd\u4e5f\u6a19\u793a\u6e05\u695a, \u4e5f\u53ef\u4ee5\u9032\u884c Listener \u522a\u9664,<br \/>\n\u7f3a\u9ede\u5c31\u662f\u592a\u7e41\u8986\u4e86, trace code \u6709\u9ede\u9ebb\u7169.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>example : \u7a0b\u5f0f\u5efa\u69cb MyEventPackage \u70ba Event \u5c01\u5305. MyObject &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[16,21,43],"class_list":["post-648","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c-2","tag-event","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/648","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=648"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/648\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}