Writing an class array to disk?

using UnityEngine;
using System.Collections;

public class ItemData : MonoBehaviour {

	void Start (){

	Item[] ItemDatabase = {
			new Item ("Baseball", "This a baseball", 5),
			new Item ("Ladder", "You can climb this", 5),
			new Item ("Radio", "Plays sounds", 5),
			new Item ("Book", "You can read this", 5) 

    class Item {

		public string _Name {get;set;}
		public string _Description {get;set;}
		public float _Value {get;set;}
		public Item (string Name, string Description, float Value)
			_Name = Name; _Description = Description; _Value = Value;


What is the easiest and most beginner friendly way to save and load that array to the disk locally?

You can use a BinaryFormatter.

Create an instance of a simple class which acts as your “save game” and holds all data that will be saved.

public class Game {
     public string savegameName = "New Save Game",
     public ItemData.Item[] itemArray


create the instance (in this case, done in your own script as it uses the ItemDataBase variable):

Game newSaveGame = new Game() {
     savegameName = "My Saved Game";
     itemArray = ItemDatabase;


Call the Save function and pass it the Game instance:


To load, call the Load function and pass it the name of the saved file, it will return a deserialized Game instance which you can use to access the itemArray variable etc. in it.

Game loadedGame = SaveLoad.Load("My Saved Game");

The SaveLoad static class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization;
//I don't remember which of these are necessary and which are not

public static class SaveLoad {

	//it's static so we can call it from anywhere
	public static void Save(Game saveGame) {
		BinaryFormatter bf = new BinaryFormatter();

		//Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
		FileStream file = File.Create (Application.persistentDataPath + saveGame.savegameName + ".sav"); //you can call it anything you want
		bf.Serialize(file, saveGame);
		Debug.Log("Saved Game: " + saveGame.savegameName);

	public static Game Load(string gameToLoad) {
		if(File.Exists(Application.persistentDataPath + gameToLoad + ".sav")) {
			BinaryFormatter bf = new BinaryFormatter();

			FileStream file = File.Open(Application.persistentDataPath + gameToLoad + ".sav", FileMode.Open);
			Game loadedGame = (Game)bf.Deserialize(file);
			Debug.Log("Loaded Game: " + loadedGame.savegameName);
			return loadedGame;
		else {
			return null;