Loading levels from Byte arrays

I am looking for a way to save my levels as byte arrays and load them as those files.
I am designing a dungeon crawler with a level editor, so I thought this would be a good way to allow people to share their created dungeons and levels as files.

I would also need a way to populate a set of uGUI buttons based on how many files there were in the folder, and what they were called, and of course a way to distinguish between the default “official” levels and the user created ones.

Has anyone ever seen anything like this done before or know of anywhere I can learn how to save (and load) levels as byte array files?

You cannot save an array, you can only save a file (as one dimension of bytes (I suppose that is an array)). So you will need to write your array to a file and give it your own file extension type and later on read and parse the data back into the level. I think you need to explain more what you mean by “save a level as byte array”. Maybe I am interpreting incorrectly?

…sorry, I mis interpreted the question, my bad…

Unity doesn’t have a way to create scenes/assetbundles or anything like that at runtime without the editor.

What you can do though is serialize a structure of gameobjects into your own custom structure. Basically mimicing the unity serializer. As long as the scene is constructed from existing assets/scripts/etc this shouldn’t be that complex of an issue.

If you serialize some hierarchy of gameobjects, you can save the generated data to disk (or elsewhere), and use it to rebuild the level elsewhere.

2 Likes

We switched to binary a while back, it was a huge improvement on speed, security, loading and efficiency. Ours was developed by our team, so I can’t provide details, but it is conceptually similar to BSON.

For the other, System.IO will let you gather data from the file system. To separate official from added, just keep a list in your main code that contains name and maybe checksum.

1 Like

For those who may have got confused by this, what I want to do is put representations of the prefabs in positions, including a start position for the player and enemies, traps, switches, doors and exit (with component information for trap and switch links) and save this information in a custom file (most likely with a custom extension).

I would then load these files up into the game as level files, similarly to how older games handled levels.

This allows people to design their own levels, or even mod/change those that already exist.

One thing I would like to do one day is allow people to bring in their own artwork, but that is something a very long way down the tree of things to do.

For now I just need to work out how to save an entire scene with component information into an externally loadable file which will instantiate what I need using the information in said file.

Yeah, it’s doable.

Just google “unity serialize scene”.

You’ll find lots of stuff.