unity學習,希望我的博客能給喜歡unity的朋友帶來幫助
代碼如下:
- using UnityEngine;
- using System.Collections;
- public class GUIText : MonoBehaviour {
- public int lifeTime = 0;//我方生命值
- public int score = 0;//得分
- public static GUIText instance;//管理對象初始化,自身定爲靜態變量
- public Player player;//獲取我方飛機
- public bool flag = false;//標誌位,標誌遊戲的開始
- void Start()
- {
- instance = this;
- player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
- }
- void Update()
- {
- if (flag)
- {
- Time.timeScale = 0;//遊戲結束
- }
- else
- {
- //暫停遊戲
- if (Input.GetKey(KeyCode.Escape))
- {
- Time.timeScale = 0;
- }
- //開始遊戲
- if (Input.GetButton("Fire"))
- {
- Time.timeScale = 1;
- }
- }
- }
- void OnGUI()
- {
-
- lifeTime = player.i;
- GUI.skin.label.fontSize = 20;
- GUI.Label(new Rect(20, 30, 150, 50), "生命值爲:" + lifeTime);
- GUI.Label(new Rect(20, 70, 150, 50), "分數:" + score);
- if (lifeTime == 0)
- {
- flag = true;//遊戲的結束
- GUI.skin.label.fontSize = 40;//設置字體的大小
- GUI.color = Color.red;//設置字體的顏色
- GUI.Label(new Rect(350, 100, 200, 200), "遊戲結束");
- GUI.skin.button.fontSize = 20;
- GUI.color = Color.white;
- if (GUI.Button(new Rect(350, 300, 100, 100), "重新開始"))
- {
- //Application.LoadLevel("GUI");
- Application.LoadLevel(Application.loadedLevelName);
- }
- }
- }
- }
更多精彩請點擊 http://www.gopedu.com/article