//method to save data public void Save() { FileStream file = null; try { BinaryFormatter bf = new BinaryFormatter(); file = File.Create(Application.persistentDataPath + "/GameData.dat"); if (data != null) { data.setIsGameStartedFirstTime(isGameStartedFirstTime); data.setHiScoreSimple(hiScoreSimple); data.setHiScoreMedium(hiScoreMedium); data.setHiScoreHell(hiScoreHell); data.setIsMusicOn(isMusicOn); bf.Serialize(file, data); } } catch (Exception e) { } finally { if (file != null) { file.Close(); } } }
//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(); } }