游戏管理器(1)

游戏管理器(1)

现在的游戏中还缺少显示游戏信息的UI和游戏失败的状态提示,我们将创建一个游戏管理器来处理这些东西。

1)创建GameManager.cs角本:

 

 
 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.  
  4. [AddComponentMenu("MyGame/GameManager")]  
  5. public class GameManager MonoBehaviour {  
  6.  
  7.     public static GameManager Instance;  
  8.  
  9.     //得分  
  10.     public int m_score 0;  
  11.  
  12.     //纪录  
  13.     public static int m_hiscore 0;  
  14.  
  15.     //主角  
  16.     protected Player m_player;  
  17.  
  18.     // 背景音乐  
  19.     public AudioClip m_musicClip;  
  20.  
  21.     // 声音源  
  22.     protected AudioSource m_Audio;  
  23.  
  24.     void Awake()  
  25.     {  
  26.         Instance this;  
  27.     }  
  28.  
  29.     // Use this for initialization  
  30.     void Start () {  
  31.  
  32.         m_Audio this.audio;  
  33.  
  34.         // 获取主角  
  35.         GameObject obj GameObject.FindGameObjectWithTag("Player");  
  36.         if (obj != null)  
  37.         {  
  38.             m_player obj.GetComponent<</span>Player>();  
  39.         }  
  40.       
  41.     }  
  42.       
  43.     // Update is called once per frame  
  44.     void Update () {  
  45.  
  46.         // 循环播放背景音乐  
  47.         if (!m_Audio.isPlaying)  
  48.         {  
  49.             m_Audio.clip m_musicClip;  
  50.             m_Audio.Play();  
  51.              
  52.         }  
  53.  
  54.         // 暂停游戏  
  55.         if (Time.timeScale > && Input.GetKeyDown(KeyCode.Escape))  
  56.         {  
  57.             Time.timeScale 0;  
  58.         }  
  59.     }  
  60.  
  61.     void OnGUI()  
  62.     {  
  63.         // 游戏暂停  
  64.         if (Time.timeScale == 0)  
  65.         {  
  66.             // 继续游戏按钮  
  67.           if (GUI.Button(new Rect(Screen.width 0.5f 50, Screen.height 0.4f, 100, 30), "继续游戏"))  
  68.             {  
  69.                 Time.timeScale 1;  
  70.             }  
  71.  
  72.             // 退出游戏按钮  
  73.           if (GUI.Button(new Rect(Screen.width 0.5f 50, Screen.height 0.6f, 100, 30), "退出游戏"))  
  74.             {  
  75.                 // 退出游戏  
  76.                 Application.Quit();  
  77.             }  
  78.         }  
  79.           
  80.         int life 0;  
  81.         if (m_player != null)  
  82.         {  
  83.             // 获得主角的生命值  
  84.             life (int)m_player.m_life;  
  85.         }  
  86.         else // game over  
  87.         {  
  88.               
  89.             // 放大字体  
  90.             GUI.skin.label.fontSize 50;  
  91.  
  92.             // 显示游戏失败  
  93.             GUI.skin.label.alignment TextAnchor.LowerCenter;  
  94.             GUI.Label(new Rect(0, Screen.height 0.2f, Screen.width, 60), "游戏失败");  
  95.  
  96.             GUI.skin.label.fontSize 20;  
  97.  
  98.             // 显示按钮  
  99.           if (GUI.Button(new Rect(Screen.width 0.5f 50, Screen.height 0.5f, 100, 30), "再试一次"))  
  100.             {  
  101.                 // 读取当前关卡  
  102.                 Application.LoadLevel(Application.loadedLevelName);  
  103.             }  
  104.         }  
  105.  
  106.         GUI.skin.label.fontSize 15;  
  107.  
  108.         // 显示主角生命  
  109.         GUI.Label(new Rect(5, 5, 100, 30), "装甲 life);  
  110.  
  111.         // 显示最高分  
  112.         GUI.skin.label.alignment TextAnchor.LowerCenter;  
  113.         GUI.Label(new Rect(0, 5, Screen.width, 30), "纪录 m_hiscore);  
  114.  
  115.         // 显示当前得分  
  116.         GUI.Label(new Rect(0, 25, Screen.width, 30), "得分 m_score);  
  117.         
  118.     }  
  119.  
  120.     // 增加分数  
  121.     public void AddScore( int point )  
  122.     {  
  123.         m_score += point;  
  124.  
  125.         // 更新高分纪录  
  126.         if (m_hiscore <</span> m_score)  
  127.             m_hiscore m_score;  
  128.     }  
  129. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值