寫了個分幾輪出怪的協程,協程正常運行,但忽然發現無法暫停這個協程,用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);
}
uj5u.com熱心網友回復:
啥叫“暫停”?是不是要找愛因斯坦來研究一下?uj5u.com熱心網友回復:
如果連業務辦理都不知道如何操作,空談編程,一定是亂講的。技術算個屁,關鍵是要做業務。
uj5u.com熱心網友回復:
比如說,你去求職應聘,然后人家如果用你就會給你打電話通知你。你現在偏說你要卡死在人家門前,不懂得什么叫做“等待電話通知”。搞不懂基本的生活中的異步設計概念,非要糾結什么阻塞的概念。那么除了撰寫剛入門時學的簡單的數學函式,還會什么互動式的“暫停”設計?uj5u.com熱心網友回復:
AutoResetEvent。ManualReserEventuj5u.com熱心網友回復:
你必須要在你想暫停的地方設定一個條件觸發。。。否則無法控制
uj5u.com熱心網友回復:
想告訴你,但又怕你打我,while(true) { if (cond) { break} yield return null;}uj5u.com熱心網友回復:
可以使用Time.timeScale = 0;和Time.timeScale = 1;轉換來實作暫停和重新開始uj5u.com熱心網友回復:
那就不能只靠協程本身了,暫停協程應該是沒有這個功能。但是結合整體代碼。
在我的代碼上,我操控多段物體的升高和下降,按鈕的開啟與關閉。
用的就是協程來做,那么使用time.timeScale=0;
在協程中加入while(time.timeScale==0)
{
yield return null;
}
我并不清楚會產生什么嚴重后果,至少我先實作了功能
如果有人覺得這樣做會留下大坑,請務必留下你的指導
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/124806.html
標籤:C#
上一篇:C#
下一篇:怎么獲得字串的位元組數呢?
