Giter Club home page Giter Club logo

iterator-tasks's Introduction

Iterator Tasks

Iterator Tasks is a iterator-based coroutine class library.

Asynchronous Operations on Unity

One of the purpose of Iterator Tasks is to simplify asynchronous operations on Unity 3D game engine. Unity is built on Mono, open source .NET development framework. It however uses older version which correspond approximately to .NET Framework 3.5 and C# 3.0. Thus, there is no Task class and async/await support.

Alternatively, Unity provides iterator-based coroutine framework for asynchronous operation. For example, following code awaits web access completoin without blocking a thread by using a yield return statement.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour
{
    public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
    IEnumerator Start()
    {
        WWW www = new WWW(url);
        yield return www;
        renderer.material.mainTexture = www.texture;
    }
}

However, there are not so many developers understanding usage of iterator block (yield return statement). Thus, it is slightly difficult to use iterator block for asynchronous operation, especially when a return value of the asynchronous operation is needed for subsequent tasks.

Because of this, Iterator Tasks wraps a iterator-based coroutine with a class which resembles Task class in .NET 4.

Example Usage

Here is an example of Task class in Iterator Tasks.

var task = new Task<double>(c => Coroutines.F1Async(x, c))
    .ContinueWith<string>(Coroutines.F2Async)
    .ContinueWith<int>(Coroutines.F3Async);

task.OnComplete(t => Console.WriteLine(t.Result));

Where F1Async, F2Async, F3Async are iterator-based coroutine as follows:

public static System.Collections.IEnumerator F1Async(double x, Action<double> completed)
{
  for (int i = 0; i < 5; i++) yield return null;
	var result = F1(x);
	completed(result);
}

public static System.Collections.IEnumerator F2Async(double x, Action<string> completed)
{
	for (int i = 0; i < 5; i++) yield return null;
	var result = F2(x);
	completed(result);
}

public static IEnumerator F3Async(string s, Action<int> completed)
{
	for (int i = 0; i < 5; i++) yield return null;
	var result = F3(s);
	completed(result);
}

License

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

iterator-tasks's People

Contributors

ppcuni avatar

Watchers

 avatar  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.