Unity and Dictionary Serializing

So ive been working for the past few days to come up with a work around for serializing dictionaries in unity to and getting it to show up in the inspector and thought id share what i came up with.

The LKey and LValue will keep the key value paring in two seperate list the unity can read.

 public class SerializeDictionary : MonoBehaviour, IDictionary
    {
        public SerializeDictionary()
        {
            Table = new Hashtable();
            LKeys = new List();
            LValues = new List();
        }
        [SerializeField]
        private List LKeys;
        [SerializeField]
        private List LValues;
        Hashtable Table;

        public TV this[TK key]
        {
            get
            {
                return (TV)Table[key];
            }
            set { }
        }

        public ICollection Keys { get { return Table.Keys.Cast().ToList(); } set { } }
        public ICollection Values { get { return Table.Values.Cast().ToList(); } set { } }

        public int Count { get { return Table.Count; } }

        public bool IsReadOnly { get { return false; } }

        public void Add(TK key, TV value)
        {
            Table.Add(key, value);
            LKeys.Add(key);
            LValues.Add(value);
        }

        public void Add(KeyValuePair item)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(KeyValuePair item)
        {
            throw new NotImplementedException();
        }

        public bool ContainsKey(TK key)
        {
            if (Table.Contains(key))
            {
                return true;
            }
            else { return false; }
        }

        public void CopyTo(KeyValuePair[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<KeyValuePair> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public bool Remove(TK key)
        {
            try
            {
                Table.Remove(key);
                var index = LKeys.IndexOf(key);
                LKeys.RemoveAt(index);
                LValues.RemoveAt(index);
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        public bool Remove(KeyValuePair item)
        {
            throw new NotImplementedException();
        }

        public bool TryGetValue(TK key, out TV value)
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}

works really good for what i need in order to display the class needs to be inherited or i get some wierd miss match error .


public class MyGameObject:SerializeDictionary
{

}

Leave a comment