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 Kazel1873 · Oct 03, 2016 at 07:54 PM · editorserializationserialize

Got a Problem when serializing Class Dictionary with Custom Editor

Hi there. I've got so much frustation while using my Custom Editor.

alt text

first, I made a MagicInformation Class for my UI. it contains sprite and values for GUI. and also I used Dictionary for more comfortable management.

and I thought it'll be nice if I can serialize all of my Classes via Inspector. so I made a Custom Editor for UICore like this Sample Code. (sadly the Codes are not from myself, though.)

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 using System.Collections;
 
 [CustomEditor(typeof(UICore))]
 
 public class UICoreEditor : Editor {
     public bool Fold = false;
     public string Aditive_Key = "";
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI ();
         Aditive_Key = EditorGUILayout.TextField ("Input Key", Aditive_Key);
         Dictionary<string, MagicInformation> Dic = (target as UICore).MagicList;
 
         if (GUILayout.Button ("Add Data", GUILayout.Width (100))) {
             MagicInformation Data = new MagicInformation ();
 
             if (Dic.ContainsKey (Aditive_Key) == false)
                 Dic.Add (Aditive_Key, Data);
             else
                 EditorUtility.DisplayDialog ("Error", "Same Key Exists", "Close");
         }
 
         if (GUILayout.Button ("Delete Value", GUILayout.Width (100))) {
 
             if (Dic.ContainsKey (Aditive_Key) == true)
                 Dic.Remove (Aditive_Key);
             else
                 EditorUtility.DisplayDialog ("Error", "No Key Matched", "Close");
         }
 
         EditorGUILayout.Foldout (Fold, string.Format ("List({0})", Dic.Count));
         {
             foreach (var item in (target as UICore).MagicList) {
                 EditorGUILayout.LabelField (item.Key);
                 EditorGUILayout.BeginVertical ("Box");
                 item.Value.MagicImage = EditorGUILayout.ObjectField ("Sprite", item.Value.MagicImage, typeof(Sprite), false) as Sprite;
                 item.Value.MagicName = EditorGUILayout.TextField ("Magic Name", item.Value.MagicName);
                 item.Value.MagicCost = EditorGUILayout.IntField ("Magic Cost", item.Value.MagicCost);
                 item.Value.MagicDescription = EditorGUILayout.TextField ("Magic Description", item.Value.MagicDescription);
                 EditorGUILayout.EndVertical ();
             }
         }
     }
 }

and when I checked UICore I got this result.

alt text

It seemed works fine, but When I put my values and run a Game, I got an Error message "KeyNotFoundException: The given key was not present in the dictionary.". and all my values are gone from Inspector.

And I cannot even guess why this happens. I though "EditorGUILayout.Foldout()" function is enough for putting values for my dictionary, but there's no values. why this keeps happening?

please help me guys... my deadline is coming... :(

1.jpg (31.2 kB)
3.jpg (50.4 kB)
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 Adam-Mechtley · Oct 03, 2016 at 08:05 PM

Hi @Kazel1873! The problem is Unity cannot serialize Dictionary<TKey, TValue>. When you create those entries in your editor, they only live on that instance until the level changes, the domain is reloaded, the game starts/stops, etc.

You'll need to do some other stuff to ensure you can't get duplicate entries and such, but basically what you need to do is:

  1. Make sure MagicInformation is marked with System.SerializableAttribue

  2. Make a KeyedMagicInformation class also marked with System.SerializableAttribute that has a string field and a MagicInformation field

  3. Make a list or array of KeyedMagicInformation on your MonoBehaviour

  4. Revise your custom editor controls as necessary. I would recommend using the ReorderableList widget in UnityEditorInternal

  5. (Optionally) Implement ISerializationCallbackReceiver on your MonoBehaviour to map your KeyedMagicInformation list to a private Dictionary<string, MagicInformation> if required or desirable in your implementation

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

SerializedProperty with children class fields 2 Answers

object list not serializing 2 Answers

Saving Game Confussion 1 Answer

ONE monobehaviour, all serialization!? 0 Answers

Save Data from Editor Script, Serialize Nested Class List 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