Serialize Fields converted to object

Hello !

I’m currently writing a Save System, while JSON has been recommended to me, I still wonder if my own system wouldn’t be… more innovative.

Sooo, I’m using Binary Serialization, like

var b = new BinaryFormatter();
b.Serialize (myFile, myData);

Data like GameObjects for example cannont be serialized like this, so I simply tried to serialize objects, but, if my object has the value for example of a GameObject, its throwing again the error that GameObjects cant be serialized.

Ok, alright, tried a little harder way.
To avoid the message of not being able to cast from source to destination type contains this example a list.

List<object> objs = new List<object>();

object obj = myGameObject; // I'm not going to write down all declarations, you know there is a GameObject extisting if I write myGameObject ;)

objs.Add ( ((Enumerable) obj).Cast<object>()) );

Anndd, its still throwing the error, myGameObject wouldn’t be serializeable.

Does somebody knows a workaroung for this ?
The main thought is simply to save everything as Object, but it seems like, though I convert it to object, it recognizes it as the type the value has been before converting it to object…

You cant serialize game objects. How I solve this issue in my code is have a save manager which converts each game objects into a CLR DTO (plain common language runtime object). I then serialize/deserialize this collection. Common properties include the prefab name (string), position, rotation, scale, velocity and health/state information. For the health/state information I use a dictionary and call a method on my objects to read/write themselves (similar to how it is done in WINRT).

For Json I am a fan of Full Serializer

EDIT

An example CLR Object

pubic class BadGuySave{
 public Vector3 Position {get;set;}
}

public class BadGuy: MonoBehaviour{
 public BadGuySave Serialize(){
  return new BadGuySave{ Position = transform.position};
 }
 public void Deserialize(BadGuySave s){
  transform.position = s.Position;
 }
}

I did something similar to what nventimiglia suggested above, my SerializeHelper, available for free here:

SerializeHelper - Free save and load utility. (De)Serialize all objects in your scene.