Unity (四)

来源:互联网 发布:淘宝的海外直购在哪 编辑:程序博客网 时间:2024/06/06 05:03

声明,最近这几篇博客都是我的笔记,所以没仔细解释,请原谅~~~

今天终于把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);//重新载入场景,这里重载的是本身的场景;

}

要下大雨了,先撤了~~~~~~


0 0
原创粉丝点击