{"id":2602,"date":"2022-10-31T04:00:00","date_gmt":"2022-10-30T20:00:00","guid":{"rendered":"https:\/\/www.clonefactor.com\/wordpress\/?p=2602"},"modified":"2022-10-31T04:00:01","modified_gmt":"2022-10-30T20:00:01","slug":"%e5%98%97%e8%a9%a6%e4%ba%86%e8%a7%a3-unity-co-rotine-%e6%80%8e%e6%a8%a3%e6%8e%a7-ienumerator","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/c\/2602\/","title":{"rendered":"\u5617\u8a66\u4e86\u89e3 Unity Co-rotine \u600e\u6a23\u63a7  IEnumerator\u00a0"},"content":{"rendered":"\n<p>\u70ba\u4e86\u7406\u89e3 Coroutine \u600e\u6a23\u8655\u7406\u8cc7\u6599, \u8a8d\u771f\u7684\u7814\u7a76\u4e00\u4e0b IEnumerator\u00a0<br \/>\u5617\u8a66\u5728\u4e0d\u4f7f\u7528 Unity StartCoroutine() \u7684\u60c5\u6cc1\u4e0b, \u662f\u600e\u6a23\u81ea\u5df1\u63a7\u5236\u57f7\u884c\u7684, \u6e2c\u8a66\u4e86\u4e00\u4e0b.<\/p>\n<p>\u5c31\u53ea\u662f Unity \u81ea\u5df1\u5b9a\u7684 YieldInstruction \u6709\u9ede\u9ebb\u7169, \u8981\u81ea\u5df1\u60f3\u8fa6\u6cd5\u8655\u7406\u751a\u9ebc\u6642\u5019\u7d50\u675f..<br \/>reflection \u53ef\u80fd\u662f\u4e00\u689d\u51fa\u8def?\u00a0<\/p>\n<p>\u00a0<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System.Reflection;\nusing System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class TestCoroutine : MonoBehaviour\n{\n    private IEnumerator iter;\n    private void OnEnable()\n    {\n        iter = MyEnumerator();\n        Debug.ClearDeveloperConsole();\n    }\n\n    private IEnumerator MyEnumerator()\n    {\n        var a = 1;\n        var b = 2;\n        var c = -1;\n        Debug.Log($\"Step 1, A{a} + b{b} = C{c}\");\n        yield return \"First block\";\n\n\n        c = a + b;\n        Debug.Log($\"Step 2, A{a} + b{b} = C{c}\");\n        yield return 2;\n\n        c = 10000;\n        Debug.Log($\"Step 3, A{a} + b{b} = C{c}\");\n\n        yield return new WaitForSeconds(3f);\n        Debug.LogWarning(\"BOOM ! 3 second.\");\n    }\n\n    object lastObj = null;\n    public void Update()\n    {\n        bool skip = lastObj is YieldInstruction;\n\n        if (!(skip || Input.GetKeyUp(KeyCode.Space)))\n            return;\n\n        if (iter.MoveNext())\n        {\n            lastObj = iter.Current;\n            if (lastObj == null)\n            {\n\n            }\n            else if (lastObj is CustomYieldInstruction cy)\n            {\n                \/\/ cy.keepWaiting;\n                if (cy is WaitWhile) { }\n                else if (cy is WaitUntil) { }\n                else if (cy is WaitForSecondsRealtime) { }\n            }\n            else if (lastObj is YieldInstruction u3d)\n            {\n                \/\/ need to handle unity3d's WaitFor family.\n                Debug.Log($\"U3D waiting {u3d.GetType()}\");\n                if (u3d is WaitForSeconds wfs)\n                {\n                    Debug.Log(wfs);\n                }\n                else if (u3d is WaitForEndOfFrame) { }\n                else if (u3d is WaitForFixedUpdate) { }\n            }\n            else if (lastObj is string str)\n            {\n                Debug.Log($\"It's str = {str}\");\n            }\n            else if (lastObj is int i)\n            {\n                Debug.Log($\"It's int = {i}\");\n            }\n            else\n            {\n                Debug.Log($\"What is this ? {lastObj.GetType()}\");\n            }\n        }\n\n        lastObj = null;\n    }\n}\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u70ba\u4e86\u7406\u89e3 Coroutine \u600e\u6a23\u8655\u7406\u8cc7\u6599, \u8a8d\u771f\u7684\u7814\u7a76\u4e00\u4e0b IEnumerator\u00a0\u5617\u8a66\u5728\u4e0d\u4f7f\u7528  &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],"tags":[],"class_list":["post-2602","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2602","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=2602"}],"version-history":[{"count":2,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2602\/revisions"}],"predecessor-version":[{"id":2604,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2602\/revisions\/2604"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}