Re: [請益] Unity StartCoroutine for loop

看板GameDesign (遊戲設計)作者 (阿龍哥)時間8年前 (2017/01/03 01:00), 編輯推噓3(308)
留言11則, 3人參與, 最新討論串2/2 (看更多)
這是我重現的code, 在pc上執行起來沒問題 ( 推薦一個plugin http://u3d.as/5E8 ) 從你的描述上看, 你應該有做所謂的暫停的功能 請檢查你是否有在特定條件下把Time.timeScale設為0 (或小值, 如0.05f) 並且請在切Scene前或切Scene後將其重設為1 using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class GameControl : MonoBehaviour { float startWait = 0.5f; float spawnWait = 0.5f; float waveWait = 0.5f; int hazardCount = 5; float xMin = -10f; float xMax = 10f; float yMax = 10f; bool gameOver = true; int waveCount = 1; void Start() { StartCoroutine (SpawnWaves ()); } IEnumerator SpawnWaves () { yield return new WaitForSeconds (startWait); while(true) { for (int i = 0; i < hazardCount; ++i){ Vector3 spawnPosition = new Vector3 (Random.Range (xMin, xMax), yMax, 0); Debug.LogFormat( "{0} trying to create enemy: {1} at {2}", waveCount, i, spawnPosition ); yield return new WaitForSeconds (spawnWait); } waveCount++; yield return new WaitForSeconds (waveWait); if( waveCount > 3 ) { Debug.Log( "Restart" ); Reload(); break; } } } void Reload() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } } ※ 引述《smailzhu (嗯嗯)》之銘言: : 各位前輩,小弟我在練習開發android上的遊戲 : 我在電腦上執行 : SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex) ; : 可以成功的重新開始這個scene : 但是當我輸出成APK到手機上執行時 : 敵人就沒有辦法順利生成 : 我敵人生成的方式是在start()內呼叫 StartCoroutine (SpawnWaves ()); : IEnumerator SpawnWaves (){ : yield return new WaitForSeconds (startWait); : while(true){ : for (int i = 0; i < hazardCount; ++i){ : Vector3 spawnPosition = : new Vector3 (Random.Range (xMin, xMax), yMax, 0); : //我有測試在這邊將xMin,xMax,yMax,hazard,hazardCount,i,Time.time顯示在螢幕上 : //在還沒有重新執行時i都順利增加,Time.time也會跑,可是當我重新開始時i跟時間 : //就只會卡在一個值了 : Instantiate (hazard, spawnPosition, transform.rotation); : yield return new WaitForSeconds (spawnWait); : } : yield return new WaitForSeconds (waveWait); : if (gameOver) { : restartText.text = "Double click to Restart"; : restart = true; : break; : } : } : } : 想請教各位前輩可以幫我提點一下嗎,謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.127.133.252 ※ 文章網址: https://www.ptt.cc/bbs/GameDesign/M.1483376411.A.A8F.html

01/03 01:01, , 1F
補充一下, 在此前題下, 會卡住不是因為Coroutine, 是因為
01/03 01:01, 1F

01/03 01:02, , 2F
WaitForSeconds
01/03 01:02, 2F

01/03 04:40, , 3F
謝謝提點,我晚點來試試是不是延遲的問題
01/03 04:40, 3F

01/03 09:44, , 4F
成功了,謝謝你,我把三個延遲設初使值後又移掉還是正常
01/03 09:44, 4F

01/03 09:44, , 5F
了囧
01/03 09:44, 5F

01/03 09:44, , 6F
我覺得納悶的是為什麼我當初在測試電腦版正常,手機卻無
01/03 09:44, 6F

01/03 09:44, , 7F
法...
01/03 09:44, 7F

01/03 13:25, , 8F
你那幾個public 變數是怎麼設的?
01/03 13:25, 8F

01/03 13:25, , 9F
有把game controller做成prefab 嗎?
01/03 13:25, 9F

01/06 21:18, , 10F
我在Instantiate裡的hazard有做成prefab
01/06 21:18, 10F

01/06 21:23, , 11F
前面的public 是設public float
01/06 21:23, 11F
文章代碼(AID): #1OQeSRgF (GameDesign)
討論串 (同標題文章)
文章代碼(AID): #1OQeSRgF (GameDesign)