public void Save() { FileStream file = null; try { BinaryFormatter bf = new BinaryFormatter(); file = File.Create(Application.persistentDataPath + "/GameData.dat"); if (data != null) { data.setHiScore(hiScore); data.setIsMusicOn(isMusicOn); data.setIsGameStartedFirstTime(isGameStartedFirstTime); bf.Serialize(file, data); } } catch (Exception e) { } finally { if (file != null) { file.Close(); } } }
void InitializeVariables() { Load(); if (data != null) { isGameStartedFirstTime = data.getIsGameStartedFirstTime(); } else { isGameStartedFirstTime = true; } if (isGameStartedFirstTime) { isGameStartedFirstTime = false; hiScore = 0; isMusicOn = true; data = new GameDat(); data.setHiScore(hiScore); data.setIsMusicOn(isMusicOn); data.setIsGameStartedFirstTime(isGameStartedFirstTime); Save(); Load(); } else { isGameStartedFirstTime = data.getIsGameStartedFirstTime(); isMusicOn = data.getIsMusicOn(); hiScore = data.getHiScore(); } }