用c#协程有什么办法能暂停么?我只看见stop。

linjiangxian11 2019-01-31 10:13:59
写了个分几轮出怪的协程,协程正常运行,但忽然发现无法暂停这个协程,用Stop就完全中断,再Start又重新开始。

不知有什么办法能让它暂停么?文中粗体字标记了重点。

求救,谢谢。

     public  float roundIntervalTime = 3f;
public float spawIntervalTime = 1f;

IEnumerator RunRound()
{
if (isPause)
{

yield return null;
//有什么办法暂停
}
//SpawnMonsterArgs e = new SpawnMonsterArgs();
for (int r = 0; r < lstRound.Count; r++)
{
//这里可以加入StartRound事件,例如可触发最后一波
roundIndex = r;
for (int i = 0; i < lstRound[r].monsterCount; i++)
{
//e.monsterID = lstRound[r].monsterID;
//SendEvent(Consts.E_SpawnMonster, e);
if (r == (lstRound.Count - 1) && (i == lstRound[r].monsterCount - 1))
{
Debug.Log(" round is end !");
}
else
{
Debug.Log("Round: r:" + r + ", i:" + i);
}
yield return new WaitForSeconds(spawIntervalTime);
}
yield return new WaitForSeconds(roundIntervalTime);
}

}

public void StartRound()
{
coroutine= Game.Instance.StartCoroutine(RunRound());
Debug.Log("RoundModel.StartRound()");
}

public void StopRound()
{
//Game.Instance.StopCoroutine(coroutine);
isPause = !isPause;
Debug.Log("isPause: "+isPause);
}
...全文
1192 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
肥橘FatOrange 2019-08-22
  • 打赏
  • 举报
回复
那就不能只靠协程本身了,暂停协程应该是没有这个功能。 但是结合整体代码。 在我的代码上,我操控多段物体的升高和下降,按钮的开启与关闭。 用的就是协程来做,那么使用time.timeScale=0; 在协程中加入while(time.timeScale==0) { yield return null; } 我并不清楚会产生什么严重后果,至少我先实现了功能 如果有人觉得这样做会留下大坑,请务必留下你的指导
ywwwfgggg 2019-06-26
  • 打赏
  • 举报
回复
想告诉你,但又怕你打我,while(true) { if (cond) { break} yield return null;}
yang121shucheng 2019-06-26
  • 打赏
  • 举报
回复
可以使用Time.timeScale = 0;和Time.timeScale = 1;转换来实现暂停和重新开始
SoulRed 2019-02-12
  • 打赏
  • 举报
回复
你必须要在你想暂停的地方设置一个条件触发。。。
否则无法控制
  • 打赏
  • 举报
回复
比如说,你去求职应聘,然后人家如果用你就会给你打电话通知你。你现在偏说你要卡死在人家门前,不懂得什么叫做“等待电话通知”。搞不懂基本的生活中的异步设计概念,非要纠结什么阻塞的概念。那么除了编写刚入门时学的简单的数学函数,还会什么交互式的“暂停”设计?
  • 打赏
  • 举报
回复
如果连业务办理都不知道如何操作,空谈编程,一定是乱讲的。 技术算个屁,关键是要做业务。
  • 打赏
  • 举报
回复
啥叫“暂停”?是不是要找爱因斯坦来研究一下?
平底锅锅锅 2019-01-31
  • 打赏
  • 举报
回复
AutoResetEvent。ManualReserEvent

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧