unity3D技术分享

【Unity】协程管理中心

2019-03-01  本文已影响14人  木心Sepith
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoroutineManager : SingletonTemplate<CoroutineManager>
{
    private Dictionary<string, IEnumerator> ieDic = new Dictionary<string, IEnumerator>();

    public void StartCorotine(string name, IEnumerator ie)
    {
        if (ieDic.ContainsKey(name)) return;

        ieDic.Add(name, ie);
        GameManager.inst.StartCoroutine(ie);
    }

    public void StopCorotine(string name)
    {
        if (!ieDic.ContainsKey(name)) return;

        IEnumerator cor = ieDic[name];
        GameManager.inst.StopCoroutine(cor);
        ieDic.Remove(name);
    }

    public void Clear()
    {
        foreach (var v in ieDic)
        {
            GameManager.inst.StopCoroutine(v.Value);
        }

        ieDic.Clear();
    }
}
上一篇下一篇

猜你喜欢

热点阅读