- Home /
Save an array with custom classes
Okay, I've looked around but can't find anything that helps me out with this issue:
I have a list, containing several custom classes, identical in layout to the example below which is a snippet from one single file, OldSystem.cs :
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
[Serializable]
public enum DriveType {
Floppy,
HDD,
SSD,
Optical
}
[Serializable]
public class File {
public string fileName;
public int fileSize;
public List<string> contents = new List<string>();
public File(string fName, int fSize)
{
fileName = fName;
fileSize = fSize;
}
}
[Serializable]
public class Drive {
public string letter;
[SerializeField]
public DriveType driveType;
[SerializeField]
public List<File> files = new List<File>();
public Drive(string assignedLetter, DriveType type)
{
letter = assignedLetter;
driveType = type;
}
}
[RequireComponent(typeof(AudioSource))]
public class OldSystem : MonoBehaviour {
[SerializeField]
public Drive[] drives;
// Rest of main script code here
....
}
I would like to be able to save the drives list from the OldSystem.cs MonoBehaviour somehow, as it is modified at runtime (either in the Editor or in the standalone build - but mostly the Editor at the moment) so that when I go back to the Editor, the changes are persistant.
As you can see, I've used a scattergun loaded with [Serializable] statements on the script, but to be honest I don't really understand what they do or how serialization works.
I would be more than happy if someone can point me in the direction of a script or tutorial or some guidance on how to save the drives list to an XML file or other type of file in the Assets folder, perhaps - so long as that approach would work with a standalone build as well.
Otherwise, any help or suggestions would be gratefully received. I'm certainly not a beginner at programming, but I'm very inexperienced at this particular sort of thing (serialization, getting Unity to save data etc.)
Have I even declared the DriveType enum, File class or Drive class in the right places?
Thanks! :D
Answer by D3Duck · Sep 01, 2015 at 08:41 PM
First of all, the enum is declared outside a class, so it is now global - available everywhere. Usually its not a good practice though, since it could mess up code where you dont want it. If you put it inside a class then you can still reference it with the class name. The other declarations should definitely be fine.
As you said you are inexperienced with saving data, just check this out. It is quite lengthy but I watched it when first learning about saving and it is very good. You will then also see how easy it is to save an enum ;) (the save as saving everything else)
Just in case you really want to save as XML, then you can find tutorials on this as well, but definitely watch this first because I think it would be best.
Cheers and good luck.
Thanks very much for this, D3Duck. I had the time to sit down and watch the video you linked to this morning, and it was spot on. The first half about PlayerPrefs wasn't relevant, but once I'd seen how simple it is to use a FileStream and Binary Formatter to save/load data, I threw the functions together into an appropriate script in my project and it worked first time! I can now save/load custom classes!
Thanks again - you've saved me much head-scratching! :D