参考游戏蛮牛:http://www.unitymanual.com/thread-38427-1-1.html?_dsign=66ce5cc0
下面我们举一些简单的例子说明Coroutine的使用方法和一些注意事项:
[1]最基本使用
using
UnityEngine;
using
System.Collections;
public
class
ExampleClass : MonoBehaviour {
void
Start() {
print(
"Starting "
+ Time.time);
StartCoroutine(WaitAndPrint(2.0F));
print(
"Before WaitAndPrint Finishes "
+ Time.time);
}
IEnumerator WaitAndPrint(
float
waitTime) {
yield
return
new
WaitForSeconds(waitTime);
print(
"WaitAndPrint "
+ Time.time);
}
}
private
IEnumerator CountDown(
float
fTime)
{
Debug.Log(
"Make a timer ftime is "
+ fTime);
for
(
float
timer = fTime;timer >= 0;timer -= Time.deltaTime)
yield
return
0;
Debug.Log(
"After "
+ fTime +
" log this message."
);
//TODO do something you want
}
[3]指定一个时间执行函数序列,要知道其中的一个”坑“如果使用WaitForSeconds();你的TimeScale被设置成了0,那么你的Coroutine将不会返回除非你的TimeScale重新设置成了非0值,例子中展示了如何让启动的Coroutine不受到TimeScale的影响
public
class
CallEvents : MonoBehaviour
{
public
class
CellEventFuntion
{
public
float
fTime;
public
CallBack callBack;
}
public
delegate
void
CallBack();
public
delegate
void
CallBack<T>(T t);
public
delegate
void
CallBack<T, K>(T t1, K t2);
public
List<CellEventFuntion> eventList =
new
List<CellEventFuntion>();
//设定一个根据时间调用的队列(每隔一定的时间调用不同的回调函数)
public
void
CallFunctionListByTimes()
{
eventList.Clear();
//添加新的Event到我们的list中
CellEventFuntion newFunction =
new
CellEventFuntion();
newFunction.fTime = 2.0f;
newFunction.callBack = Function01;
CellEventFuntion newFunction02 =
new
CellEventFuntion();
newFunction02.fTime = 2.0f;
newFunction02.callBack = Function02;
CellEventFuntion newFunction03 =
new
CellEventFuntion();
newFunction03.fTime = 2.0f;
newFunction03.callBack = Funciton03;
CellEventFuntion newFunction04 =
new
CellEventFuntion();
newFunction04.fTime = 5.0f;
newFunction04.callBack = Function04;
eventList.Add(newFunction);
eventList.Add(newFunction02);
eventList.Add(newFunction03);
eventList.Add(newFunction04);
//进行执行List中所有的Event
//将会受到TimeScale的影响
//StartCoroutine(_CallFuctionListByTimes());
//不会受到TimeScale的影响
StartCoroutine(_CallFunctionListByTimeIgnoreTimeScale());
}
private
IEnumerator _CallFuctionListByTimes()
{
foreach
(CellEventFuntion fuction
in
eventList)
{
float
fTime = fuction.fTime;
yield
return
new
WaitForSeconds(fTime);
if
(fuction.callBack !=
null
)
fuction.callBack();
}
}
private
IEnumerator _CallFunctionListByTimeIgnoreTimeScale()
{
foreach
(CellEventFuntion function
in
eventList)
{
float
fTime = function.fTime;
yield
return
StartCoroutine(_WaitTimeEnd(fTime));
if
(function.callBack !=
null
)
function.callBack();
//can be replaced by function.(这段是和使用_WaitTimeEnd一样的效果)
//float start = Time.realtimeSinceStartup;
//while (Time.realtimeSinceStartup < start + fTime)
//{
// yield return null;
//}
}
}
private
IEnumerator _WaitTimeEnd(
float
fTime)
{
float
start = Time.realtimeSinceStartup;
while
(Time.realtimeSinceStartup < start + fTime)
{
yield
return
null
;
}
}
private
void
Function01()
{
//Debug.Log("--Call Function01**" + Time.time);
Debug.Log(
"--Call Function01**"
+ Time.realtimeSinceStartup);
}
private
void
Function02()
{
//Debug.Log("--Call Function02**" + Time.time);
Debug.Log(
"--Call Function02**"
+ Time.realtimeSinceStartup);
}
private
void
Funciton03()
{
//Debug.Log("--Call Function03**" + Time.time);
Debug.Log(
"--Call Function03**"
+ Time.realtimeSinceStartup);
}
private
void
Function04()
{
//Debug.Log("--Call Function04**" + Time.time);
Debug.Log(
"--Call Function04**"
+ Time.realtimeSinceStartup);
}
public
void
PauseGame()
{
Time.timeScale = 0.0f;
}
public
void
Resume()
{
Time.timeScale = 1.0f;
}
void
OnDestroy()
{
StopAllCoroutines();
}
}