using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameController : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary;
public static GameController instance; public bool isMusicOn; public bool isGameStartedFirstTime; public bool[] levels; public int[] highscore; public int score; public int currentLevel; private GameData data;
void Awake(){ CreateInstance (); } // Use this for initialization void Start () { InitializeGameVariables (); } void CreateInstance(){ if (instance != null) { Destroy (gameObject); } else { instance = this; DontDestroyOnLoad (gameObject); } } void InitializeGameVariables(){ Load (); if (data != null) { isGameStartedFirstTime = data.GetIsGameStartedFirstTime (); } else { isGameStartedFirstTime = true; } if (isGameStartedFirstTime) { isGameStartedFirstTime = false; isMusicOn = true; levels [0] = true; for (int i = 1; i < levels.Length; i++) { levels [i] = false; } for (int i = 0; i < highscore.Length; i++) { highscore [i] = 0; } data.SetIsMusicOn (isMusicOn); data.SetIsGameStartedFirstTime (isGameStartedFirstTime); data.SetHighScore (highscore); data.SetLevels (levels); Save (); Load (); } else { isGameStartedFirstTime = data.GetIsGameStartedFirstTime (); isMusicOn = data.GetIsMusicOn (); highscore = data.GetHighScore (); levels = data.GetLevels (); } } public void Save(){ FileStream file = null; try{ file = File.Create(Application.persistentDataPath + "/data.dat"); if(data != null){ data.SetIsMusicOn(isMusicOn); data.SetIsGameStartedFirstTime(isGameStartedFirstTime); data.SetHighScore(highscore); data.SetLevels(levels); bf.Serialize(file, data); } }catch(Exception e){ Debug.LogException (e, this); }finally{ if(file != null){ file.Close (); } } } public void Load(){ FileStream file = null; try{ file = File.Open(Application.persistentDataPath + "/data.dat", FileMode.Open); data = bf.Deserialize(file) as GameData; }catch(Exception e){ Debug.LogException (e, this); }finally{ if(file != null){ file.Close (); } } } } [Serializable] class GameData{ private bool isGameStartedFirstTime; private bool isMusicOn; private bool[] levels; private int[] highscore; public void SetIsGameStartedFirstTime(bool isGameStartedFirstTime){ this.isGameStartedFirstTime = isGameStartedFirstTime; } public bool GetIsGameStartedFirstTime(){ return this.isGameStartedFirstTime; } public void SetIsMusicOn(bool isMusicOn){ this.isMusicOn = isMusicOn; } public bool GetIsMusicOn(){ return this.isMusicOn; } public void SetHighScore(int[] highscore){ this.highscore = highscore; } public int[] GetHighScore(){ return this.highscore; } public void SetLevels(bool[] levels){ this.levels = levels; } public bool[] GetLevels(){ return this.levels; }
public static MusicController instance; public AudioClip background, gameplay, loseSound, winSound; [HideInInspector] public AudioSource audioSource;
void CreateInstance(){ if (instance != null) { Destroy (gameObject); } else { instance = this; DontDestroyOnLoad (gameObject); } }
void InitializeVariables(){ audioSource = GetComponent<AudioSource> (); } void Awake(){ CreateInstance (); InitializeVariables (); }
// Use this for initialization void Start () { } // Update is called once per frame void Update () { } void CreateInstance(){ if (instance != null) { Destroy (gameObject); } else { instance = this; DontDestroyOnLoad (gameObject); } } void InitializeVariables(){ audioSource = GetComponent<AudioSource> (); } public void PlayBgMusic(){ if(background){ audioSource.clip = background; audioSource.loop = true; audioSource.Play (); } } public void GameplaySound(){ if(gameplay){ audioSource.clip = gameplay; audioSource.loop = true; audioSource.Play (); } } public void StopAllSounds(){ if(audioSource.isPlaying){ audioSource.Stop (); } }
public GameObject settingsPanel, exitPanel; public Toggle soundToggle; // Use this for initialization void Start () { if(GameController.instance != null && MusicController.instance != null){ if (GameController.instance.isMusicOn) { MusicController.instance.PlayBgMusic (); soundToggle.isOn = true; } else { MusicController.instance.StopAllSounds (); soundToggle.isOn = false; } } } // Update is called once per frame void Update () { if(Input.GetKeyDown(KeyCode.Escape)){ if (exitPanel.activeInHierarchy) { exitPanel.SetActive (false); } else { exitPanel.SetActive (true); } if (settingsPanel.activeInHierarchy) { settingsPanel.SetActive (false); } } } public void PlayButton(){ SceneManager.LoadScene("Level Menu"); } public void SettingsButton(){ settingsPanel.SetActive (true); } public void CloseSettingsButton(){ settingsPanel.SetActive (false); } public void SoundToggle(){ if (soundToggle.isOn) { GameController.instance.isMusicOn = true; MusicController.instance.PlayBgMusic (); GameController.instance.Save (); } else { GameController.instance.isMusicOn = false; MusicController.instance.StopAllSounds (); GameController.instance.Save (); } } public void YesButton(){ Application.Quit (); } public void NoButton(){ exitPanel.SetActive (false); }
using UnityEngine.SceneManagement;
using UnityEngine.UI;
Exit Panel Background
Exit Panel
Text
Buttons
Settings Panel Background
Settings Panel
Then add a several children (Text, Toggle, Button) for the Settings Panel. Then set the values of each component as shown below.Text
Button
Toggle
Now that you’re done will the components there is one thing we need to do. Attach all the needed components to the MainController to be able to run the application successfully.Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.