Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
29
Question by EMOTION-THEORY · Feb 17, 2014 at 10:40 PM · editorinspectorserializationdictionary

Dictionary in inspector

We all know Dictionaries do not appear in the Editor. What would be a way to get them to appear?

Suppose I have:

 [Serializable]
 public class MySerializableObject
 {
    public float Name;
    public float Description;
 }
 
 public Dictionary<string, MySerializableObject> MyDictionary;

Strings and Serializable objects both appear in the editor. How would I get MyDictionary (and all Dictionaries really) to display with their default inspectors for Key types and Value types?

Would a PropertyDrawer be the solution?

Any help / examples / solutions would be appreciated.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
57

Answer by Helical · Oct 22, 2014 at 11:49 AM

You could just do a hack, like you do for anything serious in unity.

expose a normal array.

 [Serializable]
 public struct NamedImage {
     public string name;
     public Texture2D image;
 }

 public NamedImage[] pictures;


then at the Start() or Awake(), or whenever your mind is flipped for it. put the array manually in a Hash or Dictionary or whatever. It looks alright in the inspector.

If it looks like a Hashtable, walks like a Hashtable, acts like a Hashtable, guess what, its a Hashtable.

Comment
Add comment · Show 9 · 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 codeman_nz · Aug 21, 2015 at 12:26 AM 0
Share

Genius! Thank you.

avatar image noptic · Sep 23, 2015 at 01:59 AM 3
Share

While this is a nice workaround it DOES not act like a Hashtable. This implementaton allows any number of images to share the same name. You should at least test if the resukting Dictonary has the same number of members as the source array.

avatar image Helical noptic · Sep 23, 2015 at 05:00 AM 3
Share

Agree :), but then again Unity is all about cutting corners.

avatar image mescalin · Dec 23, 2015 at 12:41 PM 0
Share

"If it looks like a Hashtable, walks like a Hashtable, acts like a Hashtable, guess what, its a Hashtable."

yes but it doesn't act like one and completely defeats the point of having one, this is a junk answer

avatar image semimono mescalin · Sep 17, 2021 at 06:29 AM 1
Share

The idea is that you populate a hash table in Start/Awake, and then you have the functionality available. Serializing a dictionary is, after all, turning it into a "list". Hence that O(n) operation at startup is more or less unavoidable (it could possibly be slightly more efficient if native, but probably not a lot).

That said, I do think this is something unity should have added native support for a long time ago. It's pretty fundamental.

avatar image wibble82 · Dec 23, 2015 at 12:45 PM 0
Share

Indeed. And I'd also point out that unless your objective is to end up with a mess, Unity is not all about cutting corners. That's a recipe for pain, regardless of whether you're in low level c++ or nice friendly unity. Not good advice.

avatar image mescalin wibble82 · Dec 23, 2015 at 12:50 PM 0
Share

AGREED!! lol, i will be using Hashsets for..well what they're for!

it would be nice to display Hashsets and Dictionaries in the editor but i fear dumping them out to debug lists for my inspection is the better way, for the sake of my time anyway. I personally might make some compiler directives to filter out this debug stuff for the final build

avatar image konyaevig · Sep 05, 2021 at 09:42 AM -1
Share

It's always funny to watch the Unity community come up with new hacks. And the developers of the engine cannot make elementary functionality.

Ordinary Unity team dev:

"Make SerializeField for Dictionary? Why?"

"Making the community suffer? This is what we need!"

avatar image spilat12 konyaevig · Sep 05, 2021 at 10:24 AM 1
Share

You just registered to leave that comment? If it's elementary, then surely you can implement that feature yourself?

avatar image
13

Answer by frarees · Oct 27, 2014 at 02:47 PM

Use ISerializationCallbackReceiver to let Unity serialize the keys and values of your dictionary. From there, you can access both lists and create your custom editor.

Comment
Add comment · Show 2 · 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 Helical · Oct 27, 2014 at 02:58 PM 2
Share

can you show an example?

avatar image frarees · Oct 27, 2014 at 03:04 PM 1
Share

You have an example on how to properly serialize a dictionary in the link I provided before. That will render the dictionary as two different lists, so it will be actually visible. For a editor/layout implementation, I let you investigate on your own :) (if you're starting editor scripting, this tutorial is a really good one)

avatar image
1

Answer by MakeCodeNow · Feb 17, 2014 at 11:15 PM

It's a PITA but you'd have to do the rendering yourself. Also note that Unity will not serialize/save/load dictionary for you. For this reason, a lot of people store things in Lists and then build a Dictionary at Start to make lookups faster.

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 EMOTION-THEORY · Feb 18, 2014 at 12:21 AM 1
Share

What does PITA mean?

avatar image shieldgenerator7 EMOTION-THEORY · Oct 03, 2020 at 06:27 AM 1
Share

Pain In The hAssle

avatar image MakeCodeNow · Feb 18, 2014 at 05:42 PM 3
Share

Sorry. Pain in the...butt.

avatar image tonytopper · Dec 09, 2021 at 08:47 PM 0
Share

I'd suggest building the dictionary using the ISerializationCallbackReceiver interface, not on Start.

avatar image
0

Answer by jamesbean · Jan 13 at 08:40 PM

I was just facing this problem. I tried some custom serialization, to find that it gives me trouble when switching to the HTML5 platform. I am not really savvy on serialization and its shenanigans, so I needed to implement a different solution.

In the end, my solution is a variation on the hack @Helical described:

  1. Using platform-dependent compilation, I declare a serialized key-value struct for the dictionary I want to fill, and an array of this struct. This struct, array, and the function to use them, will only exist for the editor.

  2. Create a scriptable object to contain the dictionary. This object I will be using in the game.

  3. Run an editor function that stores the data from the array into the dictionary in the scriptableObject. Since it is an asset, you will need to use the AssetDatabase class to 1) set it dirty before modifying it; 2) save the AssetDatabase, 3) reload the assetDatabase.

    The result is a visible list of items available in the editor, a plain old dictionary that will leave no extra trash and require no extra processing in the application, and an editor function that saves the first into the second.

Of couse, as some have mentioned, nothing stops you from putting duplicated key values into the array, so you need to be careful, or have the "Dumping" function check for errors like this. Other than that, I think it is a good compromise. This process of using editor-only variables and functions to process and store data into scriptableObjects has been pretty reliable to me, and I have been using it quite frequently with no problems.

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

33 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

Related Questions

Why can't a MonoBehaviour subtype with generic arguments be serialized in a list since Unity 4.5? 0 Answers

Populating list of custom class through inspector 1 Answer

Saving editor-only variables 0 Answers

Help with editor serialization 1 Answer

Custom Editor for Map/Dictionary in Unity 0 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