GithubHelp home page GithubHelp logo

coroutinehelper's Introduction

CoroutineHelper

CoroutineHelper is a unity3d coroutine tool collections.
协程助手是一个Unity3D的协程工具集。

Common:

Non-MonoBehaviour class start coroutine
你可以给非MonoBehaviour类开启协程

CoroutineHelper.StartCoroutine(...)

And packaged some common extensions
并且封装了一些常用扩展

CoroutineHelper.DelayInvoke(() => Debug.Log("foo!"), 2f);
CoroutineHelper.DelayNextFrameInvoke(() => Debug.Log("foo!"));

CoroutineGroup:

or like the MonoBehaviour class, you can ‘StopAllCoroutine’ through CoroutineGroup
或者像MonoBehaviour类一样,你可以用StopAllCoroutine来关闭当前Mono运行的所有协程通过协程组

void Test()
{
    var coroutineGroup = CoroutineHelper.Factory.CreateCoroutineGroup();
    coroutineGroup.StartCoroutine(Foo());
    coroutineGroup.StartCoroutine(Foo());
    coroutineGroup.StartCoroutine(Foo());
    coroutineGroup.StartCoroutine(Foo());

    coroutineGroup.StopAllCoroutines();
}

IEnumerator Foo() { yield break; }

CoroutinePool:

You can also create a coroutine pool to avoid the GC of StartCoroutine, but the coroutine pool is more troublesome to use.
你还可以创建协程池来避免StartCoroutine的GC问题,但是这个用起来会复杂一些。

var coroutinePool = CoroutineHelper.Factory.CreateCoroutinePool(10);
coroutinePool.TryStartCoroutineInPool(Foo());//If pool is full, execute it immediately with StartCoroutine.

if (coroutinePool.HasCoroutineIdle)
    coroutinePool.StartCoroutine(Foo());//Or like this.

InternalPool:

and you can avoid GC with some internal pools.
另外还封装了一些yield函数的内部池,避免诸如new WaitForSeconds(...)带来的GC开销。

IEnumerator Foo()
{
    yield return new WaitForSeconds(1f);//wait 1 sec, has GC.

    var waitForSeconds = CoroutineHelper.Pool_WaitForSeconds.Spawn();
    yield return waitForSeconds.Reset(1f);
    CoroutineHelper.Pool_WaitForSeconds.Despawn(waitForSeconds);//wait 1 sec with CoroutineHelper, no GC.
}

Statistics:

Finally, there is a counting function to troubleshoot leaks.
最后,还有一个计数功能,来排查泄露问题

void OnGUI()
{
    GUILayout.Box("Coroutine Count: " + CoroutineHelper.CoroutineCount);
    GUILayout.Box("Pool Total Size: " + CoroutineHelper.PoolTotalSize);
}

coroutinehelper's People

Contributors

hont127 avatar

Stargazers

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