协程管理器

2017-10-01  本文已影响41人  晓龙酱

有时我们需要分帧去执行一个任务,或者执行一个Update方法。常用的方法就是继承MonoBehaviour,或者在父类中调用当前对象的Update方法。

更简单的方法是通过协程来实现,这样对象内部只需要实现一个返回类型为IEnumerator的方法,然后通过调用MonoBehaviour单例对象的StartCoroutine来执行即可。当然我们可以包装一下,提供类如暂停,停止任务的方法。

/*
    The task can be executed only once, which means Start(), Stop() can be called just once.
    Use Pause(bool) to pause the task or continue the task.
*/
class Task
{
    static private int m_index;

    private IEnumerator m_enumerator;
    private bool m_running;
    private bool m_pause;
    private bool m_stopped;
    private Action<bool> m_finished;

    private CoroutineManager m_cm;


    public Task(CoroutineManager cm)
    {
        m_cm = cm;
    }

    // init and reset
    public void Init(IEnumerator enumerator, Action<bool> finishCallBack = null, bool startNow = false)
    {
        Debug.Assert(enumerator != null, "enumerator can't be null");

        m_enumerator = enumerator;
        m_finished = finishCallBack;
        id = ++m_index;

        m_running = false;
        m_pause = false;
        m_stopped = false;

        if(startNow)
            Start();
    }

    
    public void Start()
    {
        if(m_stopped)
            return;

        m_running = true;
        m_cm.StartCoroutine(Loop());
    }

    public void Stop()
    {
        if(m_stopped)
            return;

        m_stopped = true;
        m_running = false;
    }

    public void Pause(bool pause)
    {
        if(m_pause == pause)
            return;

        m_pause = pause;
    }

    private IEnumerator Loop()
    {
        // keep the coroutine always run after update per frame avoid any potential bugs.
        // because coroutine will run before update the first frame if coroutine is called in the Start or Awake function
        yield return null;

        while(m_running)
        {
            if(m_pause)
            {
                yield return null;
            }
            else
            {
                if(m_enumerator.MoveNext())
                {
                    yield return m_enumerator.Current;
                }
                else
                {
                    m_running = false;
                }
            }           
        }

        if(m_finished != null)
            m_finished(m_stopped);
        m_cm.Recycle(this);
    }

    public int id
    {
        get;
        private set;
    }

}
public class CoroutineManager : SingletonMonoBehaviour<CoroutineManager, ICoroutineManager>, ICoroutineManager
{
    private Dictionary<int, Task> m_tasks = new Dictionary<int, Task>();
    private LinkedList<Task> m_taskCache = new LinkedList<Task>();

    [SerializeField]
    private bool IsDebug = false;


    public int CreateCoroutine(IEnumerator enumerator, Action<bool> finishedCallBack = null, bool startNow = true)
    {
        Task task = GetTaskObj();
        task.Init(enumerator, finishedCallBack, startNow);
        m_tasks.Add(task.id, task);

        if(IsDebug)
            Debug.LogWarning("StartTask " + m_taskCache.Count + " " + m_tasks.Count);

        return task.id;
    }

    public void StartCoroutine(int id)
    {
        Task task = null;
        if(m_tasks.TryGetValue(id, out task))
            task.Start();
        else
            Debug.LogError("Can't find task to start");

        if(IsDebug)
            Debug.LogWarning("StartTask " + m_taskCache.Count + " " + m_tasks.Count);
    }

    public void StopCoroutine(int id)
    {
        Task task = null;
        if(m_tasks.TryGetValue(id, out task))
            task.Stop();
        else
            Debug.LogError("Can't find task to stop");
    }

    public void PauseCoroutine(int id, bool pause)
    {
        Task task = null;
        if(m_tasks.TryGetValue(id, out task))
            task.Pause(pause);
        else
            Debug.LogError(String.Format("Can't find task {0} to pause {1}", id, pause));

        if (IsDebug)
            Debug.LogWarning (string.Format ("PauseCoroutine {0} {1}", id, pause));
    }

    internal void Recycle(Task task)
    {
        m_tasks.Remove(task.id);
        m_taskCache.AddLast(task);

        if(IsDebug)
            Debug.LogWarning(m_taskCache.Count + " " + m_tasks.Count);
    }
        
    private Task GetTaskObj()
    {
        Task task = m_taskCache.Last == null ? null : m_taskCache.Last.Value;
        if(task != null)
            m_taskCache.RemoveLast();
        else
            task = new Task(this);

        return task;
    }
}

CoroutineManager做的事并不多,MonoBehaviour单例,缓存Task对象,对外公布接口。具体的每个协程由每个Task维护。

上一篇下一篇

猜你喜欢

热点阅读