//we initialize variables here void InitializeVariables() { //first we load any data is avialable Load(); if (data != null) { isGameStartedFirstTime = data.getIsGameStartedFirstTime(); } else { isGameStartedFirstTime = true; } if (isGameStartedFirstTime) { //when game is started for 1st time on device we set the initial values isGameStartedFirstTime = false; hiScoreSimple = 0; hiScoreMedium = 0; hiScoreHell = 0; isMusicOn = true; data = new GameData_HeadGames(); //storing data data.setIsGameStartedFirstTime(isGameStartedFirstTime); data.setIsMusicOn(isMusicOn); data.setHiScoreSimple(hiScoreSimple); data.setHiScoreMedium(hiScoreMedium); data.setHiScoreHell(hiScoreHell); Save(); Load(); } else { //getting data isGameStartedFirstTime = data.getIsGameStartedFirstTime(); isMusicOn = data.getIsMusicOn(); hiScoreSimple = data.getHiScoreSimple(); hiScoreMedium = data.getHiScoreMedium(); hiScoreHell = data.getHiScoreHell(); } }
//method to load data public void Load() { FileStream file = null; try { BinaryFormatter bf = new BinaryFormatter(); file = File.Open(Application.persistentDataPath + "/GameData.dat", FileMode.Open);//here we get saved file data = (GameData_HeadGames)bf.Deserialize(file); } catch (Exception e) { } finally { if (file != null) { file.Close(); } } }