Unity3D【GUI基础二】

Application


1Application.LoadLevel("Login");//跳转到登录视图

2Application.LoadLevel(Application.loadedLevelName); //跳转到本视图(游戏重新开始)

3Application.Quit();//退出游戏


游戏暂停和开始


1、游戏暂停:Time.timeScale=0

2、游戏开始:Time.timeScale=1


例如:


  1. using UnityEngine;
  2. using System.Collections;
  3. public class MyGUI : MonoBehaviour {
  4.     public static float playerLifeTime=0;  //生命值
  5.     float score=0;//分数
  6.     public static MyGUI myGUIInstance;
  7.     private Player player;//我机对象
  8.     bool flag = false;
  9.     void OnGUI() {
  10.         GUI.skin.label.fontSize = 20;
  11.         GUI.Label(new Rect(100, 100, 150, 30), "生命值:" + playerLifeTime);
  12.         GUI.Label(new Rect(100, 130, 150, 30), "分   数:" + score);
  13.         if(playerLifeTime<=0){
  14.             playerLifeTime = 0;//给我机生命值重新赋值为零,避免出现负数
  15.             GUI.skin.label.fontSize = 45;
  16.             GUI.color = Color.red;
  17.             GUI.Label(new Rect(600, 200, 200, 80), "游戏结束");
  18.             GUI.skin.label.fontSize = 35;
  19.             GUI.color = Color.white;//颜色
  20.             if (GUI.Button(new Rect(610, 300, 150, 30), "重新开始")) {
  21.                 flag = true;
  22.                 Application.LoadLevel("airFight");//跳转到airFight视图
  23.             }
  24.             if (GUI.Button(new Rect(610, 350, 150, 30), "游戏退出")) {
  25.                 Application.Quit();//游戏退出
  26.             }
  27.             
  28.         }
  29.     }
  30.         void Start () {
  31.         myGUIInstance = this;
  32.         player = GameObject.FindWithTag("Player").GetComponent();//根据标签名获取我机对象下脚本Player
  33.         }        
  34.         void Update () {
  35.        
  36.         if (flag) {
  37.             Time.timeScale = 0;  //游戏暂停        
  38.         }else {
  39.             Time.timeScale = 1; //游戏继续
  40.          playerLifeTime = player.liftTime;//我机生命值
  41.         score = Enemy.enemyliftTime +Enemy2.enemy2liftTime;//分数累加
  42.         }
  43.         }
  44. }


更多精彩请点击 http://www.gopedu.com/

你可能感兴趣的:(Unity3D,unity,unity3d)