Это два скрипта, которые используют time.timeScale. Один использует его, чтобы остановить игру, когда у нас заканчивается время, а другой использует его, чтобы приостановить игру, когда нажимается escape. GameControlScript отлично работает, кроме Time.timeScale. Gameover GUI показывает, но игра все еще продолжается за GUI.
//This script is for game mechanics, this script controls point system, gameover, score, total time in the game
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameControlScript : MonoBehaviour {
// public GUISkin skin;
float timeRemaining = 10; //time left at start of game
float timeExtension = 3f; //time added after powerup is collected
float timeDeduction = 2f; //time deducted after obsticle is hit
float totalTimeElapsed = 0; //time you last in game
float score=0f; //player's score
public bool isGameOver = false; //flag to check if game is over
public AudioSource GameOverSound; //sound play at the end game
public Text ScoreBoard;
public Text FinalScore;
public Text GameTimeLeft;
public GameObject GameOverPanel;
void Start(){
Time.timeScale = 1; // set the time scale to 1, to start the game world. This is needed if you restart the game from the game over menu
}
void Update () {
if(isGameOver) //check if game is over
return; //if game is not over don't do anything (it sends control to Update and doesn't let it excute code down there)
totalTimeElapsed += Time.deltaTime; //adding amount of time passed in totalTimeElapsed variable
score = totalTimeElapsed * 100; //multipling totalTimeElapsed with 100 to create score of player
timeRemaining -= Time.deltaTime; //subtracting passing time from timeRemaining
GameTimeLeft.text = "TIME LEFT: " + ((int)timeRemaining).ToString ();
ScoreBoard.text = "SCORE: " + ((int)score).ToString ();
if (timeRemaining <= 0) { //check if timeRemaining is less or equal to zero
Time.timeScale = 0;
GameOverPanel.SetActive (true);
FinalScore.text = "YOUR SCORE: " + ((int)score).ToString ();
isGameOver = true; //if it is less or equal to zero game is over
GameOverSound.Play (); //play gameover sound
}//end if
}//end Update
Это сценарий паузы:
//This Script pause the game when escape is pressed
using UnityEngine;
using System.Collections;
public class PauseMenuScript : MonoBehaviour
{
public GUISkin myskin; //custom GUIskin reference
public string levelToLoad;// name of level to load
public bool paused = false; //flag to check if the game is paused
public AudioSource MenuClickSound;
private void Start()
{
Time.timeScale=1; //Set the timeScale back to 1 for Restart option to work
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape) ) //check if Escape key/Back key is pressed
{
if (paused)
paused = false; //unpause the game if already
else
paused = true; //pause the game if not paused
}
if(paused)
Time.timeScale = 0; //set the timeScale to 0 so that all the procedings are halte
else
Time.timeScale = 1; //set it back to 1 on unpausing the game
}
private void OnGUI()
{
GUI.skin=myskin; //use the custom GUISkin
if (paused){
//Shows text PAUSED when game is paused
GUI.Box(new Rect(Screen.width/4, Screen.height/4, Screen.width/2, Screen.height/2), "PAUSED");
//Draws button Resume and resume game when it is pressed
if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESUME")){
MenuClickSound.Play ();
paused = false;
}
//Draws button Restart and restart game when it is pressed
if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+2*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESTART")){
MenuClickSound.Play ();
Application.LoadLevel(Application.loadedLevel); // load already loaded level
}
//Draws button Main Menu and takes user to main menu when it is pressed
if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+3*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "MAIN MENU")){
MenuClickSound.Play ();
Application.LoadLevel(levelToLoad); // name of the scene which has to be load in order to go to main menu
}
}
}
}