Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 hollowspecter1 · May 13, 2015 at 03:29 PM · serializationeditorwindowdictionary

Dictionairy Serialization Problem: Keys and values get lost

So, I've been working on this problem for a while. I have an .asset, derived from a Scriptable Object, that is mainly one float and a Dictionairy. I've been using help from here: http://answers.unity3d.com/questions/460727/how-to-serialize-dictionary-with-unity-serializati.html to serialize my Dictionairy. My keys look like this:

 [Serializable]
 public class Hex : IEquatable<Hex>, ISerializationCallbackReceiver
 {
     public static Hex[] directions = {
           new Hex(+1,  0), new Hex(+1, -1), new Hex( 0, -1),
           new Hex(-1,  0), new Hex(-1, +1), new Hex( 0, +1)};
 
     [SerializeField]
     public int q;
     [SerializeField]
     public int r;
 
     public Hex(int q, int r)
     {
         this.q = q;
         this.r = r;
     }
     public Hex() : this(0,0) { }
 
     //more stuff here
 
     public override bool Equals(System.Object obj)
     {
         Hex h = obj as Hex;
         if (h == null)
             return false;
         else
             return h.q == q && h.r == r;
     }
 
     public override int GetHashCode()
     {
         string s = q + "," + r;
         return s.GetHashCode();
     }
 
     public bool Equals(Hex other)
     {
         if (other == null)
             return false;
         else
             return other.q == q && other.r == r;
     }
 
     public void OnAfterDeserialize()
     {
         
     }
 
     public void OnBeforeSerialize()
     {
         
     }
 }

I've been working on different solutions, but basically what I am trying to do is:

Have a custom editor window that can Generate, Load and Save those Dictionairy-Assets (They're Grid-Maps). I've been using Object-Fields to get my target, then change it, and "set it Dirty", or generate new Assets and copy and paste, but somehow my Map-Objects lose Keys and Values, causing IndexOutOfRange Errors here and there.

Is the problem in my Key Class?

Unfortunately my code is starting to get messy.

I feel like, if that does not work out I just write my own FileWriter that writes the Dictionairy in a textfile and read it out when I need it. It's exhausting, help!

Here's a picture of what I am trying to do:

Screenshot of my Editor Window

ss-2015-05-12-at-121928.png (17.1 kB)
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 FortisVenaliter · May 13, 2015 at 04:39 PM 0
Share

I don't see the actual dictionary in your code, so it's hard to diagnose directly, but let me tell you how I solve this problem:

I usually create a secondary serializable struct that hass all of the value data as well as a field for the key. Then in OnBeforeSerialize, I copy the source data into those structs and write them out to an array or list which can be automatically serialized by Unity. Then in OnAfterDeserialize, I read that list or array back into the Dictionary.

It can be a bit troublesome when working in the editor, as serialization happens frequently, but in the game, it works well.

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

SerializedProperty and PropertyField NullReference 1 Answer

PropertyDrawer and EditorWindow 1 Answer

Nested Dictionary data lost after playmode 0 Answers

Serializing Dictionary with Vector3 keys 1 Answer

Custom Editor window loses values on play. 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