【妙用协程】 - 协程甚至能用来做动画

协程用途广泛,甚至可以用来控制动画效果。在手游时代著名的游戏引擎Unity 3D内置了C#作为脚本语言的支持。C#有和Python的generator几乎一样的语法(叫做Enumerator),而C#的Enumerator在Unity 3D被用作控制一个动画的过程的工具。本文是 http://unitygems.com/coroutines/ 这篇文章的一个摘要。
协程是一个可被中断然后又被继续的函数执行过程,在Unity 3D中,这个继续又中断的动机就是动画是一帧一帧的(Frame)。控制动画的协程会在每一帧中被执行一下,然后又被中断,然后又在下一帧中继续,如此往复:
请输入图片描述

代码写出来就是这个样子的:

IEnumerator Die()
{
       //Wait for the die animation to be 50% complete
       yield return StartCoroutine(WaitForAnimation("die",0.5f, true));
       //Drop the enemies on dying pickup
       DropPickupItem();
       //Wait for the animation to complete
       yield return StartCoroutine(WaitForAnimation("die",1f, false));
       Destroy(gameObject);

}

Die函数控制了一个动画人物的死亡过程。yield了之后,函数就不在被执行了,直到yield的子协程执行完了,引擎才会继续执行yield后面的动画动作。而其中的WaitForAnimation的子协程是真正细化到每一帧的动画播放过程了:

IEnumerator WaitForAnimation(string name, float ratio, bool play)
{
    //Get the animation state for the named animation
    var anim = animation[name];
    //Play the animation
    if(play) animation.Play(name);

    //Loop until the normalized time reports a value
    //greater than our ratio.  This method of waiting for
    //an animation accounts for the speed fluctuating as the
    //animation is played.
    while(anim.normalizedTime + float.Epsilon + Time.deltaTime < ratio)
        yield return new WaitForEndOfFrame();

}

把代码用协程这样写的好处是什么?就是Logic Locality,前后相关的逻辑放在一个函数内,比分散在多个回调函数中要更容易理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值