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 KingNeil · Dec 26, 2016 at 08:05 PM · classdictionarycompilestruct

[SOLVED]How do i make this dictionary survive recompiles?

So i'm trying to make this dictionary survive recompiles, but i don't know how.

 public class HexGraph : MonoBehaviour {
     
         public Dictionary<HexCell, List<Edge>> edges = new
             Dictionary<HexCell, List<Edge>>();  
     }

Here is the Edge struct, if that makes a difference.

 public struct Edge : IComparable<Edge> {
 
     public int CompareTo (Edge other) {
         return cost.CompareTo(other.cost);
     }
 
     public HexCell connection; 
     public float cost;
 
     public Edge (HexCell connection, float cost) {
         this.connection = connection;
         this.cost = cost;
     }
 }

EDIT

So i tried what RobAnthem suggested, but couldn't make it work, so instead i updated my dictionary OnEnable()

I'm still marking his answer as correct, because i'm sure it was my fault it didn't work.

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 jmgek · Dec 26, 2016 at 08:38 PM 0
Share

Are you saying you want to save the data that's in the dic, the questions is very confusing?

You need to serialize the Dictionary. http://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us

avatar image KingNeil jmgek · Dec 29, 2016 at 09:40 PM 0
Share

Sorry for my bad phrasing. I just want the dictionary to not loose it's data when i recompile my scripts, which i believe you do by serializing it.

I have no idea what XElement is, or X$$anonymous$$L in general, so i understand little of the link you so generously provided. I just want to know if there is an easy way of serializing a dictionary.

1 Reply

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

Answer by RobAnthem · Dec 29, 2016 at 10:58 PM

Basic binary serialization

 IFormatter formatter = new BinaryFormatter();  
 Stream stream = new FileStream("edges.bin", FileMode.Create, FileAccess.Write, FileShare.None);  
 formatter.Serialize(stream, edges);  
 stream.Close();  

Basic binary deserialization

 FileStream fs = new FileStream("edges.bin", FileMode.Open);
 IFormatter formatter = new BinaryFormatter();  
 edges = (Dictionary<HexCell, List<Edge>>)formatter.Deserialize(fs);  
 fs.Close();  




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

62 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

Related Questions

Class or struct? for multiple turrets...? 0 Answers

Variable updating itself or not (ex: Transform vs Vector3) 2 Answers

How -exactly- do classes and structs work in Unity? 0 Answers

Conversion of a static class to struct? 1 Answer

more Burgzerg Blues 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