• 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 sjoerdwouters · Jul 08, 2020 at 11:15 AM · vector3serializationsavingdatadictionary

Serializing Dictionary with Vector3 keys

hello everyone that finds t$$anonymous$$s question. i need help with saving some data for my game. i need to serialize and store a dictionairy with a vector3 as key. exept unity cant do t$$anonymous$$s for some stupid reason. please help me.

T$$anonymous$$s is the data i want to store:

 [System.Serializable]
 public class WorldData
 {
     public string worldname;
     public int smoot$$anonymous$$ng;
     public int noisetype;
     public float zoom;
     public int octaves;
     public int chunkrenderdistance;
     public int chunksize;
 
     public Dictionary<Vector3, WorldGenerator.TerrainChunk> WorldDataDictionairy = new 
     Dictionary<Vector3, WorldGenerator.TerrainChunk>();
 }

T$$anonymous$$s is the function i nuse to save t$$anonymous$$s data:

     //worldgenerator -> worlddata -> file
     public void SaveWorld()
     {
         var worldgenerator = 
         GameObject.FindObjectOfType<WorldGenerator> 
         ().GetComponent<WorldGenerator>();
 
         WorldData worlddata = new WorldData();
 
         worlddata.worldname = worldgenerator.worldname;
         worlddata.chunksize = worldgenerator.chunksize;
         worlddata.chunkrenderdistance = 
         worldgenerator.chunkrenderdistance;
         worlddata.octaves = worldgenerator.octaves;
         worlddata.zoom = worldgenerator.zoom;
         worlddata.noisetype = worldgenerator.noisetype;
         worlddata.smoot$$anonymous$$ng = worldgenerator.smoot$$anonymous$$ng;
         worlddata.WorldDataDictionairy = 
         worldgenerator.currentWorldChunksGenerated;
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/" + 
         worlddata.worldname + ".world");
         bf.Serialize(file, worlddata);
         file.Close();
     }

t$$anonymous$$s is the function i use to load t$$anonymous$$s data:

     //file -> worlddata -> worldgenerator
     public void LoadWorld()
     {
         var worldgenerator = 
         GameObject.FindObjectOfType<WorldGenerator> 
         ().GetComponent<WorldGenerator>();
 
         WorldData worlddata = new WorldData();
 
         BinaryFormatter bf = new BinaryFormatter();
         if(System.IO.File.Exists(Application.persistentDataPath + "/" + 
         DropDownLabel.text + ".world"))
         {
             FileStream file = File.OpenRead(Application.persistentDataPath + 
             "/" + DropDownLabel.text + ".world");
             if(file != null)
             {
                 worlddata = (WorldData)bf.Deserialize(file);
                 file.Close();
             }
         }
 
 
         worldgenerator.worldname = worlddata.worldname;
         worldgenerator.chunksize = worlddata.chunksize;
         worldgenerator.chunkrenderdistance = 
         worlddata.chunkrenderdistance;
         worldgenerator.octaves = worlddata.octaves;
         worldgenerator.zoom = worlddata.zoom;
         worldgenerator.noisetype = worlddata.noisetype;
         worldgenerator.smoot$$anonymous$$ng = worlddata.smoot$$anonymous$$ng;
         worldgenerator.currentWorldChunksGenerated = 
         worlddata.WorldDataDictionairy;
     }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ray2yar · Jul 08, 2020 at 11:58 AM

You need to save the information inside the dictionary individually (FALSE). However, your WorldTerrain data might also give you problems. Anyways.... somet$$anonymous$$ng like t$$anonymous$$s:

     foreach(Vector3 key in MyData.Keys)
     {
         //do stuff with the data... 

     }

Is your world procedural? If it is I t$$anonymous$$nk it would be way simpler to use Random.Seed and save JUST the random seed. Then when you load the game you just load t hat key, send it to your generator, and then rebuild the world from that.

Comment
Add comment · Show 4 · 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
avatar image sjoerdwouters · Jul 08, 2020 at 12:09 PM 0
Share
avatar image ray2yar sjoerdwouters · Jul 08, 2020 at 12:24 PM 0
Share
avatar image ray2yar sjoerdwouters · Jul 08, 2020 at 12:55 PM 0
Share
avatar image sjoerdwouters ray2yar · Jul 08, 2020 at 02:14 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

170 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 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

How to structure my project so that I can save/load information? 0 Answers

Save/Load Animation State of Instantiated Prefabs 0 Answers

Saving changes to MeshRenderer at edit time 0 Answers

In this case the lists do not serialize. Why? 2 Answers

Multiplayer data exchange 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