public class Main : MonoBehaviour
{
Text title;
Button startButton;
Button quitButton;
private void Start()
{
GameObject uicanvas = GameObject.Find("Canvas");
foreach (Transform t in uicanvas.transform.GetComponentsInChildren())
{
if (t.name.CompareTo("title") == 0)
{
title = t.GetComponent();
}
else if (t.name.CompareTo("StartButton") == 0)
{
startButton = t.GetComponent
进度条:
public class ProgressBar : MonoBehaviour
{
int level;
Text sliderText;
Image bg;
public Slider _progress;
//过渡场景图片名称
private static string LoadSceneBg= "LoadSceneBg";
private static int loadSceneBgNum = 1;
void Awake()
{
_progress = GetComponent();
level = GameManager.level;
GameObject uicanvas = GameObject.Find("Canvas");
foreach (Transform t in uicanvas.transform.GetComponentsInChildren())
{
if (t.name.CompareTo("sliderText") == 0)
{
sliderText = t.GetComponent();
}
else if(t.name.CompareTo("Bg") == 0)
{
bg= t.GetComponent();
string path = LoadSceneBg + loadSceneBgNum;
bg.overrideSprite = Resources.Load(path, typeof(Sprite)) as Sprite;
loadSceneBgNum++;
}
}
}
//使用协程
void Start()
{
StartCoroutine(LoadScene());
}
private void Update()
{
//显示进度条加载数值
sliderText.text = _progress.value*100 + "/100";
}
IEnumerator LoadScene()
{
//用Slider 展示的数值
int disableProgress = 0;
int toProgress = 0;
//异步场景切换
AsyncOperation op = SceneManager.LoadSceneAsync(level);
//关卡数加一
GameManager.level++;
//不允许有场景切换功能
op.allowSceneActivation = false;
//op.progress 只能获取到90%,最后10%获取不到,需要自己处理
while (op.progress < 0.9f)
{
//获取真实的加载进度
toProgress = (int)(op.progress * 100);
while (disableProgress < toProgress)
{
++disableProgress;
_progress.value = disableProgress / 100.0f;//0.01开始
yield return new WaitForEndOfFrame();
}
}
//因为op.progress 只能获取到90%,所以后面的值不是实际的场景加载值了
toProgress = 100;
while (disableProgress < toProgress)
{
++disableProgress;
_progress.value = disableProgress / 100.0f;
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
}
游戏主界面
核心代码:
[AddComponentMenu("Game/GameManager")]
public class GameManager : MonoBehaviour
{
public static int level = 2;
private static string transitionToLevel= "TransitionToLevel";
public static GameManager Instance = null;
public int m_score = 0;
public static int m_hiscore = 0;
public int m_ammo = 100;
Player m_player;
EnemySpawn m_enemySpawn;
Text txt_ammo;
Text txt_hiscore;
Text txt_life;
Text txt_score;
Text txt_gameOver;
Text txt_gameVictory;
Button button_restart;
Button button_nextGame;
Button button_quitGame;
Image hud;
private void OnEnable()
{
Time.timeScale = 1;
}
private void Awake()
{
Instance = this;
m_player = GameObject.FindGameObjectWithTag("Player").GetComponent();
m_enemySpawn = GameObject.FindGameObjectWithTag("EnemySpawn").GetComponent();
GameObject uicanvas = GameObject.Find("Canvas");
foreach (Transform t in uicanvas.transform.GetComponentsInChildren())
{
if (t.name.CompareTo("txt_ammo") == 0)
{
txt_ammo = t.GetComponent();
}
else if (t.name.CompareTo("txt_hiscore") == 0)
{
txt_hiscore = t.GetComponent();
txt_hiscore.text = m_hiscore.ToString();
}
else if (t.name.CompareTo("txt_life") == 0)
{
txt_life = t.GetComponent();
}
else if (t.name.CompareTo("txt_score") == 0)
{
txt_score = t.GetComponent();
}
else if (t.name.CompareTo("txt_gameOver") == 0)
{
txt_gameOver = t.GetComponent();
txt_gameOver.gameObject.SetActive(false);
}
else if (t.name.CompareTo("txt_gameVictory") == 0)
{
txt_gameVictory = t.GetComponent();
txt_gameVictory.gameObject.SetActive(false);
}
else if (t.name.CompareTo("hud") == 0)
{
hud = t.GetComponent();
}
else if (t.name.CompareTo("Restart Button") == 0)
{
button_restart = t.GetComponent();
button_restart.onClick.AddListener(delegate ()//按钮事件
{
//Time.timeScale = 1;
//读取关卡
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
});
//开始游戏按钮不可见
button_restart.gameObject.SetActive(false);
}
else if (t.name.CompareTo("Next Button") == 0)
{
button_nextGame = t.GetComponent();
button_nextGame.onClick.AddListener(delegate ()//按钮事件
{
//Time.timeScale = 1;
SceneManager.LoadScene(transitionToLevel);
//level++;
});
//下一关游戏按钮不可见
button_nextGame.gameObject.SetActive(false);
}
else if (t.name.CompareTo("Quit Button") == 0)
{
button_quitGame = t.GetComponent();
button_quitGame.onClick.AddListener(delegate ()//按钮事件
{
//Time.timeScale = 1;
Application.Quit();
});
//退出游戏按钮不可见
button_quitGame.gameObject.SetActive(false);
}
}
}
//更新分数
public void SetScore(int score)
{
m_score += score;
if(m_score>0)
{
m_hiscore += m_score;
}
txt_score.text = m_score.ToString();
txt_hiscore.text = m_hiscore.ToString();
}
//更新弹药
public void SetAmmo(int ammo)
{
m_ammo -= ammo;
if(m_ammo<=0)
{
m_ammo = 100 - m_ammo;
}
txt_ammo.text = m_ammo.ToString() + "/100";
}
//更新生命
public void SetLife(int life)
{
txt_life.text = life.ToString();
}
//游戏失败
public void GameOver()
{
if(m_player.m_life<=0)
{
hud.gameObject.SetActive(false);
txt_gameOver.gameObject.SetActive(true);
button_restart.gameObject.SetActive(true);
button_quitGame.gameObject.SetActive(true);
m_enemySpawn.enabled = false;
m_player.enabled = false;
Time.timeScale = 0;
Cursor.visible = true;
}
}
//游戏胜利
public void GameVictory()
{
if (m_player.m_life > 0 && m_score>=100)
{
hud.gameObject.SetActive(false);
txt_gameVictory.gameObject.SetActive(true);
button_nextGame.gameObject.SetActive(true);
button_quitGame.gameObject.SetActive(true);
m_enemySpawn.enabled = false;
m_player.enabled = false;
Time.timeScale = 0;
Cursor.visible = true;
}
}
private void Update()
{
GameOver();
GameVictory();
}
}
This article is from an interview with Zuhaib Siddique, a production engineer at HipChat, makers of group chat and IM for teams.
HipChat started in an unusual space, one you might not