Skip to content

Unity Asset that allows you to use Coroutine in a functional style.

License

Notifications You must be signed in to change notification settings

zsaladin/AsCoroutine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsCoroutine

AsCouroutine is a unity asset that allows you to use Coroutine in a functional style. You can handle coroutine as first-class citizen so that you can use method chain and combine coroutines.

How to use

You might face the case below. You would have to write an additional method because of using just one line of 'yield return' state related with some instructions like 'WaitForSeconds', 'WaitForEndOfFrame' and so on. Sometimes it is annoying and makes your code ugly.

private void OnEnable()
{
    StartCoroutine(UnityCoroutine());
}

private IEnumerator UnityCoroutine()
{
    yield return new WaitForSeconds(1f);
    Debug.Log("UnityCoroutine");
}

Now you are free from writing the additional method.

using AsCoroutine;

private void OnEnable()
{
    this.AsCoroutine()
        .YieldWaitForSeconds(1f).Action(() => Debug.Log("AsCoroutine"))
        .Start(this);
}

Also it makes 'Awake' and 'OnEnable' use Coroutine. Unlike 'Start' we could not use Coroutine in them but you can from now. (Actually there is an another alternative in above situation. See link)

Use method chain:

using AsCoroutine;

private void OnEnable()
{
    this.AsCoroutine()
        .YieldWaitForSeconds(1f).Action(() => Debug.Log("WaitForSeconds"))
        .YieldWaitForNextFrame().Action(() => Debug.Log("WaitForNextFrame"))
        .YieldWaitForEndOfFrame().Action(() => Debug.Log("WaitForEndOfFrame"))
        .YieldWaitForFixedUpdate().Action(() => Debug.Log("WaitForFixedUpdate"))
        .Start(this);
}

It provides a way to combine coroutines:

using AsCoroutine;

private void OnEnable()
{
    Cooperator co1 = this.AsCoroutine().YieldWaitForSeconds(1f).Action(() => Debug.Log("co1"));
    Cooperator co2 = this.AsCoroutine().YieldWaitForNextFrame().Action(() => Debug.Log("co2"));
    Cooperator co3 = this.AsCoroutine().YieldWaitForEndOfFrame().Action(() => Debug.Log("co3"));

    Cooperator newCo =
        Random.value < 0.5f ?
        co1.YieldCoroutine(co2) :
        co1.YieldCoroutine(co3);

    newCo.Start(this);
}

You can use 'WWW' with AsCoroutine:

using AsCoroutine;

private void OnEnable()
{
    this.AsCoroutine()
        .YieldWWW(new WWW("http://ip.jsontest.com/?mime=5")).Action(www =>
        {
            Debug.Log(www.text);
        }).Start(this);
}

Handle it like an original coroutine:

using AsCoroutine;

private Cooperator _cooperator;
private void OnEnable()
{
    _cooperator = this.AsCoroutine().Action(() => Debug.Log("Example4")).Repeat(() => true);
    StartCoroutine(_cooperator);
    // _cooperator.Start(this);
}

private void OnMouseDown()
{
    StopCoroutine(_cooperator);
    // _cooperator.Stop();
}

Author

License

  • This asset is under MIT License.

About

Unity Asset that allows you to use Coroutine in a functional style.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages