飛機大戰之GUI設置

unity學習,希望我的博客能給喜歡unity的朋友帶來幫助


前幾天我們講了飛機大戰,但是界面設置卻不完整,缺少文字提示等內容,接下來我們就用GUI來實現文字設置。

當我方飛機被攻擊銷燬後,會出現遊戲結束的字樣,並停止遊戲,會出現重新開始的按鈕,點擊按鈕遊戲會重新開始,還可以完成按下鍵盤esc鍵遊戲暫停,點擊鼠標左鍵遊戲接續的功能。

遊戲界面如圖所示:


代碼如下:

  1. using UnityEngine;
  2. using System.Collections;

  3. public class GUIText : MonoBehaviour {

  4.     public int lifeTime = 0;//我方生命值
  5.     public int score = 0;//得分
  6.     public static GUIText instance;//管理對象初始化,自身定爲靜態變量
  7.     public Player player;//獲取我方飛機
  8.     public bool flag = false;//標誌位,標誌遊戲的開始

  9.     void Start()
  10.     {
  11.         instance = this;
  12.         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
  13.     }

  14.     void Update()
  15.     {
  16.         if (flag)
  17.         {
  18.             Time.timeScale = 0;//遊戲結束
  19.         }
  20.         else
  21.         {
  22.             //暫停遊戲
  23.             if (Input.GetKey(KeyCode.Escape))
  24.             {
  25.                 Time.timeScale = 0;
  26.             }
  27.             //開始遊戲
  28.             if (Input.GetButton("Fire"))
  29.             {
  30.                 Time.timeScale = 1;
  31.             }
  32.         }
  33.     }

  34.     void OnGUI()
  35.     {
  36.         
  37.         lifeTime = player.i;
  38.         GUI.skin.label.fontSize = 20;
  39.         GUI.Label(new Rect(20, 30, 150, 50), "生命值爲:" + lifeTime);
  40.         GUI.Label(new Rect(20, 70, 150, 50), "分數:" + score);

  41.         if (lifeTime == 0) 
  42.         {
  43.             flag = true;//遊戲的結束
  44.             GUI.skin.label.fontSize = 40;//設置字體的大小
  45.             GUI.color = Color.red;//設置字體的顏色
  46.             GUI.Label(new Rect(350, 100, 200, 200), "遊戲結束");

  47.             GUI.skin.button.fontSize = 20;
  48.             GUI.color = Color.white;
  49.             if (GUI.Button(new Rect(350, 300, 100, 100), "重新開始"))
  50.             {
  51.                 //Application.LoadLevel("GUI");
  52.                 Application.LoadLevel(Application.loadedLevelName);
  53.             }
  54.         }

  55.     }

  56. }

更多精彩請點擊 http://www.gopedu.com/article



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章