飞机大战之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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值