Unity协程管理器工具脚本,自己修改网上的一个版本

方便管理协程的开始暂停提前结束。增加了常用的延迟执行的协程,重复执行协程。

顺便在此宣传下Unity挖坑爬坑群:853164080,人数比较少,见谅,喜欢钻研技术,喜欢自己挖坑,帮别人填坑的朋友都可以进来一起挖坑埋坑。

using UnityEngine;  
using System.Collections;
using System;

public class Task
{

    public bool Running
    {
        get
        {
            return task.Running;
        }
    }

 
    public bool Paused
    {
        get
        {
            return task.Paused;
        }
    }

    public delegate void FinishedHandler(bool manual);


    public event FinishedHandler Finished;

    public Task(Action action, float time, bool autoStart = true)
    {
        task = TaskManager.CreateTask(action, time);
        task.Finished += TaskFinished;
        if (autoStart)
            Start();    
    }


    public Task(Func<bool> func,float repeatTime,bool autoStart = true)
    {
        task = TaskManager.CreateTask(func, repeatTime);
        task.Finished += TaskFinished;
        if (autoStart)
            Start();
    }

    public Task(FuncOut func, bool autoStrat = true)
    {
        task = TaskManager.CreateTask(func);
        task.Finished += TaskFinished;
        if (autoStrat)
            Start();
    }


    public Task(IEnumerator c, bool autoStart = true)
    {
        task = TaskManager.CreateTask(c);
        task.Finished += TaskFinished;
        if (autoStart)
            Start();
    }

    public void Start()
    {
        task.Start();
    }


    public void Stop()
    {
        task.Stop();
    }

    public void Pause()
    {
        task.Pause();
    }

    public void Unpause()
    {
        task.Unpause();
    }

    void TaskFinished(bool manual)
    {
        FinishedHandler handler = Finished;
        if (handler != null)
            handler(manual);
    }

    TaskManager.TaskState task;
}



class TaskManager : MonoBehaviour
{
    public class TaskState
    {
        public bool Running
        {
            get
            {
                return running;
            }
        }

        public bool Paused
        {
            get
            {
                return paused;
            }
        }

        public delegate void FinishedHandler(bool manual);
        public event FinishedHandler Finished;

        IEnumerator coroutine;

        bool running = false;
        bool paused = false;
        bool stopped = false;

        public TaskState(IEnumerator c)
        {
            coroutine = c;
        }

        public void Pause()
        {
            paused = true;
        }

        public void Unpause()
        {
            paused = false;
        }

        public void Start()
        {
            running = true;
            Singleton.StartCoroutine(CallWrapper());
        }

        public void Stop()
        {
            stopped = true;
            running = false;
        }

        IEnumerator CallWrapper()
        {
            yield return null;
            IEnumerator e = coroutine;
            while (running)
            {
                if (paused)
                    yield return null;
                else
                {
                    if (e != null && e.MoveNext())
                    {
                        yield return e.Current;
                    }
                    else
                    {
                        running = false;
                    }
                }
            }
            FinishedHandler handler = Finished;
            if (handler != null)
                handler(stopped);
        }

       
    }

    static TaskManager instance;
    static TaskManager Singleton { get { if (instance == null) instance = FindObjectOfType<TaskManager>(); return instance; } }

    public static TaskState CreateTask(IEnumerator coroutine)
    {
        return new TaskState(coroutine);
    }


    public static TaskState CreateTask(Action action,float time)
    {
        return new TaskState(Singleton.DelayCoroutine(action, time));
    }

    public static TaskState CreateTask(Func<bool> func,float time)
    {
        return new TaskState(Singleton.RepeatCoroutine(func, time));
    }

    public static TaskState CreateTask(FuncOut func)
    {
        return new TaskState(Singleton.RepeatCoroutine(func));
    }


    private IEnumerator DelayCoroutine(Action a,float t)
    {
        while (t > 0)
        {
            if (t > 0.02f)
            {
                yield return new WaitForSeconds(0.02f);
                t -= 0.02f;
            }
            else
            {
                yield return new WaitForSeconds(t);
                t -= t;
            }
        }
        a?.Invoke();
    }

    private IEnumerator RepeatCoroutine(Func<bool> f,float t)
    {
        float temp = t;
        while (f())
        {
            temp = t;
            while (temp > 0)
            {
                if (temp > 0.02f)
                {
                    yield return new WaitForSeconds(temp);
                    temp -= 0.02f;
                }
                else
                {
                    yield return new WaitForSeconds(temp);
                    temp -= temp;
                }
            }
        }
    }

    private IEnumerator RepeatCoroutine(FuncOut f)
    {
        float t;
        while (f.Invoke(out t))
        {
            while (t > 0)
            {
                if (t > 0.02f)
                {
                    yield return new WaitForSeconds(t);
                    t -= 0.02f;
                }
                else
                {
                    yield return new WaitForSeconds(t);
                    t -= t;
                }
            }
        }
    }
}

public delegate bool FuncOut(out float time);

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mayikami

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值