Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Spacejet13 · Feb 02, 2019 at 04:58 AM · gameobjectserialization.netsaveloadbinaryformatter

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Feb 02, 2019 at 06:23 AM 0
Share

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".

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges