您的位置:首页 > 移动开发 > Unity3D

Unity 协程(coroutine)详细使用

2017-05-30 14:54 393 查看
1:只讲使用,没有介绍(因为。。。不会介绍),使用的是unity5.5

2:使用

(1)如何开启协程和关闭协程?

开启协程和关闭协程有两种,第1种是通过XX开启关闭,第2种是通过字符串开启关闭

第一种:

private Coroutine coroutine;
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
coroutine = StartCoroutine(IETestCoroutine());
}
else if (Input.GetKeyDown(KeyCode.Space))
{
if (coroutine != null)
StopCoroutine(coroutine);
}
}
IEnumerator IETestCoroutine()
{
while (true)
{
yield return null;
Debug.Log("coroutine");
}
}
或者:

private IEnumerator coroutine;
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
coroutine = IETestCoroutine();
StartCoroutine(coroutine);
}
else if (Input.GetKeyDown(KeyCode.Space))
{
if (coroutine != null)
StopCoroutine(coroutine);
}
}
IEnumerator IETestCoroutine()
{
while (true)
{
yield return null;
Debug.Log("coroutine");
}
}
第二种通过字符串形式:

   

void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
StartCoroutine("IETestCoroutine");
}
else if (Input.GetKeyDown(KeyCode.Space))
{
StopCoroutine("IETestCoroutine");
}
}
IEnumerator IETestCoroutine()
{
while (true)
{
yield return null;
Debug.Log("coroutine");
}
}
建议用第一种!!!

3:介绍下协程里面的几个方法

当我们需要用协程等待具体的时间可以用

(1)

IEnumerator IETestCoroutine()
{
//等待1s,受time.scale限制
yield return new WaitForSeconds(1f);

//等待1s,不收time.scale限制
//yield return new WaitForSecondsRealtime(1f);

//do something......
}
这里介绍一下
yield return new WaitForSecondsRealtime


这个呢是不受time.scale 限制的,如果你有调节time.scale,那么使用这个方法将不会受影响

(2)重点介绍 yield return new WaitUntil();  yield return new WaitWhile();这两个方法

(.1)yield return new WaitUntil()的使用,WaitUnitl指的意思就是当满足这个条件的时候才会执行下面的语句,比如

private bool isDownSpace = false;
void Start()
{
StartCoroutine(IETestCoroutine());
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
isDownSpace = true;
}
else if (Input.GetKeyDown(KeyCode.S))
{
isDownSpace = false;
}
}
IEnumerator IETestCoroutine()
{
yield return new WaitUntil(TestResult);
Debug.Log("Space down");
}
bool TestResult()
    {
        return isDownSpace;
    }
只有我们按下空格键,才会打印“Space down”这句话,这是一个条件等待,waitUnitl里面是个Func委托,我们也可以这样

private bool isDownSpace = false;
void Start()
{
StartCoroutine(IETestCoroutine());
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
isDownSpace = true;
}
else if (Input.GetKeyDown(KeyCode.S))
{
isDownSpace = false;
}
}
IEnumerator IETestCoroutine()
{
yield return new WaitUntil(()=>isDownSpace==true);
Debug.Log("Space down");
}
这两个效果是一样的

(.2)yield return new Wait While()的使用,

private bool isDownSpace = true;
void Start()
{
StartCoroutine(IETestCoroutine());
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
isDownSpace = true;
}
else if (Input.GetKeyDown(KeyCode.S))
{
isDownSpace = false;
}
}
IEnumerator IETestCoroutine()
{
yield return new WaitWhile(()=>isDownSpace);
Debug.Log("Space down");
}
这里我将isDownSpace 初始值为true,当我们按下S的时候就会打印" Space down” 这句话。。。。

没错你可能觉察到了,这个和waitUntil相反,官方也解释的很清楚

waitUntil :Suspends the coroutine execution until the supplied delegate evaluates to 
true


waitWhile:Suspends the coroutine execution until the supplied delegate evaluates to 
false
.

附上链接哈,里面也有例子,如果我举得这个例子不好的话,看看官方的,https://docs.unity3d.com/ScriptReference/WaitWhile.html

就比如说:

waitUntil

//当满足后面的委托条件返回为true的时候执行下面的这句话

//这里就是isDonwSpace为true的时候执行下面的话
yield return new WaitUntil(() => isDownSpace==true);
Debug.Log("Space down true");
//这里就是isDonwSpace为false的时候执行下面的话
yield return new WaitUntil(() => isDownSpace == false);
Debug.Log("Space down false");


waitWhile

//当满足后面的委托条件返回为false的时候执行下面的这句话

//这里就是isDonwSpace为false的时候执行下面的话
yield return new WaitWhile(() => isDownSpace == true);
Debug.Log("Space down false");

//这里就是isDonwSpace为true的时候执行下面的话
yield return new WaitWhile(() => isDownSpace == false);
Debug.Log("Space down true");


ok。。。懂了不?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: