U3D点滴-Coroutines

using UnityEngine;
using System.Collections;
 
public class Countdown : MonoBehaviour
{
     public float timer = 3;
 
     void Update()
     {
         timer -= Time.deltaTime;
         if (timer <= 0)
             Debug.Log( "Timer has finished!" );
     }
}
Not too bad, very little code that serves its purpose well. But the problem is, what if we have more complicated components (like a Player or Enemy class) that have multiple timers? Our code might start to look like this:

不太糟糕,对于少量代码来说是合适的,但是问题是,如果我们有很多大量的计时器呢


using UnityEngine;
using System.Collections;
 
public class MultiTimer : MonoBehaviour
{
     public float firstTimer = 3;
     public float secondTimer = 2;
     public float thirdTimer = 1;
 
     void Update()
     {
         firstTimer -= Time.deltaTime;
         if (firstTimer <= 0)
             Debug.Log( "First timer has finished!" );
 
         secondTimer -= Time.deltaTime;
         if (secondTimer <= 0)
             Debug.Log( "Second timer has finished!" );
 
         thirdTimer -= Time.deltaTime;
         if (thirdTimer <= 0)
             Debug.Log( "Third timer has finished!" );
     }
}
Which isn’t too bad, but personally I don’t like having those timer variables muddy up my code. They feel dirty, and I always have to make sure to reset them when I want to restart the timers (which I often forget to do).

不算太糟,但是个人感觉不太喜欢这些个计时器变量在代码中,而且我不得不去重启计时器。


for ( float timer = 3; timer >= 0; timer -= Time.deltaTime)
{
     //Just do nothing...
}
Debug.Log( "This happens after 5 seconds!" );
This would be so much nicer, because now the variable for each timer can just be part of the loop, and I don’t have all these iterating variables sitting around.

这个要好多了,因为现在每个计时器的变量就是每个循环中的一部分。



下面是coroutine代码实例:

using UnityEngine;
using System.Collections;
 
public class CoroutineCountdown : MonoBehaviour
{
     void Start()
     {
         StartCoroutine(Countdown());
     }
 
     IEnumerator Countdown()
     {
         for ( float timer = 3; timer >= 0; timer -= Time.deltaTime)
             yield return 0;
 
         Debug.Log( "This message appears after 3 seconds!" );
     }
}

StartCoroutine(Countdown());

这句话就是开始我们countdown方法。


When you “yield” a function, you’re effectively saying, “Stop this function now, then resume from here the next frame!”

当你使用yield方法时候,意味着,从这个现在起停止这个方法,然后返回去执行下一帧。


秒计数:

IEnumerator CountSeconds()
{
     int seconds = 0;
 
     while ( true )
     {
         for ( float timer = 0; timer < 1; timer += Time.deltaTime)
             yield return 0;
 
         seconds++;
         Debug.Log(seconds + " seconds have passed since the Coroutine started." );
     }
}


Starting & Stopping Coroutines


startCoroutine ( FirstTimer () );

startCoroutine ( SecondTimer () );


//If you start a Coroutine by name...
StartCoroutine( "FirstTimer" );
StartCoroutine( "SecondTimer" );
 
//You can stop it anytime by name!
StopCoroutine( "FirstTimer" );


原文引用:http://unitypatterns.com/introduction-to-coroutines/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值