- Home /
[Serialization] Multiple classes into one file?
I'm new to binary serialization and have already encountered a problem that's left me perplexed.
What I want to do is this: Take data from two custom classes and serialize it into a single "save" file.
It sounds simple, but I can't for the life of me figure out how to go serializing them simultaneously via "bf.Serialize()" (where "bf" is the name of the Binary Formatter).
Just so you guys have an idea of what I'm working with, here's my code:
public class SaveLoad : MonoBehaviour {
GameObject player;
GameObject[] enemies;
public void SaveGame(string fileName)
{
// Grab player & enemies
player = GameObject.FindGameObjectWithTag("Player");
enemies = GameObject.FindGameObjectsWithTag("Enemy");
// Prepare to write
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/" + fileName);
// Get player data
PlayerData playerData = new PlayerData();
playerData.xPos = player.transform.position.x;
playerData.yPos = player.transform.position.y;
playerData.health = player.GetComponent<PlayerStats>().health;
playerData.hasKey = player.GetComponent<PlayerStats>().hasKey;
// Get enemy data
EnemyData[] enemyData = new EnemyData[enemies.Length];
for (int i = 0; i < enemyData.Length; i++) enemyData[i] = new EnemyData(); // Initialize class instances
for (int i = 0; i < enemies.Length; i++)
{
enemyData[i].instanceID = enemies[i].GetInstanceID();
enemyData[i].xPos = enemies[i].transform.position.x;
enemyData[i].yPos = enemies[i].transform.position.y;
enemyData[i].health = enemies[i].GetComponent<EnemyStats>().health;
}
// Write data to disk
bf.Serialize(file, playerData); // PROBLEM HERE
file.Close();
}
}
[System.Serializable]
class PlayerData
{
public float xPos;
public float yPos;
public int health;
public bool hasKey;
}
[System.Serializable]
class EnemyData
{
public float instanceID;
public float xPos;
public float yPos;
public int health;
}
The "PROBLEM HERE" comment shows where I'm stuck. I can plug in either playerData or enemyData, but not both. As a result, I can only serialize one of the classes! How the heck would I go about serializing both of them?
I'd also like to point out two things:
The PlayerData and EnemyData classes are extremely similar only because this is a small test project. If I were making an actual game, they'd obviously have far more differences. As a result, these classes must be kept separately. I don't want to combine them into a single unified "data" class.
I realize I could split the classes up into two separate binary files, and just load (deserialize) them one-after-another, but I don't want to do that. I want to have a single save file that stores all of my data.
Thanks in advance for any help!
Answer by Feyyyyy · Mar 20, 2017 at 07:15 PM
Hi,
I think something like that will solve your problem.
[System.Serializable]
class AllData{
public PlayerData playerData;
public EnemyData[] enemyDatas;
}
Serialize this class instead of serializing them separately. I do it all the time and i don't see any downside of this.
@Chubzdoomer this should work. You've already done all of the legwork, so just add one more layer of abstraction to your serialization process and let unity figure out the rest.
Thanks guys, I just tried it and it worked perfectly! I didn't even think about encapsulating the classes inside another class.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Serial Exemption when saving binary file 1 Answer
Serializing an Action as XML? 0 Answers
Distribute terrain in zones 3 Answers
Deserialize a static class C# 1 Answer