Giter Site home page Giter Site logo

awaitablecoroutines's Introduction

Awaitable Coroutines

  • Temporary solution to easily run Coroutines as async code.
  • Unity minimum version: 2019.3
  • Current version: 1.0.0
  • License: MIT

Summary

Currently, Unity does not support a proper way to run Coroutines using async/await code.

This will change in the next big update for the Unity .NET environment where asynchronous programing will be improved significantly.

Until there, this package provides a simple way to execute Coroutines asynchronous and improve your code legibility. Use it until Unity provides the official solution.

How To Use

Simply import the namespace ActionCode.AwaitableCoroutines and use the asynchronous AwaitableCoroutine.Run() function to run any Coroutine in your code.

using UnityEngine;
using System.Collections;
using ActionCode.AwaitableCoroutines;

public sealed class Test : MonoBehaviour
{
    public async void Start()
    {
        await AwaitableCoroutine.Run(CountUntil(5));
        print("Counting until five has finished!");

        await AwaitableCoroutine.Run(CountUntil(10));
        print("Counting until ten has finished!");
    }

    IEnumerator CountUntil(int seconds)
    {
        var waitOneSecond = new WaitForSeconds(1);
        for (int i = 1; i <= seconds; i++)
        {
            print("Counting: " + i);
            yield return waitOneSecond;
        }
    }
}

Note: your class does not need to be a MonoBehaviour.

How It Works

The static AwaitableCoroutine class uses a GameObject with the internal AwaitableCoroutineBehaviour script to wrap and execute asynchronous tasks.

This GameObject is lazy created, i.e. it'll be only created when used for the first time and it'll be sent to the special scene DontDestroyOnLoad. It's cached after that.

Awaitable Coroutine Behaviour in Inspector.

For safety, this GameObject cannot be edited using the Inspector. Do not delete this GameObject.

Installation

Using the Package Registry Server

Follow the instructions inside here and the package ActionCode-Awaitable Coroutines will be available for you to install using the Package Manager windows.

Using the Git URL

You will need a Git client installed on your computer with the Path variable already set.

  • Use the Package Manager "Add package from git URL..." feature and paste this URL: https://github.com/HyagoOliveira/AwaitableCoroutines.git

  • You can also manually modify you Packages/manifest.json file and add this line inside dependencies attribute:

"com.actioncode.awaitable-coroutines":"https://github.com/HyagoOliveira/AwaitableCoroutines.git"

Hyago Oliveira

GitHub - BitBucket - LinkedIn - [email protected]

awaitablecoroutines's People

Contributors

hyagooliveira avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.