聲明,最近這幾篇博客都是我的筆記,所以沒仔細解釋,請原諒~~~
今天終於把Space_Shooter搞完了。但是視頻裏面的GUIText在4.6版本以上不能用。老大說,這個不用在意,因爲我們要用的是恩谷一。
上代碼——GameController
//在遊戲場景中生成障礙物;
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount; //設置循環的次數;
public float spawnWait;
public float startWait;
public float waveWait;
public Canvas scoreText;
private int score;
public string restartText;
public string gameoverText;
private bool gameOver;
private bool restart;
//第一幀自動調用;
void Start ()
{
gameOver = false;
restart = false;
restartText = "";
gameoverText = "";
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
void Update()
{
if (restart)
{
if (Input.GetKeyDown(KeyCode.R))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
//hazard孵化器;
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds(startWait);
while(true)
{
for (int i = 0; i < hazardCount; i++)
{
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);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartText = "Press 'R' for Restart ";
restart = true;
break;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
//scoreText.guiText.text = "Score: " + score;
//scoreText.guiText.text = "Score:";
}
public void GameOver()
{
gameoverText = "Game Over";
gameOver = true;
}
}
這裏面那個重新開始如下if (Input.GetKeyDown(KeyCode.R))//如果按鍵是‘R’
{
Application.LoadLevel(Application.loadedLevel);//重新載入場景,這裏重載的是本身的場景;
}
要下大雨了,先撤了~~~~~~