WaitUntil和WaitWhile
来源:互联网 发布:windows系统启动 efi 编辑:程序博客网 时间:2024/05/21 01:45
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
0 0
- WaitUntil和WaitWhile
- [Selenium] selenium — selenium三种等待方式解读,waituntil sleep等
- . 和 ->
- || 和&&
- .和::
- “?”和“!”
- #和##
- && 和 ||
- 、和
- #和##
- .//和..//
- 和
- ?和\?
- ./ 和 ../
- #和##
- #、和##
- #和##
- *和&
- IE8 OCX 无法进入DEBUG
- 打印功能
- POJ 3261 Milk Patterns
- 建立自增序列
- fresco 显示缩略图,不直接使用setImageURI,防止卡顿和显示不全:纯黑色或纯白色
- WaitUntil和WaitWhile
- 解决UITableViewCell 上放置 UIScrollView 两者手势冲突
- 数据结构与算法--基本知识
- 在VB.net中使用Entity Framework(vb.net EF6)
- CSS3的calc()使用
- javaWeb项目中的单例,多例,
- 关于Oracle中dbms_jobs的使用,异常:表或视图不存在
- 不可不知的 Android strings.xml 那些事
- JAVA多线程之Thread&&Runnable