- Home /
Saving gameobjects to .NET binary files
So I know that you can't save any predefined Unity objects using .NET serialization, but you can create wrapper classes for things such as Transforms. What would you do to Gameobjects? I know that they are just holders for all the components, so I wanted to ask how I could Save and load the mesh data, collider, material, etc?
there is a reason why unity has no answer to what you are asking. if you did successfully get an all inclusive stand alone file format that included all the components, scripting, mesh and texture info, blah, blah, blah. it would take your users forever to load and any reasonable game would crash an burn.
most saving and loading in game development is done through some sort of prefabs or references.For proficiency you only send "need to know" information only. how much info you need to save/load is different for every game.
for example if you wanted to save a material of a game object, unity offers ways to crunch texture information down into image formats to create a png or jpg in byte array format. but if you simply wanted to save the material an object is using it would be easier to save the index number of the material you are using in a small text file.
if your game has prefabs then it might be as easy as saving the name or the index number of your prefab.
there are easy ways to save floats for position and rotation if thats what you are looking for. if there are specifac variables out of scripts you can do that to.
anyways if you are more specific on what why and how you are wanting to save things i can help. I wish game development was as easy as just saying "save everything".
Answer by Bunny83 · Feb 02, 2019 at 06:30 AM
Well you would need wrappers as well. For everything. However i would strongly recommend to not use the BinaryFormatter for save files. First of all they aren't as compact as you may think. They use the .NET remorting protocol which is strongly typed and stores a lot of metadata in the binary stream. You can also run into several issues with newer .NET versions or if you change even the slightest bit on your classes that you serialize.
If you want a solution that works more or less out of the box you may want to use Unity's JsonUtility to serialize your component / object data and finally use any sort of compression library to turn the text into a compressed binary stream. Of course you still have to stitch together all components which make up your original gameobject by hand.
Another solution would be to create your own binary format. Don't forget a version identifer in case something changes in the future. I've created a MeshSerializer which can serialize Mesh objects in a compact binary format. Though note that currently it only supports 4 UV channels as the new 8 channels weren't out the time it was written. However it does support skinned meshes as well as blendshapes. Keep in mind that the actual bone structure of a skinned Mesh is not part of the Mesh but of the SkinnedMeshRenderer. It's vital to keep the order of the bones array the same. To save the required type information you can use my SerializableType (Just use the static Read / Write methods)
Finally you could roll your own json format by using my SimpleJSON framework. When you use the additional "SimpleJSONUnity.cs" you already got conversion support for most primitive Unity types (vectors, quaternion, matrix4x4, ...). This makes it quite easy to write a serialize / deserialize method for each component you want to support.
Keep in mind that in the last three cases (when using json or your own binary format) you have to store type information yourself. So you have to be able to identify the type of object that you save in order to recreate it. If you plan to create a serializer for a whole scene you probably want to implement some sort of reference system. JSON doesn't has support for object references, however you could implement a custom one. So each object that is serialized gets a unique ID. When you want to reuse an object on another serialized object within the same serialization stream you could insert a "special type" which just references the already serialized object based on that ID.
Long story short: Unity doesn't have a built-in solution to save gameobjects or scenes at runtime. Note in many cases saving the whole scene would be overkill since usually most parts of a scene don't change / can't change. So it usually makes more sense to store the name / index of the level as well as "modifications". Of course in order to do that you have to track those modifications. This includes destroying objects.
Your answer
Follow this Question
Related Questions
SerializationException: End of Stream encountered before parsing was completed. 0 Answers
Bool array to binaryformatter method 1 Answer
When I serialize a GameObject, what gets saved? 0 Answers
How to serialize a gameobject using binaryformatter? 0 Answers
How to save/load references to components and prefabs 1 Answer