Serializing gameobjects

this is… unbelievable! im searching around and apparently you cant serialize them…

i really do need a way around this, i have lots of gameobjects and i need them to be serialized with all their monobehaviour and non data

also i have many gameobjects with a complex structure, with childs and subchilds and dependancies

many other game libraries have strong serializers that handle circular references and everything complicated, how can unity not have anything at all?

dont tell me i have to dump the idea of saving my game :frowning:

Why serialize the game object itself… with all the access baggage that comes with them (potentially meshes, materials, colliders, physics, etc.) you’d end up with a ridiculous save game size. Serialize only the data about the gameobject you need. Position, rotation, parent, anything that you need to store. Then loading the game is just re instantiating the relevant gameobjects and setting the data through de-serialization.

because i would have to do all the work? like i said is not just about position, most of them are composed objects or dependant (like a enemy targetting something, or an enemy with a damaged part or an enemy with attached a weapon)

i wouldnt know where to start and assuming i figure it out seems like weeks of work just for a savegame

Well yes, it does require some work. On the plus side you end up with a game. Better yet you end up with a game that can fit and run on target devices.

I’m no expert on the best solution but as a starting point:

public class EnemySaveData
{
int ID;
string type;
List<bool> damagedParts;
int attachedWeaponID;
}

public class SaveData
{
List<EnemySaveData> enemies
}

public class LoadData
{
Foreach (enemy in enemies)
{
if (type == droid)
    GameObeject go = instantiate (droid);
    attachWeapon(attachedWeaponID);
    destroyParts(damagedParts);
}

A simplification sure, but not by much. Serialize the EnemySaveData through SaveData. De-serialize into the LoadData.

I didn’t include enemy targeting data because I can only assume that is done programmatically, so the enemy can just reacquire their targets.

Hope that helps as a start.

I’m going to be honest, I find many things inefficient about Unity. But adillix is spot on, and this is one thing Unity does very well as opposed to other engines.

Also as a bit of an addition to serialization for the purpose of save games, which sure you could say screw it and go for X Gigs of save game and just save your entire gamestate as it lives in memory, this format works very well for objects beyond the bounds of the playable area.

For example, droid squad A returns to base. No longer need any of the targeting info, meshes, materials etc for those instances, just loop them off into the EnemySaveData class. Have a co-routine that repairs a part every x mins, droid squad is “loaded” back in when fully repaired.

Game now only has a limit on the number of objects you can display and handle at any one time in the playable area. But for the wider game, armies and empires are moving across planet systems.

i dont think binary serialization can ever need 1 giga… unless you have hundreds of thousands of objects…
but not even a RTS have them.
anyway disk space is the least of my concerns, if there was a way to use 100x the disk space and save the whole scene easily i would go for it

I’m not exactly sure if this is possible for GOs, but I made a QuaternionSerializable and a Vector3Serializable class that basically take in Vector3s and Quaternions and change them to the above, which I can then serialize. After, I just deserialize and convert back to Vector3 and Quaternion values. So it might be possible to convert you GO to a GOSerilaizable class you write, serialize it and be good (Though like the above stated, you’d have data that you don’t have to save, thus wasting resources AND space).

Someone made an asset to do just that. It hasn’t been updated to support Unity 5, but it is free and the source is included.

https://www.assetstore.unity3d.com/en/#!/content/3675

Just for my curiosity, have you used it? And if so, how is it with space? (:

No, I have not had any reason to use it. My assumption on space usage is that it should be approximately the same as Unity’s scene files when set to output compressed binary data.