作者gyd (阿龙哥)
看板GameDesign
标题Re: [请益] Unity StartCoroutine for loop
时间Tue Jan 3 01:00:08 2017
这是我重现的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://webptt.com/cn.aspx?n=bbs/GameDesign/M.1483376411.A.A8F.html
1F:→ gyd: 补充一下, 在此前题下, 会卡住不是因为Coroutine, 是因为 01/03 01:01
2F:→ gyd: WaitForSeconds 01/03 01:02
3F:推 smailzhu: 谢谢提点,我晚点来试试是不是延迟的问题 01/03 04:40
4F:→ smailzhu: 成功了,谢谢你,我把三个延迟设初使值後又移掉还是正常 01/03 09:44
5F:→ smailzhu: 了囧 01/03 09:44
6F:→ smailzhu: 我觉得纳闷的是为什麽我当初在测试电脑版正常,手机却无 01/03 09:44
7F:→ smailzhu: 法... 01/03 09:44
8F:→ madturtle: 你那几个public 变数是怎麽设的? 01/03 13:25
9F:→ madturtle: 有把game controller做成prefab 吗? 01/03 13:25
10F:推 smailzhu: 我在Instantiate里的hazard有做成prefab 01/06 21:18
11F:推 smailzhu: 前面的public 是设public float 01/06 21:23