Unity 协程实现的一些原理

曾被问道  Unity 协程是如何知道条件是否满足的。

直到看了一个讲师的视频后才明白一些。

IEnumerator  迭代器是一个容器,可以装一个一个的函数,
依次执行每个函数(即:MoveNext()),每个函数会有一个返回值,
返回值会保存在  IEnumerator.Current对象里。

yield return null;
yield 是单独存在的,yield是yield,return null是return null。 yield做了什么事情呢?
yield把它之前的代码 “Debug.Log("111111111");”和 “return 1;”做成一个函数放到IEnumerator容器中

然后在更新函数里每帧判断是否满足条件,满足就开始执行下一个函数。

由于对C#语言还做不到深入的理解,迭代器到底咋回事不清楚,所以讲师讲到的明白,其它的条件还是不会实现。

 

以下是测试代码:

public class CoroutineTest : MonoBehaviour
{
    IEnumerator e = null;

    //IEnumerator  迭代器是一个容器,可以装一个一个的函数,
    //依次执行每个函数,每个函数会有一个返回值,
    //返回值会保存在  IEnumerator.Current对象里。

    //yield return null;
    //yield 是单独存在的,yield是yield,return null是return null。 yield做了什么事情呢?
    //yield把它之前的代码 “Debug.Log("111111111");”和 “return 1;”做成一个函数放到IEnumerator容器中
    //function0()
    //{
    //    Debug.Log("111111111");
    //    return 1;
    //}

    //function1()
    //{
    //    Debug.Log("22222222");
    //    return 2;
    //}

    //function2()
    //{
    //    Debug.Log("我要睡5秒钟");
    //    return new WaitForSeconds(5);
    //}

    IEnumerator start_Coroutine()
    {
        Debug.Log("111111111");
        yield return 1;

        Debug.Log("22222222");
        yield return 2;

        Debug.Log("我要睡5秒钟");
        yield return new ClassWaitForTime(5);

        Debug.Log("我醒了");
        yield break;//yield break是一起的吗?
    }

    void Start()
    {
        e = start_Coroutine();

        //while (e.MoveNext())//执行当前的函数,返回值存放到IEnumerator.Current并把位置拨动到下一个
        //{
        //    Debug.Log(e.Current);
        //}
    }

    private void LateUpdate()
    {
        if (e != null)
        {
            if (e.Current is ClassWaitForTime)
            {
                ClassWaitForTime wt = e.Current as ClassWaitForTime;
                wt.Update(Time.deltaTime);
                if (!wt.IsOver())
                {
                    return;//如果时间没有到,就不往后面执行
                }
            }

            if (!e.MoveNext()) //如果枚举数已成功地推进到下一个元素,则为 true;如果枚举数传递到集合的末尾,则为 false。
            {
                e = null;
            }
        }
    }  
}

//自己实现一个类似 new WaitForSeconds()
public class ClassWaitForTime
{
    private float total;
    private float now;

    public ClassWaitForTime(float time)
    {
        total = time;
        now = 0;
    }

    public void Update(float dt)
    {
        now += dt;
    }

    public bool IsOver()
    {
        return now >= total;
    }
}

  • 0
    点赞
  • 3
    收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wang123qian

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值