开启协程_Unity协程增加开始暂停功能

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



public static class CoroutineExtension
{
    /// <summary>
    ///扩展mono开启协程方法
    /// </summary>
    /// <param name="mono"></param>
    /// <param name="runner"></param>
    /// <returns></returns>
    public static CoroutineController StartMyCoroutine(this MonoBehaviour mono,IEnumerator runner)
    {
        CoroutineController controller = new CoroutineController(mono,runner);
        controller.Start();
        return controller;
    }
}


public class CoroutineController
{
    private static int s_id;
    public static int Id
    {
        get { return s_id; }
        private set { s_id = value; }
    }


    private MonoBehaviour m_mono;

    private MyCoroutine m_myCoroutine;

    private IEnumerator m_runner;
    private Coroutine m_coroutine;


    public CoroutineController(MonoBehaviour mono,IEnumerator runner)
    {
        Id = s_id++;
        this.m_myCoroutine = new MyCoroutine();
        this.m_mono = mono;
        this.m_runner = runner;
    }


    /// <summary>
    /// 更新运行协程
    /// </summary>
    /// <param name="runner"></param>
    /// <returns></returns>
    public bool UpdateIErunner(IEnumerator runner)
    {
        this.m_runner = runner;
        if (this.m_myCoroutine?.m_State== MyCoroutine.CoroutineState.Stop)
            return true;
        else
        {
            Debug.LogWarning("请等待当前协程停止后 再替换运行的协程");
            return false;
        }
    }

    /// <summary>
    /// 启动协程
    /// </summary>
    public void Start()
    {
        this.m_myCoroutine.m_State = MyCoroutine.CoroutineState.Running;
        this.m_coroutine = m_mono.StartCoroutine(this.m_myCoroutine.HelperIE(m_runner));
    }

    /// <summary>
    /// 暂停协程
    /// </summary>
    public void Pause()
    {
        this.m_myCoroutine.m_State = MyCoroutine.CoroutineState.Pause;
        
    }

    /// <summary>
    /// 恢复协程
    /// </summary>
    public void Resume()
    {
        this.m_myCoroutine.m_State = MyCoroutine.CoroutineState.Running;
    }

    /// <summary>
    ///停止协程
    /// </summary>
    public void Stop()
    {
        this.m_myCoroutine.m_State = MyCoroutine.CoroutineState.Stop;
    }

    /// <summary>
    /// 重启协程
    /// </summary>
    public void Restart()
    {
        if (m_coroutine != null) m_mono.StopCoroutine(this.m_coroutine);
        Start();
    }
}





public class MyCoroutine 
{
    
    public enum CoroutineState
    {
        Waitting,
        Running,
        Pause,
        Stop
    }

    public CoroutineState m_State= CoroutineState.Pause;
    /// <summary>
    /// 流程控制
    /// </summary>
    /// <returns></returns>
    public IEnumerator HelperIE(IEnumerator runnner)
    {
        while (m_State == CoroutineState.Waitting) yield return null;
        while (m_State==CoroutineState.Running)
        {
            while (m_State == CoroutineState.Pause) yield return null;
            if (runnner != null && runnner.MoveNext())
                yield return runnner.Current;
            else m_State = CoroutineState.Stop;
            while (m_State == CoroutineState.Pause) yield return null;
        }
        Debug.Log("Coroutine over");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值