{"id":13786269,"url":"https://github.com/zsaladin/Asyncoroutine","last_synced_at":"2025-05-11T22:30:50.324Z","repository":{"id":47190913,"uuid":"103415353","full_name":"zsaladin/Asyncoroutine","owner":"zsaladin","description":"Asyncoroutine is a unity asset that allows you to use Coroutine and async/await together.","archived":false,"fork":false,"pushed_at":"2021-09-09T04:43:49.000Z","size":99,"stargazers_count":134,"open_issues_count":2,"forks_count":19,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-11-17T22:36:13.633Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zsaladin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-13T15:12:46.000Z","updated_at":"2024-09-02T09:34:08.000Z","dependencies_parsed_at":"2022-08-15T12:40:09.679Z","dependency_job_id":null,"html_url":"https://github.com/zsaladin/Asyncoroutine","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zsaladin%2FAsyncoroutine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zsaladin%2FAsyncoroutine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zsaladin%2FAsyncoroutine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zsaladin%2FAsyncoroutine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zsaladin","download_url":"https://codeload.github.com/zsaladin/Asyncoroutine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253645101,"owners_count":21941311,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T19:01:12.746Z","updated_at":"2025-05-11T22:30:49.929Z","avatar_url":"https://github.com/zsaladin.png","language":"C#","funding_links":[],"categories":["Async / Await"],"sub_categories":[],"readme":"# Asyncoroutine\n\nAsyncoroutine is a unity asset that allows you to use Coroutine and async/await together.\nYou might face the situation that Coroutine and async/await get together. In this situation 'Asyncoroutine' is useful.\nYou don't need to switch your code style from Coroutine to async/await or from async/await to Coroutine by using 'Asyncoroutine'.\nSee below.\n\n## How to use\n### Coroutine in async/await\nYou can 'await' Coroutine in async/await.\n```C#\nusing Asyncoroutine;\n\nasync void Awake()\n{\n    await new WaitForSeconds(1f);\n    Debug.Log(\"WaitForSeconds\");\n\n    await Task.Delay(1000);\n    Debug.Log(\"Delay\");\n\n    WWW www = await new WWW(\"http://google.com\");\n    Debug.Log(www.text);\n\n    await new WaitForSecondsRealtime(1f);\n    Debug.Log(\"WaitForSecondsRealtime\");\n\n    await UnityCoroutine();\n    Debug.Log(\"UnityCoroutine\");\n}\n```\nAs you can see, you just write 'await' at the front of Coroutine or YieldInstrunction like WaitForSeconds.\nAll the things will happen by 'using Asyncoroutine'.\n\nAlso it makes 'Awake' and 'OnEnable' use Coroutine. Unlike 'Start' we could not use Coroutine in them but you can from now.\n(Actually there is an another alternative in above situation. See [link](https://github.com/zsaladin/AsCoroutine))\nMoreover you can use Coroutine on 'OnDisable' by using it.\n\n### WaitForMainThread\nSometimes you might want to call Coroutine from non main thread. It requires to switch to main thread.\nAsyncoroutine provides it.\n```C#\nusing Asyncoroutine;\n\nasync void Awake()\n{\n    // It's not guaranteed that it will be completed on main thread because of 'ConfigureAwait(false)'\n    await Task.Delay(1000).ConfigureAwait(false); \n    Debug.LogFormat(\"ThreadID : {0}\", Thread.CurrentThread.ManagedThreadId);\n\n    await new WaitForMainThread();\n    WWW www = await new WWW(\"https://api.ipify.org?format=json\");\n    Debug.Log(www.text);\n    Debug.LogFormat(\"ThreadID : {0}\", Thread.CurrentThread.ManagedThreadId);\n}\n```\n\n### Task in Coroutine\nIf you don't familiar with async/await then use original Coroutine style with async/await\n```C#\nusing Asyncoroutine;\n\nIEnumerator Start()\n{\n    yield return new WaitForSeconds(1f);\n    Debug.Log(\"WaitForSeconds\");\n\n    yield return Task.Delay(1000).AsCoroutine();\n    Debug.Log(\"AsCoroutine1 Delay\");\n\n    var taskYieldInstruction = Task.Run(() =\u003e LongTimeJob()).AsCoroutine();\n    yield return taskYieldInstruction;\n\n    Debug.LogFormat(\"AsCoroutine2 Task.Run Result : {0}\", taskYieldInstruction.Result);\n}\n```\nJust write 'AsCoroutine()' at the end of Task. It creates a proper YieldInstrunction which Coroutine can handle.\n\n#### Note : You must have Unity 2017 or above and be sure that Scripting Runtime Version is '.Net 4.6'.\n\n## Author\n- Kim Daehee, Software engineer in Korea.\n- zsaladinz@gmail.com\n\n## License\n- This asset is under MIT License.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzsaladin%2FAsyncoroutine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzsaladin%2FAsyncoroutine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzsaladin%2FAsyncoroutine/lists"}