{"id":1215,"date":"2014-08-01T03:43:14","date_gmt":"2014-08-01T03:43:14","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1215"},"modified":"2014-08-01T03:52:06","modified_gmt":"2014-08-01T03:52:06","slug":"%e5%8e%9f%e4%be%86%e9%80%99%e6%89%8d%e6%98%af-ienumerable-%e7%9a%84%e5%ad%98%e5%9c%a8%e6%84%8f%e7%be%a9","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/1215\/","title":{"rendered":"\u539f\u4f86\u9019\u624d\u662f IEnumerable \u7684\u5b58\u5728\u610f\u7fa9."},"content":{"rendered":"<pre class=\"brush:csharp\">\/\/\/\/\/\/\/\/\r\nIEnumerable SpecialCollection()\r\n{\r\n\/\/for (int i = 1; ; ++i) yield return i;\r\nfor (int i = 2; ; i += 2) yield return i;\r\n}\r\n\/\/\/\/\/\/\/\/\r\nforeach (int i in SpecialCollection())\r\n{\r\nConsole.Write(i + \" \");\r\n}<\/pre>\n<p>\u5176\u5be6 IEnumerable \u662f\u4e00\u500b Interface .. \u4e3b\u8981\u662f\u5e0c\u671b\u505a\u4e00\u500b\u50cf List \u7684\u4e00\u7a2e\u5b58\u5728. SpecialCollection() \u662f\u4e00\u500b\u00a0Infinite loop, foreach \u9019\u500b List \u5247\u6703\u56de\u50b3\u4e00\u500b\u6c38\u9060\u4e0d\u6703\u5b8c\u7d50\u7684 loop. \u6240\u4ee5\u4f7f\u7528 Unity3d \u4e2d\u7684\u00a0Coroutine \u662f\u53ef\u4ee5\u4f7f\u7528\u00a0yield return \u53bb\u7b49\u5f85\u53e6\u4e00\u500b Coroutine \u7684\u57f7\u884c\u7dd2. \u800c\u518d\u9032\u884c\u4e0b\u4e00\u6b65\u7684\u5de5\u4f5c. \u5230\u4eca\u5929\u624d\u660e\u767d Coroutine \u7684\u6839\u672c\u7d50\u69cb.<\/p>\n<pre class=\"brush:csharp\">IEnumerator Func1()\r\n{\r\n    Debug.Log(\"Func1 startup.\");\r\n    for (int i=0; i&lt;10; i++)\r\n    {\r\n        yield return null;\r\n    }\r\n    Debug.Log(\"Func1 End.\");\r\n    yield break;\r\n}\r\n \r\nIEnumerator Func2()\r\n{\r\n    \/\/wait for the completion of Func1\r\n    Debug.Log(\"Func2 startup.\");\r\n    yield return StartCoroutine( Func1() );\r\n    Debug.Log(\"Func2 End.\");\r\n}\r\nStart()\r\n{\r\n    StartCoroutine(Func2());\r\n}\r\n\/*\r\nResult :\r\nFunc2 startup\r\nFunc1 startup\r\nFunc1 End\r\nFunc2 End\r\n*\/<\/pre>\n<p>\u9019\u88e1\u884d\u751f\u4e86\u53e6\u4e00\u7a2e\u5beb\u6cd5\u53eb\u4f5c Nested coroutine .<\/p>\n<pre class=\"brush:csharp\">IEnumerator Func2()\r\n{\r\n    \/\/wait for the completion of Func1\r\n    IEnumerator e = Func1();\r\n    while (e.MoveNext()) yield return e.Current;\r\n}<\/pre>\n<p>\u628a\u6574\u500b IEnumerator Func1 \u50cf list \u4e00\u6a23\u4ee3\u5165 variable e.<\/p>\n<p>\u4f7f\u7528 MoveNext() \u53bb\u57f7\u884c\u6bcf\u500b cycle,<\/p>\n<p>e.Current \u5247\u6703\u56de\u50b3\u00a0Coroutine \u88e1 yield return \u7684 result.<\/p>\n<p>\u4f7f\u5230 Func2 \u53ef\u4ee5\u4f7f\u7528 Func1 \u88e1\u7684 yield return value \u9032\u884c\u8655\u7406,<\/p>\n<p>\u6216\u8005\u5f9e Func2 \u64cd\u63a7\u57f7\u884c Func1 \u7684\u6642\u6a5f.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\/\/\/\/\/\/ IEnumerable SpecialCollection() { \/\/for ( &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,58,59,43],"class_list":["post-1215","post","type-post","status-publish","format-standard","hentry","category-unity3d","tag-c-2","tag-coroutine","tag-ienumerable","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1215","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=1215"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1215\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}