From e18da80d645cceacf3d086ff2974dc7c66aac57a Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Fri, 28 Oct 2016 11:52:58 -0400 Subject: [PATCH] Back button: Implement reactions on Scenes --- Assets/Scripts/GameOver.cs | 2 ++ Assets/Scripts/HighScores.cs | 2 ++ Assets/Scripts/Menu.cs | 3 ++- Assets/Scripts/UIController.cs | 2 ++ Assets/Scripts/Utils/BackButtonHelper.cs | 25 +++++++++++++++++++++++++ Assets/Scripts/Utils/BackButtonHelper.cs.meta | 12 ++++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Utils/BackButtonHelper.cs create mode 100644 Assets/Scripts/Utils/BackButtonHelper.cs.meta diff --git a/Assets/Scripts/GameOver.cs b/Assets/Scripts/GameOver.cs index 96e6bff..9e6ac4f 100644 --- a/Assets/Scripts/GameOver.cs +++ b/Assets/Scripts/GameOver.cs @@ -4,6 +4,7 @@ using System.Collections; using UnityEngine.SceneManagement; using Assets.Scripts; using Assets.Scripts.API; +using Assets.Scripts.Utils; public class GameOver : MonoBehaviour { @@ -28,6 +29,7 @@ public class GameOver : MonoBehaviour // SceneManager.LoadScene("Menu"); score = PlayerPrefs.GetInt("lastscore"); Score.text = "You scored " + score; + BackButtonHelper.playOnBack(); } public void OnButtonPlayClicked() diff --git a/Assets/Scripts/HighScores.cs b/Assets/Scripts/HighScores.cs index bbea898..c996e3d 100644 --- a/Assets/Scripts/HighScores.cs +++ b/Assets/Scripts/HighScores.cs @@ -1,4 +1,5 @@ using Assets.Scripts.API; +using Assets.Scripts.Utils; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; @@ -21,6 +22,7 @@ public class HighScores : MonoBehaviour { // Update is called once per frame void Update () { + BackButtonHelper.playOnBack(); if (!updatedScores) { if (Server.Scores.status.Equals("ok")) { Score[] highScores = Server.Scores.scores; diff --git a/Assets/Scripts/Menu.cs b/Assets/Scripts/Menu.cs index fe479bf..80218c4 100644 --- a/Assets/Scripts/Menu.cs +++ b/Assets/Scripts/Menu.cs @@ -1,6 +1,7 @@ using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; +using Assets.Scripts.Utils; public class Menu : MonoBehaviour { @@ -11,7 +12,7 @@ public class Menu : MonoBehaviour { // Update is called once per frame void Update () { - + BackButtonHelper.quitOnBack(); } public void OnButtonPlayClicked() diff --git a/Assets/Scripts/UIController.cs b/Assets/Scripts/UIController.cs index 1e00890..d19b5e1 100644 --- a/Assets/Scripts/UIController.cs +++ b/Assets/Scripts/UIController.cs @@ -3,6 +3,7 @@ using UnityEngine; using System.Collections; using System.Collections.Generic; using Assets.Scripts; +using Assets.Scripts.Utils; using UnityEngine.UI; public class UIController : MonoBehaviour @@ -37,6 +38,7 @@ public class UIController : MonoBehaviour DisplayBattery(); HandlePowerUps(); HandleScore(); + BackButtonHelper.menuOnBack(); } private void HandleScore() diff --git a/Assets/Scripts/Utils/BackButtonHelper.cs b/Assets/Scripts/Utils/BackButtonHelper.cs new file mode 100644 index 0000000..3df7787 --- /dev/null +++ b/Assets/Scripts/Utils/BackButtonHelper.cs @@ -0,0 +1,25 @@ +using System; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace Assets.Scripts.Utils +{ + public static class BackButtonHelper + { + public static void quitOnBack() { + if (Input.GetKeyDown(KeyCode.Escape)) + Application.Quit(); + } + + public static void menuOnBack() { + if (Input.GetKeyDown(KeyCode.Escape)) + SceneManager.LoadScene("Menu"); + } + + public static void playOnBack() { + if (Input.GetKeyDown(KeyCode.Escape)) + SceneManager.LoadScene("Main"); + } + } +} + diff --git a/Assets/Scripts/Utils/BackButtonHelper.cs.meta b/Assets/Scripts/Utils/BackButtonHelper.cs.meta new file mode 100644 index 0000000..89e1b6f --- /dev/null +++ b/Assets/Scripts/Utils/BackButtonHelper.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 3be153ffde4554d9cbb676f03dc3005d +timeCreated: 1477660884 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- libgit2 0.27.0