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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Madigan · Jun 13, 2015 at 07:01 PM · editorserializationdictionary

Custom Editor for Map/Dictionary in Unity

I'm trying to build my own little ResourceManager in Unity that will allow me to tag resources (ranging from materials to prefabs) to a label in the editor, and then access them in game.

The ResourceManager itself is really easy to write- it's just a Dictionary. However the Unity Editor doesn't seem to be able to serialize (and thus render) Dictionaries.

To get around this, I created a list of key-value pairs which gets loaded into the dictionary by unity. That way Unity gets its list and I get my Dictionary. However, when I try and run the code shown below, I can only add a single entry to the list. Any idea what I'm doing wrong?

ResourceManager.cs

     using UnityEngine;
     using System.Collections.Generic;
     
     public class ResourceManager : MonoBehaviour, ISerializationCallbackReceiver {
         private Dictionary<string, Object> resources = new Dictionary<string, Object>();
     
         #region Unity
         // Unity doesn't know how to deal with maps, so we give it a list to work with
         public List<ResourceEntry> entries;
     
         // Dictionary -> List
         public void OnBeforeSerialize() {
             entries.Clear();
             foreach(string key in resources.Keys) {
                 entries.Add(new ResourceEntry(key, resources[key]));
             }
         }
     
         // List -> Dictionary
         public void OnAfterDeserialize() {
             resources = new Dictionary<string, Object>();
             foreach(ResourceEntry entry in entries) {
                 resources[entry.key] = entry.value;
             }
         }
     
         [System.Serializable]
         public class ResourceEntry {
             public string key;
             public Object value;
             
             public ResourceEntry() {}
             
             public ResourceEntry(string key, Object value) {
                 this.key = key;
                 this.value = value;
             }
         }
         #endregion
     
         private static ResourceManager instance;
         public static Object Get(string category, string key) {
             instance = instance ?? GameObject.FindObjectOfType<ResourceManager>();
             if( instance.resources.ContainsKey(key)) {
                 return instance.resources[key];
             } else {
                 return null;
             }
         }
     }
 
 ResourceManagerEditor.cs
 
     using UnityEngine;
     using System.Collections.Generic;
     using UnityEditor;
     using UnityEditorInternal;
     
     [CustomEditor(typeof(ResourceManager))]
     public class ResourceManagerEditor : Editor {
         private ReorderableList list;
     
         public void OnEnable() {
             list = new ReorderableList(serializedObject, serializedObject.FindProperty("entries"), true, true, true, true);
             list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
                 SerializedProperty element = list.serializedProperty.GetArrayElementAtIndex(index);
                 rect.y += 2;
                 EditorGUI.PropertyField(new Rect(rect.x, rect.y, 60, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("key"), GUIContent.none);
                 EditorGUI.PropertyField(new Rect(rect.x + 70, rect.y, 160, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("value"), GUIContent.none);
             };
             list.drawHeaderCallback = (Rect rect) => {
                 EditorGUI.LabelField(rect, "Resources");
             };
         }
     
         public override void OnInspectorGUI() {
             serializedObject.Update();
             list.DoLayoutList();
             serializedObject.ApplyModifiedProperties();
         }
     }



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

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

Dictionary in inspector 4 Answers

CustomEditor for 2 List to look like dictionary 1 Answer

Scriptable Object resets after Unity is closed 2 Answers

Dictionairy Serialization Problem: Keys and values get lost 0 Answers

Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject? 5 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