http://www.manew.com/thread-92253-1-1.html
目标
本篇博文的主要目标是告诉你在项目中如何使用WaitUntil和WaitWhile这两个API函数。
是否混淆了WaitUntil和WaitWhile?
不知道如何使用WaitUntil和WaitWhile?
如果你是上面这两种情况,那么现在就该我为你服务了。我假定在你阅读这篇博文前你已经了解了协程,因为 WaitUntil和 WaitWhile仅仅在协程中的 yield语句后面出现。 如果你还不了解协程,请看下面的链接:
Mastering Coroutines in Unity in 10 mins
不知道如何使用WaitUntil和WaitWhile?
如果你是上面这两种情况,那么现在就该我为你服务了。我假定在你阅读这篇博文前你已经了解了协程,因为 WaitUntil和 WaitWhile仅仅在协程中的 yield语句后面出现。 如果你还不了解协程,请看下面的链接:
Mastering Coroutines in Unity in 10 mins
开始学习WaitUntil:
θ 根据定义,它挂起语句, 直到指定的条件返回 true。
θ 换句话说,当我们指定的条件是 false时,它不会 继续执行语句。 unity 将会等待条件返回 true 。
OK ,我们了解了概念,下一步该做什么呢? 现在我们将用一个简单的 demo 来告诉你如何使用,在什么地方使用它。 假设你是在开发一个汽车竞速游戏,并且当你的油量为空时通知玩家。 在这种情况下,看看如下代码:
θ 根据定义,它挂起语句, 直到指定的条件返回 true。
θ 换句话说,当我们指定的条件是 false时,它不会 继续执行语句。 unity 将会等待条件返回 true 。
OK ,我们了解了概念,下一步该做什么呢? 现在我们将用一个简单的 demo 来告诉你如何使用,在什么地方使用它。 假设你是在开发一个汽车竞速游戏,并且当你的油量为空时通知玩家。 在这种情况下,看看如下代码:
[C#]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using
UnityEngine;
using
System.Collections;
public
class
Demo : MonoBehaviour {
public
int
counter;
void
Start() {
counter = 20;
StartCoroutine(FuelNotification());
}
IEnumerator FuelNotification() {
Debug.Log(
"Waiting for tank to get empty"
);
yield
return
new
WaitUntil(()=> counter <= 0);
Debug.Log(
"Tank Empty!"
);
//Notification
}
void
Update() {
if
(counter > 0)
{
Debug.Log(
"Fuel Level: "
+ counter);
counter--;
}
}
}
|
当counter
变量变为
0
,玩家会得到一个通知。

非常简单,不是吗?
Yeah! 但是和WaitUntil一起使用的神秘的“() =>”是什么呢?好,我将为你解释这个
问题。这个东西叫做
Lambda表达式。(仅仅
=>
部分,不包含括号)它是一种匿名方法(另一个
C#
概念),它隐式地获得传递变量的信息,它大量使用在
LINQ
表达式中。
Lambda表达式是一个非常便利的工具,试一试吧。
下面的链接展示它如何工作:
θ https://msdn.microsoft.com/en-us/library/bb397687.aspx
θ https://www.youtube.com/watch?v=LDgQ-spnrYY
无论如何,如果这是你的大脑混乱,我们有另外一个解决办法:
下面的链接展示它如何工作:
θ https://msdn.microsoft.com/en-us/library/bb397687.aspx
θ https://www.youtube.com/watch?v=LDgQ-spnrYY
无论如何,如果这是你的大脑混乱,我们有另外一个解决办法:
[C#]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
using
UnityEngine;
using
System.Collections;
public
class
Demo : MonoBehaviour {
public
int
counter;
void
Start() {
counter = 20;
StartCoroutine(FuelNotification());
}
IEnumerator FuelNotification() {
Debug.Log(
"Waiting for tank to get empty"
);
yield
return
new
WaitUntil(IsTankEmpty);
Debug.Log(
"Tank Empty!"
);
}
void
Update() {
if
(counter > 0)
{
Debug.Log(
"Fuel Level: "
+ counter);
counter--;
}
}
public
bool
IsTankEmpty(){
if
(counter>0){
return
false
; }
else
{
return
true
; }
}
}
|
检查控制台;输出是一样的,对吧?学会了吗?一整个方法仅仅一行代码,看看Lambda
表达式是多么有威力。
现在我希望你忘记前面说的
WaitUntil
。
现在来学习WaitWhile:WaitWhile
是WaitUntil
的对立面。它抑制表达式,当条件为真。不像
WaitUntil
,它将会等待条件变为
false
,才能指向后面的代码块。
和前面同样的例子,我们使用WaitWhile:
[C#]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using
UnityEngine;
using
System.Collections;
public
class
Demo : MonoBehaviour {
public
int
counter;
void
Start() {
counter = 20;
StartCoroutine(FuelNotification());
}
IEnumerator FuelNotification() {
Debug.Log(
"Waiting for tank to get empty"
);
yield
return
new
WaitWhile(()=> counter > 0);
Debug.Log(
"Tank Empty!"
);
}
void
Update() {
if
(counter > 0)
{
Debug.Log(
"Fuel Level: "
+ counter);
counter--;
}
}
}
|
你可以看见,仅仅处理条件的机制不同。其他部分都一样。 但是请注意使用这些概念是一把双刃剑。 如果你的运算量非常巨大,这将会降低游戏的性能,因为这些函数每一帧都会调用。 好了,我将要结束这篇关于
WaitUntil
和
WaitWhile
的博客。如果你对这篇博客的任何地方还是有疑惑,请在下面留言。保持联系,就像大多数博客一样。想要获得游戏开发的想法?你还在等什么?现在就联系我们并获得想法。我们的公司是印度最好的Unity 3D
游戏开发公司之一。
原文作者: Viju Dudiya
原文链接: http://www.theappguruz.com/blog/the-mystery-of-waituntil-waitwhile-in-unity-5-3-revealed
原文作者: Viju Dudiya
原文链接: http://www.theappguruz.com/blog/the-mystery-of-waituntil-waitwhile-in-unity-5-3-revealed