{"id":1812,"date":"2018-08-17T18:12:18","date_gmt":"2018-08-17T10:12:18","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1812"},"modified":"2020-09-17T09:23:01","modified_gmt":"2020-09-17T01:23:01","slug":"xlua%e8%b0%83%e7%94%a8unity%e5%8d%8f%e7%a8%8bcoroutine%e5%b7%a5%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1812","title":{"rendered":"xLua\u8c03\u7528Unity\u534f\u7a0bCoroutine\u5de5\u4f5c"},"content":{"rendered":"<p>xLua\u4e2d\u4e5f\u4f1a\u9891\u7e41\u7684\u4f7f\u7528Unity\u534f\u7a0b\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u6bd4\u5982WWW\u4e0b\u8f7d\u3001WaitForSeconds\u3001WaitForEndOfFrame\u3001WaitForSecondsRealtime\u7b49\u7b49<\/p>\n<p>\u672c\u6b21\u7684\u6559\u7a0b\u5c31\u662f\u6559\u4f1a\u5927\u5bb6\u5982\u4f55\u5728Lua\u8c03\u7528Unity\u534f\u7a0b\u5de5\u4f5c\uff0cxLua\u91cc\u6709coroutine\u7684\u4ecb\u7ecd\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u591a\u8bf4\u4e86\u3002<\/p>\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n<p>Coroutine_Runner.cs\uff1a<\/p>\n<pre class=\"lang:c# decode:true\">using UnityEngine;\nusing System.Collections;\nusing System;\n\npublic class Coroutine_Runner : MonoBehaviour\n{\n    public void YieldAndCallback(object to_yield, Action callback)\n    {\n        StartCoroutine(CoBody(to_yield, callback));\n    }\n\n    private IEnumerator CoBody(object to_yield, Action callback)\n    {\n        if (to_yield is IEnumerator)\n            yield return StartCoroutine((IEnumerator)to_yield);\n        else\n            yield return to_yield;\n        callback();\n    }\n}\n<\/pre>\n<p>Coroutine.lua<\/p>\n<pre class=\"lang:lua decode:true \">local util = require 'xlua.util'\n\nlocal gameobject = CS.UnityEngine.GameObject('Coroutine_Runner')\nCS.UnityEngine.Object.DontDestroyOnLoad(gameobject)\nlocal cs_coroutine_runner = gameobject:AddComponent(typeof(CS.Coroutine_Runner))\n\nlocal function async_yield_return(to_yield, cb)\n    cs_coroutine_runner:YieldAndCallback(to_yield, cb)\nend\n\nyield_return = util.async_to_sync(async_yield_return)<\/pre>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:lua decode:true\">function UIDelayController:OnCoroutineClick()\n    local co = coroutine.create(function()\n        print('coroutine start!')\n        local s = os.time()\n        yield_return(CS.UnityEngine.WaitForSeconds(3))\n        print('wait interval:', os.time() - s)\n\n        local www = CS.UnityEngine.WWW('http:\/\/www.u3d8.com')\n        yield_return(www)\n        if not www.error then\n            print(www.bytes)\n        else\n            print('error:', www.error)\n        end\n    end)\n\n    coroutine.resume(co)\nend<\/pre>\n<p>\u8f93\u51fa\uff1a<br \/>\n<img title=\"xLua\u8c03\u7528Unity\u534f\u7a0bCoroutine\u5de5\u4f5c - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"xLua\u8c03\u7528Unity\u534f\u7a0bCoroutine\u5de5\u4f5c - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" class=\"alignleft wp-image-1813 size-full\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/QQ\u622a\u56fe20181009182710.jpg\" alt=\"\" width=\"575\" height=\"96\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/QQ\u622a\u56fe20181009182710.jpg 575w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/QQ\u622a\u56fe20181009182710-300x50.jpg 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>xLua\u4e2d\u4e5f\u4f1a\u9891\u7e41\u7684\u4f7f\u7528Unity\u534f\u7a0b\u505a\u4e00\u4e9b\u4e8b\u60c5\uff0c\u6bd4\u5982WWW\u4e0b\u8f7d\u3001WaitFor &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1812\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1813,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[364],"tags":[369,370],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1812"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1812"}],"version-history":[{"count":3,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1812\/revisions"}],"predecessor-version":[{"id":2083,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1812\/revisions\/2083"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/media\/1813"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1812"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}