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 Korahan · May 08, 2018 at 09:34 PM · serializationxmlsavingparsing

Saving serializable class list at runtime.

Good Day!

Status quo: Currently, I use serializable classes for things like Items or characters in my game. These classes contain "stats" (ints, strings, floats, bools) but also Unity-classes (sprites, gameObjects) that define their appearance and are stored in lists (say databases), where they can be found by an integer ID if needed. All of it is used to generate procedural content. All of it works in runtime.

Issue: I need to be able to save the lists at runtime. Either for me to use it (for debugging, content creation) in the editor or for the player, who expands the list of his known randomly created entities with every second he is playing.

What is the best way to achieve that?

I tried to use XML parsing but it doesn't seem to work. Maybe because my classes contain unity-classes? Also, it would make cheating very easy, if the player could just open the .xml in an editor and manipulate all values.

Thank you!

Comment
Add comment · Show 2
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 TreyH · May 08, 2018 at 09:42 PM 0
Share

Certain things in Unity can be serialized pretty easily, other not so much. There's an exhaustive number of posts where others have discussed serializing these sorts of things, but you'll likely need to write a wrapper of some sort that can take the important information from your procedural stuff (terrain heights, rng seed for a roguelike dungeon layout, positions, rotations, whatever you will need to rebuild it).

Then for the reload process, you'd take that serialized wrapper object and recreate the object using that important information.

avatar image Korahan TreyH · May 09, 2018 at 03:09 PM 0
Share

Thank you for your answer!

1 Reply

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

Answer by Korahan · May 09, 2018 at 03:26 PM

What I did:

I rewrote parts of my serializable container-classes to only contain serializable fields. For example, I changed the type "GameObject" variables pointing to the prefabs to strings that contain the corresponding path (resource folder).

Then I used binary formatter to save and load the lists like so:

 private List <MyEntity> myEntityDB; 
     
 //Load
 public void LoadDatabase(){
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Open(Application.persistentDataPath + "/myEntityDatabase.edb", FileMode.Open);
     MyEntityDB = (List <MyEntity>)bf.Deserialize(file);
     file.Close();
 }
 //Save
 public void SaveDatabase(){
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/myEntityDatabase.edb");
     bf.Serialize(file, myEntityDB);
     file.Close();
 }

Quite simple, but I am still not sure if this is the best way. At least I can save and load my generated content now.

EDIT: I decided to convert my class lists into json string arrays before serializing. This way I can change my classes (adding new fields for example) without running into serialization errors. I am now quite happy with the solution.

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

86 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

Related Questions

Trying to XML serialize objects, getting error that UnityEngine.Transform doesn't implement Add() 1 Answer

Storing list aka "save function" 3 Answers

Need experienced opinion: Saving a players inventory: XML or PlayerPrefs? 1 Answer

Questions about XML serialization of Class Properties 1 Answer

Serializing GameObject Data with XML, Adaptation 2 Answers


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