{"id":13662239,"url":"https://github.com/TorVestergaard/UnityAsyncRoutines","last_synced_at":"2025-04-25T07:31:01.418Z","repository":{"id":215783655,"uuid":"83085447","full_name":"TorVestergaard/UnityAsyncRoutines","owner":"TorVestergaard","description":"An extremely lightweight Unity library for creating and managing asynchronous coroutines for easy, straight-forward multi-threading and parallellism","archived":false,"fork":false,"pushed_at":"2017-03-21T16:37:30.000Z","size":115,"stargazers_count":50,"open_issues_count":0,"forks_count":10,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-10T17:46:48.414Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/TorVestergaard.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-02-24T21:47:14.000Z","updated_at":"2024-10-15T08:09:37.000Z","dependencies_parsed_at":"2024-01-07T21:45:02.249Z","dependency_job_id":"c6ff7cc0-5b2b-4e8e-a656-3c08db68917a","html_url":"https://github.com/TorVestergaard/UnityAsyncRoutines","commit_stats":null,"previous_names":["torvestergaard/unityasyncroutines"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TorVestergaard%2FUnityAsyncRoutines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TorVestergaard%2FUnityAsyncRoutines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TorVestergaard%2FUnityAsyncRoutines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TorVestergaard%2FUnityAsyncRoutines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TorVestergaard","download_url":"https://codeload.github.com/TorVestergaard/UnityAsyncRoutines/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250774645,"owners_count":21485183,"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-02T05:01:53.087Z","updated_at":"2025-04-25T07:31:01.411Z","avatar_url":"https://github.com/TorVestergaard.png","language":"C#","readme":"# UnityAsyncRoutines\nAn extremely lightweight Unity library for creating and managing asynchronous coroutines for easy, straight-forward multi-threading and parallellism.\n\n## Getting started\n\nTo get started, simply download AsyncRoutines.unitypackage from the root of the repository,\nor clone the entire repository project. \n\n## Using Unity Async Routines\n\nAsync routines are basically fancy coroutines, only with an expanded set of\npossible values to yield. Most fundamentally, yielding Async.ToAsync will\nswitch the method to run on a thread pool thread, and yield Async.ToGame will\nreturn your method to the game thread.\n\n![](image.png)\n\nThis is very useful for the cases where you need to execute heavy work, but\nyou also interact closely with Unity. This ease of switching between threads\nis of great assistance when creating asynchronous code.\n\nWhen you start an async routine using Async.Run, you will receive a handle.\nThis handle is a convenient way for you to talk about that instance of the\nroutine in particular. You can stop the routine with the handle, or you can\nyield it in a regular, vanilla Unity coroutine and wait for the async routine\nto finish.\n\n```csharp\n\tvar handle = Async.Run(CallIPAddressAsync());\n\tyield return handle;\n\thandle.stop();\n```\n\t\nAll of Unity's own coroutine yield instructions are supported so you can, should\nyou wish, pretend that async routines are simply Unity coroutines. If you don't\nintend to use any of the special async routine functonality, though, it is\nrecommended to simply use Unity's vanilla coroutines.\n\n```csharp\n\tyield return Async.ToAsync;  // switches to running in background threadpool\n\tThread.Sleep(40000); // simulating work in the background thread\n\tyield return Async.ToGame; // switch back to game thread, you can go back and forth\n```\n\t\nYou can create new async instructions easily by inheriting from AsyncInstruction.\nLook at the pre-existing instructions for a template on how to create an instruction.\n\nIt is possible to throttle the game-synced execution of async routines have available.\nThrottling is on by default, and set to a max of 25ms per frame.\n\nThat's pretty much it. Enjoy!\n\n## Examples\n\nThere are a couple of included examples in the Examples scene in this project.  Just run that scene and see the demo, then dig around in the project a bit and modify the code to understand how to use UnityAsyncRoutines.\n\n### Fading a game object over time\n\nThis is a simple example of doing work outside of the game loop using Async.  The 'over time' is implemented with a simple call to sleep - which of course you don't want to do in the main game thread.  But you need to be in the game thread to set the color of the gameObject.\n\nSnippet from Example : [FadeColor.cs](Unity%20Async%20Routines/Assets/Plugins/AsyncRoutines/Examples/FadeColor.cs)\n\n```csharp\n\tIEnumerator Fade()\n    {\n        yield return Async.ToGame;  // we need to interact with game objects, switch to game thread\n        statusText.text = \"Async fading started\";\n        rend.material.color = defaultColor;\n        yield return Async.ToAsync; // we are doing non-game stuff, switch out of main thread \n        for (float f = 1f; f \u003e= 0; f -= 0.05f)\n        {\n            yield return Async.ToGame; // need to do game stuff, switching to game thread\n            Color c = rend.material.color;\n            c.a = f;\n            rend.material.color = c;\n            yield return Async.ToAsync; // switch out of game thread\n            Thread.Sleep(100);\n        }\n        Thread.Sleep(2000);\n        yield return Async.ToGame; // switch to game thread to set the color back to default\n        rend.material.color = defaultColor;\n        yield return null; // stop\n        statusText.text = \"Async fading finished\";\n        Debug.Log(\"Async finished fading color\");\n    } \n```\n\n### Using blocking IO\n\nThis example uses the .Net WebClient - which blocks on the return of the url to show how Asych allows you to do blocking IO outside of the unity main thread, yet return with results and update game objects with them - easy as can be.\n\n[CallWebService.cs](Unity%20Async%20Routines/Assets/Plugins/AsyncRoutines/Examples/CallWebService.cs)\n\n```csharp\npublic class CallWebService : MonoBehaviour {\n\n\tpublic Text displayText;\n\n\tpublic void CallIPAddressServiceSync() {\n\t\tdisplayText.text = \"Fetching Ip address... brb.\";\n\t\t// just to be extra rude\n\t\tThread.Sleep(10000);\n\n\t\t// this blocks on the return of DownloadString\n\t\tdisplayText.text = new WebClient().DownloadString(\"http://api.ipify.org\");\n\t}\n\n\tpublic void CallIPAddressServiceAsync() {\n\t\tvar handle = Async.Run(CallIPAddressAsync());\n\t}\n\n\tIEnumerator CallIPAddressAsync() {\n\n\t\tstring ip = \"IP n/a\";\n\t\tdisplayText.text = \"Fetching Ip address... brb.\";\n\t\tyield return Async.ToAsync; // leave the unity thread and do background processing\n\t\t// just to be extra rude\n\t\tThread.Sleep(10000);\n\n\t\t// this blocks on the return of DownloadString\n\t\tip = new WebClient().DownloadString(\"http://api.ipify.org\");\n\n\t\tyield return Async.ToGame; // I've got the result, need to return to unity to set the display\n\t\tdisplayText.text = ip;\n\t}\n\n}\n```\n\n### Async Test \n\nThis is actually a great place to continue your study of Async in depth - just take a look at the test code to see examples of Paralell and some of the other more advanced uses of Async.\n\n[AsyncTests.cs](Unity%20Async%20Routines/Assets/Plugins/AsyncRoutines/Tests/AsyncTests.cs)","funding_links":[],"categories":["Open Source Repositories","C\\#","C#","Open Source Packages"],"sub_categories":["Theading","Utilities"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTorVestergaard%2FUnityAsyncRoutines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTorVestergaard%2FUnityAsyncRoutines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTorVestergaard%2FUnityAsyncRoutines/lists"}