【Unity】协程管理中心

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoroutineManager : SingletonTemplate
{
    private Dictionary ieDic = new Dictionary();

    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();
    }
}

你可能感兴趣的:(【Unity】协程管理中心)