一、新增一个MainMenuController.cs,管理MainMenu场景的GNUI交互
using UnityEngine;
using System.Collections;
public class MainMenuController : MonoBehaviour {
//Tween
public TweenPosition welcomeWidget;
public TweenPosition settingsWidget;
public TweenPosition resetScoresWidget;
//Save the value of level
private int curentGameLevle;
//Judged the toggle
private bool toggleValue;
//save the time of game when is starting
private float timeCount;
void Awake()
{
MainSeceneManager.gameLevel = 5;
MainSeceneManager.openSound = true;
}
///*************************************
///click the button of resetScores
///*************************************
//ensure whether reset game scores
public void ClickYes()
{
resetScoresWidget.PlayReverse();
settingsWidget.PlayForward();
PlayerPrefs.SetFloat("score",0);
}
public void ClickNo()
{
resetScoresWidget.PlayReverse();
settingsWidget.PlayForward();
}
//****************************************
//Click the button of WelcomeWidget
//****************************************
public void ClickPlayButton()
{
Application.LoadLevel("StartGame");
}
public void ClickOptionsButton()
{
welcomeWidget.PlayForward();
settingsWidget.PlayForward();
}
public void ClickExitButton()
{
Application.Quit();
}
///**************************************
/// click the button of optionWeidget
/// *************************************
//Click the resetScoresButton
public void ChangeGameLevel()
{
switch (UIPopupList.current.value.Trim())
{
case "Easy":
curentGameLevle = 5;
break;
case "Simple":
curentGameLevle =7;
break;
case "Hard":
curentGameLevle=9;
break;
}
}
public void ClickResetScoresButton()
{
resetScoresWidget.PlayForward();
settingsWidget.PlayReverse();
}
public void ClickBackButton()
{
welcomeWidget.PlayReverse();
settingsWidget.PlayReverse();
}
public void ClickSaveAndBackButton()
{
MainSeceneManager.gameLevel = curentGameLevle;
MainSeceneManager.openSound = toggleValue;
welcomeWidget.PlayReverse();
settingsWidget.PlayReverse();
}
public void ClickEasyButton() //GameLevel Button and Save the value of level
{
curentGameLevle = 5;
}
public void ClickSimpleButton()
{
curentGameLevle = 7;
}
public void ClickHardButton()
{
curentGameLevle = 9;
}
public void ClickSoundButton() //Control sound whether closed
{
toggleValue = UIToggle.current.value;
}
}
二、删除结束场景,将ui放在StartScene场景中,优化脚本
新增GameOverMenu.cs
using UnityEngine;
using System.Collections;
public class GameOverMenu : MonoBehaviour {
//get the object 's UILabel of component
public UILabel currentScores;
public UILabel bestScores;
//get the game over menu
public Object gameOverMenu;
private float currentScore;
//void Awake(){
// JudgeTheGameOverMenu();
// //display the current score and best score by Label
// UpdateTheScores(currentScore);
// //print("GameOverMenu_currentScore:"+currentScore);
//}
//private void JudgeTheGameOverMenu()
//{
// if (gameOverMenu)
// {
// //save current scores
// currentScore = RemenberScores.currentScore;
// }
//}
public void StartGame()
{
Application.LoadLevel("StartGame");
}
public void LoadMainMenu()
{
Application.LoadLevel("MainScene");
}
public void Exit()
{
Application.Quit();
}
public void UpdateTheScores(float currentScore)
{
float bestScore = PlayerPrefs.GetFloat("score", 0);
if (currentScore > bestScore)
{
bestScore = currentScore;
GameObject.Find("Title").GetComponent<UILabel>().text = "Good Score!";
}
else if(currentScore<bestScore)
{
GameObject.Find("Title").GetComponent<UILabel>().text = "No Good!";
}
else
{
GameObject.Find("Title").GetComponent<UILabel>().text = "Just so so!";
}
PlayerPrefs.SetFloat("score",bestScore);
currentScores.text = currentScore + "";
bestScores.text = bestScore + "";
}
}
修改GameManager.cs
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
/// <summary>
/// the class used to control the Game
/// </summary>
public static GameManager gameInstance;
//the Game State
public enum GameState { GAMESTART = 0, GAMEISPLAYING = 1, GAMEOVER = 2 };
public int currentGameState;
//save the collide count
public int collideCount=0;
//choose a background to transform position
public Transform firstBackGround;
//Save the current scores
public int currentScores=0;
//achieve the game object
public GameObject scoreTitle;
private int delayTime = 2;
//receive bird and GameOverMenu
private GameObject bird;
private GameObject gameOverMenu;
//private AudioSource startSound;
void Awake()
{
gameInstance = this;
scoreTitle.SetActive(false);
currentGameState = (int)GameState.GAMESTART;
bird = GameObject.FindGameObjectWithTag("Player");
gameOverMenu = GameObject.Find("GameOverMenu");
PlayTheStartSound();
}
void Update()
{
if(currentGameState ==(int)GameState.GAMESTART)
{
gameOverMenu.SetActive(false);
if(Input.GetMouseButtonDown(0))
{
scoreTitle.SetActive(true);
GameObject.Find("FlappyBird").SetActive(false);
currentGameState = (int)GameState.GAMEISPLAYING;
bird.SendMessage("GetTheGameBegin");
}
}
if(currentGameState==(int)GameState.GAMEOVER)
{
RemenberScores.currentScore = currentScores;
StartCoroutine("DisplayScores");
}
}
IEnumerator DisplayScores()
{
yield return new WaitForSeconds(delayTime);
gameOverMenu.SetActive(true);
gameOverMenu.SendMessage("UpdateTheScores", currentScores);
}
private void PlayTheStartSound()
{
if (currentGameState == (int)GameState.GAMESTART)
{
if (MainSeceneManager.openSound)
{
audio.Play();
}
}
}
}
新增MainSceneManager.cs脚本
using UnityEngine;
using System.Collections;
public class MainSeceneManager {
//Save the game level
public static int gameLevel;
//Judged whether open the sound
public static bool openSound;
}
修改ColliderFloor.cs脚本,其他脚本作类似修改
using UnityEngine;
using System.Collections;
public class ColliderFloor : MonoBehaviour {
void OnCollisionEnter(Collision gameObject)
{
if (gameObject.gameObject.tag == "Player")
{
GameManager.gameInstance.collideCount++;
if (MainSeceneManager.openSound && GameManager.gameInstance.collideCount<=1)
{
audio.Play();
}
GameManager.gameInstance.currentGameState = (int)GameManager.GameState.GAMEOVER;
}
}
}