{"id":1170,"date":"2017-06-08T13:32:15","date_gmt":"2017-06-08T05:32:15","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1170"},"modified":"2017-08-01T11:20:13","modified_gmt":"2017-08-01T03:20:13","slug":"%e5%88%b6%e4%bd%9c%e4%b8%80%e4%b8%aa%e5%8d%8f%e7%a8%8b%e5%85%ac%e5%85%b1%e7%b1%bbcoroutineutil","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1170","title":{"rendered":"\u5236\u4f5c\u4e00\u4e2a\u534f\u7a0b\u516c\u5171\u7c7bCoroutineUtil"},"content":{"rendered":"<p>\u5728\u7ee7\u627fMonoBehavior\u7684\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u534f\u7a0b\u6765\u5ef6\u8fdf\u6267\u884c\u67d0\u5757\u4ee3\u7801\uff0c\u6216\u8005\u505a\u4e2a\u8ba1\u65f6\u5668\u4e4b\u7c7b\u7684\u529f\u80fd<\/p>\n<p>\u90a3\u6211\u4eec\u80fd\u4e0d\u80fd\u628a\u8fd9\u5757\u4ee3\u7801\u516c\u5171\u8d77\u6765\u5462\uff1f<\/p>\n<p>\u4eca\u5929\u6211\u7ed9\u5206\u4eab\u4e2aCoroutineUtil\u7c7b\uff0c\u5728\u9879\u76ee\u521b\u5efa\u5373\u6302\u8f7d\uff0c\u4e14\u6c38\u4e0d\u9500\u6bc1<\/p>\n<p>\u4e0b\u9762\u4e3a\u4ee3\u7801\u5185\u5bb9\uff1a<\/p>\n<pre class=\"lang:c# decode:true \">\/*********************\r\n * \u59d3\u540d\uff1a\u738b\u51b2\r\n * \u529f\u80fd\uff1a\u5ef6\u8fdf\u6307\u5b9a\u65f6\u95f4\u540e\u8c03\u7528\u56de\u8c03\u65b9\u6cd5\uff0c\u53ef\u7528\u4e8e\u89e3\u51b3\u90e8\u5206\u65f6\u5e8f\u95ee\u9898\u3002\r\n * \u65e5\u671f\uff1a2017\/5\/27\r\n**********************\/\r\n\r\nusing UnityEngine;\r\nusing System.Collections;\r\nusing System;\r\n\r\npublic class CoroutineUtil : MonoBehaviour {\r\n\r\n    private static CoroutineUtil instance;\r\n    public static CoroutineUtil Instance {get{return instance;}}\r\n\r\n    void Awake()\r\n    {\r\n        instance = this;\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7b49\u5f85\u6307\u5b9a\u65f6\u95f4\u540e\uff0c\u6267\u884c\u56de\u8c03\u65b9\u6cd5\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"time\"&gt;\u7b49\u5f85\u65f6\u95f4 \u79d2\uff0c0\u4e3a1\u5e27&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"ignoreTimeScale\"&gt;\u662f\u5426\u5ffd\u7565TimeScale\u5f71\u54cd&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"callBack\"&gt;\u56de\u8c03\u65b9\u6cd5&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"objects\"&gt;\u56de\u8c03\u53c2\u6570&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n\tpublic Coroutine WaitTime(float time = 0, bool ignoreTimeScale = false, Action&lt;object[]&gt; callBack = null, params object[] objects)\r\n    {\r\n        return StartCoroutine(ICoroutine(time, ignoreTimeScale, callBack, objects));\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u5012\u8ba1\u65f6\u6216\u6b63\u8ba1\u65f6 \u6bcf\u9694\u6307\u5b9a\u65f6\u95f4\u56de\u8c03\u4e00\u6b21\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"startTime\"&gt;\u5f00\u59cb\u65f6\u95f4&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"endTime\"&gt;\u7ed3\u675f\u65f6\u95f4&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"perTime\"&gt;\u56de\u8c03\u95f4\u9694&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"ignoreTimeScale\"&gt;\u662f\u5426\u5ffd\u7565TimeScale\u5f71\u54cd&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"callBack\"&gt;\u56de\u8c03\u65b9\u6cd5&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    public Coroutine WaitPerSecond(float startTime = 10, float endTime = 0, float perTime = 1, bool ignoreTimeScale = false, Action&lt;float&gt; callBack = null)\r\n    {\r\n        return StartCoroutine(ICoroutine(startTime, endTime, perTime, ignoreTimeScale, callBack));\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u7b49\u5f85FixedUpdate\u65f6\u95f4\u540e\u6267\u884c\u56de\u8c03\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"time\"&gt;\u7b49\u5f85\u65f6\u95f4 \u79d2\uff0c0\u4e3a1\u5e27&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"callBack\"&gt;\u56de\u8c03\u65b9\u6cd5&lt;\/param&gt;\r\n    \/\/\/ &lt;param name=\"objects\"&gt;\u56de\u8c03\u53c2\u6570&lt;\/param&gt;\r\n    \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n    public Coroutine WaitFixedUpdate(float time = 0, Action&lt;object[]&gt; callBack = null, params object[] objects)\r\n    {\r\n        return StartCoroutine(ICoroutine(time, callBack, objects));\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u4e3b\u52a8\u505c\u6b62\u6307\u5b9a\u534f\u7a0b\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"coroutine\"&gt;&lt;\/param&gt;\r\n    public void Stop(Coroutine coroutine)\r\n    {\r\n        if (coroutine != null)\r\n        {\r\n            StopCoroutine(coroutine);\r\n        }\r\n    }\r\n\r\n    IEnumerator ICoroutine( float time = 0, bool ignoreTimeScale = false, Action&lt;object[]&gt; callBack = null, params object[] objects)\r\n    {\r\n        if (time == 0)\r\n        {\r\n            yield return null;\r\n        }\r\n        else if (time &gt; 0)\r\n        {\r\n            if (ignoreTimeScale)\r\n            {\r\n                float start = Time.realtimeSinceStartup;\r\n                while (Time.realtimeSinceStartup &lt; start + time)\r\n                {\r\n                    yield return null;\r\n                }\r\n            }\r\n            else\r\n                yield return new WaitForSeconds(time);\r\n        }\r\n\r\n        if (callBack != null)\r\n        {\r\n            callBack(objects);\r\n        }\r\n    }\r\n\r\n\r\n    IEnumerator ICoroutine(float beginTime = 10, float endTime = 0, float perTime = 1, bool ignoreTimeScale = false, Action&lt;float&gt; callBack = null)\r\n    {\r\n        if (beginTime &gt; endTime)\r\n        {\r\n            while (beginTime &gt; endTime)\r\n            {\r\n                if (ignoreTimeScale)\r\n                {\r\n                    float start = Time.realtimeSinceStartup;\r\n                    while (Time.realtimeSinceStartup &lt; start + perTime)\r\n                    {\r\n                        yield return null;\r\n                    }\r\n                }\r\n                else\r\n                    yield return new WaitForSeconds(perTime);\r\n                beginTime -= perTime;\r\n                if (callBack != null)\r\n                {\r\n                    callBack(beginTime);\r\n                }\r\n            }\r\n        }\r\n        else if (beginTime &lt; endTime)\r\n        {\r\n            while (beginTime &lt; endTime)\r\n            {\r\n                if (ignoreTimeScale)\r\n                {\r\n                    float start = Time.realtimeSinceStartup;\r\n                    while (Time.realtimeSinceStartup &lt; start + perTime)\r\n                    {\r\n                        yield return null;\r\n                    }\r\n                }\r\n                else\r\n                    yield return new WaitForSeconds(perTime);\r\n                beginTime += perTime;\r\n                if (callBack != null)\r\n                {\r\n                    callBack(beginTime);\r\n                }\r\n            }\r\n        }\r\n\r\n    }\r\n\r\n    IEnumerator ICoroutine(float waitTime, Action&lt;object[]&gt; callBack = null, params object[] objects)\r\n    {\r\n        if (waitTime == 0)\r\n        {\r\n            yield return new WaitForFixedUpdate();\r\n        }\r\n        else if(waitTime &gt; 0)\r\n        {\r\n            float time = 0;\r\n            while (time &lt; waitTime)\r\n            {\r\n                yield return new WaitForFixedUpdate();\r\n                time += Time.fixedDeltaTime;\r\n            }\r\n        }\r\n\r\n        if (callBack != null)\r\n        {\r\n            callBack(objects);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7ee7\u627fMonoBehavior\u7684\u811a\u672c\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u534f\u7a0b\u6765\u5ef6\u8fdf\u6267\u884c\u67d0\u5757\u4ee3\u7801\uff0c\u6216 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1170\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[261,262,260],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1170"}],"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=1170"}],"version-history":[{"count":2,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1170\/revisions"}],"predecessor-version":[{"id":1209,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1170\/revisions\/1209"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1170"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}