GotNoPockets / MayhemJamGameThingy / Assets / Scripts / SaveScript.cs
SaveScript.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


public class SaveScript : MonoBehaviour
{
	public void SaveData()
	{
		// does stuff with binrary one assumes :)
		BinaryFormatter BinFormat = new BinaryFormatter();

		FileStream DataFile;

		// creates a file in the data path /C/users/appdata/......
		if (!File.Exists(Application.persistentDataPath + "/gamedata.ini"))
		{
			DataFile = File.Create(Application.persistentDataPath + "/gamedata.ini");
		}
		else
		{
			DataFile = File.Open(Application.persistentDataPath + "/gamedata.ini", FileMode.Open);
		}

		// creates an instance of the game data class...
		GameData Data = new GameData();


		// Change Values here
		Data.PlayMusic = FindObjectOfType<MusicEnableScript>().MusicEnabled;


		// Converts to binrary, using the data from the data thingy in a data file
		BinFormat.Serialize(DataFile, Data);

		// Closes the data file
		DataFile.Close();
	}

	public void LoadData()
	{
		// checks to see if the file exsists, duh...
		if (File.Exists(Application.persistentDataPath + "/gamedata.ini"))
		{
			BinaryFormatter BinFormat = new BinaryFormatter();

			// Makes a local file with the file in the location and opens it :)
			FileStream DataFile = File.Open(Application.persistentDataPath + "/gamedata.ini", FileMode.Open);

			// converts the file to readable thingys :) ( "unbinraryfys" the file )
			GameData Data = (GameData)BinFormat.Deserialize(DataFile);

			// Closes the file
			DataFile.Close();


			// update values here
			FindObjectOfType<MusicEnableScript>().MusicEnabled = Data.PlayMusic;
		}
	}


}

[Serializable]
class GameData
{
	public bool PlayMusic;
}