第五步,现在我们猜测一下,一般来说3种陨石的生成应该调用同一个函数或至少在一起,尤其是制作陨石的Prefabs基本很相似,简单浏览下余下所有脚本的状况后,尝试一下这个想法,我们记得第一个陨石是在GameController.cs脚本中,去看一下,果然发现public GameObject[] hazard;变成了一个数组,同时不仅填入3中陨石,还填入了敌方飞船的prefab,我们填写hazards这种数组是,在size填写相应个数,然后回车,就会多出几个填写行
public GameObject[] hazards; //用来存储陨石和飞船的预设体prefab
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait = 4.0f;
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOvetText;
private bool gameOver;
private bool restart;
private int score;
// Use this for initialization
void Start () {
gameOver = false;
restart = false;
restartText.text = "";
gameOvetText.text = "";
score = 0;
UpdateScore (); //start中初始化各种数据
StartCoroutine (SpawnWaves ()); //StartCoroutine从各种行为看就像是开启一个新的线程,单独做一件事,很多事不放在Update中了,这么做的好坏我暂时无法判断,也许能效率高些,或是不想和其它同步
}
void Update() {
if (restart) {
if (Input.GetKeyDown (KeyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
} //restart 为true,可以点R键重新载入场景
}
IEnumerator SpawnWaves() {
yield return new WaitForSeconds (startWait);
while (true) {
for (int i = 0; i < hazardCount; i++) {
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
} //每隔一段时间,按spawnWait间隔随机在hazards里选一个生成
yield return new WaitForSeconds (waveWait);
if (gameOver) {
restartText.text = "Press 'R' to restart";
restart = true;
break;
} //gameOver为true,改变restartText上的文字,进入可以重新开始状态,同时跳出生成Enemy的循环
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
} //给总分score上加分,同时更新分数栏文字
void UpdateScore() {
scoreText.text = "Score: " + score;
} //更新分数栏文字
public void GameOver()
{
gameOvetText.text = "Game Over";
gameOver = true;
} //设置gameover状态,在其他脚本中调用,所以是public、
其实就是改了 public GameObject[] hazards; //用来存储陨石和飞船的预设体prefab
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
Instantiate (hazard, spawnPosition, spawnRotation);
试验下,石头可以变成3种了,本步修改完成