using UnityEngine;
using System.Collections;
[AddComponentMenu("MyGame//GameManager")]
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
//得分
public int m_score = 0;
//记录
public static int m_hiscore = 0;
//主角
protected player m_player;
//背景音乐
public AudioClip m_musicClip;
//声音源
protected AudioSource m_Audio;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start()
{
m_Audio = this.audio;
//获取主角
GameObject obj = GameObject.FindGameObjectWithTag("Player");
if (obj != null)
{
m_player = obj.GetComponent<player>();
}
}
// Update is called once per frame
void Update()
{
if (!m_Audio.isPlaying)
{
m_Audio.clip = m_musicClip;
m_Audio.Play();
}
//暂停游戏
if (Time.timeScale > 0 && Input.GetKeyDown (KeyCode.Escape))
{
Time.timeScale = 0;
}
}
void OnGuI()
{
//游戏暂停
if (Time.timeScale == 0)
{
//继续游戏按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 30), "继续游戏"))
{
Time.timeScale = 1;
}
//退出游戏按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 30), "退出游戏"))
{
//退出游戏
Application.Quit();
}
}
int life = 0;
if (m_player != null)
{
//获得主角的生命值
life = (int)m_player.m_life;
}
else // game over
{
//放大字体
GUI.skin.label.fontSize = 50;
//显示游戏失败
GUI.skin.label.alignment = TextAnchor.LowerCenter;
GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, 60), "游戏失败");
GUI.skin.label.fontSize = 20;
//显示按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.5f, 100, 30), "再试一次"))
{
//读取当前关卡
Application.LoadLevel(Application.loadedLevelName);
}
}
GUI.skin.label.fontSize = 15;
GUI.skin.label.alignment = TextAnchor.LowerLeft;
//显示主角生命
GUI.Label(new Rect(5, 5, 100, 30), "装甲" + life);
//显示最高分
GUI.Label(new Rect(0, 5, Screen.width, 90), "记录" + m_hiscore);
//显示当前得分
GUI.Label(new Rect(0, 125, Screen.width, 30), "得分" + m_score);
}
//增加分数
public void AddScore( int point )
{
m_score += point;
//更新高分记录
if (m_hiscore < m_score)
m_hiscore = m_score;
}
}
这段是不显示的,
我在网上找的下面这段却可以显示
using UnityEngine;
using System.Collections;
[AddComponentMenu("MyGame/GameManager")]
public class GameManager : MonoBehaviour {
public static GameManager Instance;
// 得分
public int m_score = 0;
// 历史最高得分
public static int m_hiscore = 0;
// 主角
protected player m_player;
// 背景音乐
public AudioClip m_musicClip;
// 声音源
protected AudioSource m_Audio;
void Awake() {
// 自身实例。单例
Instance = this;
}
// 初始化
void Start () {
m_Audio = this.audio;
// 获取主角
GameObject obj = GameObject.FindGameObjectWithTag("Player");
if (obj != null) {
m_player = obj.GetComponent<player>();
}
}
// 每帧渲染一次
void Update () {
// 循环播放背景音乐
if (!m_Audio.isPlaying) {
m_Audio.clip = m_musicClip; // 音乐文件
m_Audio.Play(); // 播放
}
// 暂停游戏
// Time.timeScale 时间缩放,时间的速度
// 按下ESC键
if (Time.timeScale > 0 && Input.GetKeyDown(KeyCode.Escape)) {
// 时间的速度为0,时间静止
Time.timeScale = 0;
}
}
/** 专用绘制UI界面 **/
void OnGUI() {
// 游戏暂停时显示-------------------------------------
// 游戏暂停 --- 当时间静止
if (Time.timeScale == 0) {
// 在屏幕绘制两个按钮
// 继续游戏按钮
// Buttom 按钮(区域,文字)
// Rect 四方区域(x,y,w,h)
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 30), "继续游戏")) {
// 时间按原速度(正常速度)流失
Time.timeScale = 1;
}
// 退出游戏按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 30), "退出游戏")) {
// 退出游戏
Application.Quit();
}
}
// 以下当玩家创建后显示--------------------------------
int life = 0;
if (m_player != null) {
// 获得主角的生命值
life = (int)m_player.m_life;
}
else { // 游戏结束
GUI.skin.label.fontSize = 50; // 字号
GUI.skin.label.alignment = TextAnchor.LowerCenter; // 对齐
GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, 60), "游戏失败"); // 标签
GUI.skin.label.fontSize = 20; // 字号
// 显示按钮
if (GUI.Button(new Rect(Screen.width*0.5f-50, Screen.height*0.5f, 100, 30), "再试一次")) {
// 读取当前关卡
// Application.LoadLevel 读取下一个关卡,当前关卡的一切对象都被销毁
// Application.loadedLevelName 当前关卡
Application.LoadLevel(Application.loadedLevelName);
}
}
// 以下一直显示---------------------------------------
GUI.skin.label.fontSize = 15; // 字号
GUI.skin.label.alignment = TextAnchor.LowerLeft; // 对齐位置
// 显示主角生命
GUI.Label(new Rect(5, 5, 100, 30), "装甲" + life);
// 显示最高分
GUI.Label(new Rect(0, 5, Screen.width, 90), "记录" + m_hiscore);
// 显示当前得分
GUI.Label(new Rect(0, 125, Screen.width, 30), "得分" + m_score);
}
// 增加分数
// point即敌机控制类Enemy中的属性m_point
public void AddScore( int point ) {
m_score += point;
// 更新高分纪录
if (m_hiscore < m_score)
m_hiscore = m_score;
}
}
我仔细对比过(一条条复制过去对)除了几个空格不一样之外,没发现原因(空格改了也不行),没报错,不论是暂停游戏还是正常游戏界面都不显示ui。求高人告知原因
using System.Collections;
[AddComponentMenu("MyGame//GameManager")]
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
//得分
public int m_score = 0;
//记录
public static int m_hiscore = 0;
//主角
protected player m_player;
//背景音乐
public AudioClip m_musicClip;
//声音源
protected AudioSource m_Audio;
void Awake()
{
Instance = this;
}
// Use this for initialization
void Start()
{
m_Audio = this.audio;
//获取主角
GameObject obj = GameObject.FindGameObjectWithTag("Player");
if (obj != null)
{
m_player = obj.GetComponent<player>();
}
}
// Update is called once per frame
void Update()
{
if (!m_Audio.isPlaying)
{
m_Audio.clip = m_musicClip;
m_Audio.Play();
}
//暂停游戏
if (Time.timeScale > 0 && Input.GetKeyDown (KeyCode.Escape))
{
Time.timeScale = 0;
}
}
void OnGuI()
{
//游戏暂停
if (Time.timeScale == 0)
{
//继续游戏按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 30), "继续游戏"))
{
Time.timeScale = 1;
}
//退出游戏按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 30), "退出游戏"))
{
//退出游戏
Application.Quit();
}
}
int life = 0;
if (m_player != null)
{
//获得主角的生命值
life = (int)m_player.m_life;
}
else // game over
{
//放大字体
GUI.skin.label.fontSize = 50;
//显示游戏失败
GUI.skin.label.alignment = TextAnchor.LowerCenter;
GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, 60), "游戏失败");
GUI.skin.label.fontSize = 20;
//显示按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.5f, 100, 30), "再试一次"))
{
//读取当前关卡
Application.LoadLevel(Application.loadedLevelName);
}
}
GUI.skin.label.fontSize = 15;
GUI.skin.label.alignment = TextAnchor.LowerLeft;
//显示主角生命
GUI.Label(new Rect(5, 5, 100, 30), "装甲" + life);
//显示最高分
GUI.Label(new Rect(0, 5, Screen.width, 90), "记录" + m_hiscore);
//显示当前得分
GUI.Label(new Rect(0, 125, Screen.width, 30), "得分" + m_score);
}
//增加分数
public void AddScore( int point )
{
m_score += point;
//更新高分记录
if (m_hiscore < m_score)
m_hiscore = m_score;
}
}
这段是不显示的,
我在网上找的下面这段却可以显示
using UnityEngine;
using System.Collections;
[AddComponentMenu("MyGame/GameManager")]
public class GameManager : MonoBehaviour {
public static GameManager Instance;
// 得分
public int m_score = 0;
// 历史最高得分
public static int m_hiscore = 0;
// 主角
protected player m_player;
// 背景音乐
public AudioClip m_musicClip;
// 声音源
protected AudioSource m_Audio;
void Awake() {
// 自身实例。单例
Instance = this;
}
// 初始化
void Start () {
m_Audio = this.audio;
// 获取主角
GameObject obj = GameObject.FindGameObjectWithTag("Player");
if (obj != null) {
m_player = obj.GetComponent<player>();
}
}
// 每帧渲染一次
void Update () {
// 循环播放背景音乐
if (!m_Audio.isPlaying) {
m_Audio.clip = m_musicClip; // 音乐文件
m_Audio.Play(); // 播放
}
// 暂停游戏
// Time.timeScale 时间缩放,时间的速度
// 按下ESC键
if (Time.timeScale > 0 && Input.GetKeyDown(KeyCode.Escape)) {
// 时间的速度为0,时间静止
Time.timeScale = 0;
}
}
/** 专用绘制UI界面 **/
void OnGUI() {
// 游戏暂停时显示-------------------------------------
// 游戏暂停 --- 当时间静止
if (Time.timeScale == 0) {
// 在屏幕绘制两个按钮
// 继续游戏按钮
// Buttom 按钮(区域,文字)
// Rect 四方区域(x,y,w,h)
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.4f, 100, 30), "继续游戏")) {
// 时间按原速度(正常速度)流失
Time.timeScale = 1;
}
// 退出游戏按钮
if (GUI.Button(new Rect(Screen.width * 0.5f - 50, Screen.height * 0.6f, 100, 30), "退出游戏")) {
// 退出游戏
Application.Quit();
}
}
// 以下当玩家创建后显示--------------------------------
int life = 0;
if (m_player != null) {
// 获得主角的生命值
life = (int)m_player.m_life;
}
else { // 游戏结束
GUI.skin.label.fontSize = 50; // 字号
GUI.skin.label.alignment = TextAnchor.LowerCenter; // 对齐
GUI.Label(new Rect(0, Screen.height * 0.2f, Screen.width, 60), "游戏失败"); // 标签
GUI.skin.label.fontSize = 20; // 字号
// 显示按钮
if (GUI.Button(new Rect(Screen.width*0.5f-50, Screen.height*0.5f, 100, 30), "再试一次")) {
// 读取当前关卡
// Application.LoadLevel 读取下一个关卡,当前关卡的一切对象都被销毁
// Application.loadedLevelName 当前关卡
Application.LoadLevel(Application.loadedLevelName);
}
}
// 以下一直显示---------------------------------------
GUI.skin.label.fontSize = 15; // 字号
GUI.skin.label.alignment = TextAnchor.LowerLeft; // 对齐位置
// 显示主角生命
GUI.Label(new Rect(5, 5, 100, 30), "装甲" + life);
// 显示最高分
GUI.Label(new Rect(0, 5, Screen.width, 90), "记录" + m_hiscore);
// 显示当前得分
GUI.Label(new Rect(0, 125, Screen.width, 30), "得分" + m_score);
}
// 增加分数
// point即敌机控制类Enemy中的属性m_point
public void AddScore( int point ) {
m_score += point;
// 更新高分纪录
if (m_hiscore < m_score)
m_hiscore = m_score;
}
}
我仔细对比过(一条条复制过去对)除了几个空格不一样之外,没发现原因(空格改了也不行),没报错,不论是暂停游戏还是正常游戏界面都不显示ui。求高人告知原因