Using BinaryWriter/Reader to save levels?


I have recently come across BinaryWriter and BinaryReader. What I can tell so far, you can save information with it. If I am wrong, please tell me! But if this is the case, would it be possible to use it to save levels for a level editor? Like saving all the items and objects the player put in the scene. How would I do it? And how would I read it? I have been searching for some information about this for around 30 minutes and I will keep looking for a while.

If you don’t want to read the whole text, just answer these questions.

Is is possible to save all the objects in a scene to a custom format? Example: mylevel.gamelvl

How would I save it?

How would I read the information in the file so everything would appear in the game again?

Thanks in advance!

It’s certainly possible and in fact it’s relatively easy.

Before continuing: Have a look at Unity Serializer, it is a free all-in-one package that saves and loads everything your want.
If you decide to use it, also have a look at this link.

Now, as for custom serialization, the basic code is as presented in this Stackoverflow thread and also this link.. I hope it’s ok to just hrow a bunch of links at you, but of course this has been done multiple times, including with Unity, so it’s best to see how others have successfully done it :slight_smile:

Tha way my level save and load features work is that I have everything that needs to be loaded as a prefab, and it has a script that holds a unique ID when the object is created the first time. When saving, the custom savegame class holds an array of all the objects with their corresponding IDs and which prefabs they are. When loading, this inforatmion is used to instantiate the objects again. Things like position, rotation and any custom attributes can of course also be serialized (How to serialize any variables of a class, look at yet another thread! :smiley: