Is this okay to do or is there a better way? (Creating A Unity Serializable Dictionary)

Hey all I am creating my own dictionary class called "Table " which is an abstract class that I plan to inherit into other classes in order to implement property drawers for those. However since unity won’t serialize Dictionary, is it okay to build my class like this for serialization purposes? Or is there a better way to handle this?

(This Class is not quite complete yet but you get the idea :))

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public abstract class Table <TKey, TValue> {
	public byte[] bytes = new byte[0];
	Dictionary<TKey,TValue> dict = new Dictionary<TKey, TValue> ();
	public Dictionary<TKey,TValue> dictionary{
		get{
			if(dict.Count == 0 && bytes.LongLength > 0){
				Deserialize ();
			}
			return dict;
		}
		set{
			dict = value;
			Serialize ();
		}
	}

	public void Add(TKey key, TValue value){
		if (!ContainsKey (key)) {
			dictionary.Add (key, value);
			Serialize ();
			return;
		}
		throw(new System.Exception ("The Table Already Contains The Key \"" + key.ToString () + "\"."));
	}

	public TValue this[TKey key]{
		get{
			if(ContainsKey (key)){
				return dictionary[key];
			}
			throw (new System.Exception("The Key : \"" + key.ToString () + "\" Does Not Exist."));
		}
		set{
			if(ContainsKey (key)){
				dictionary[key] = value;
			}
			else{
				Add (key, value);
			}
			Serialize ();
		}
	}

	public bool ContainsKey(TKey key){
		return dictionary.ContainsKey (key);
	}

	void Serialize(){
		BinaryFormatter formatter = new BinaryFormatter ();
		MemoryStream stream = null;
		try{
			stream = new MemoryStream();
			formatter.Serialize (stream, dict);
			bytes = stream.ToArray ();
		}
		catch{
			bytes = new byte[0];
		}
		finally{
			if(stream != null)stream.Close ();
		}
	}
	void Deserialize(){
		BinaryFormatter formatter = new BinaryFormatter ();
		MemoryStream stream = null;
		try{
			stream = new MemoryStream(bytes);
			dict = (Dictionary<TKey, TValue>) formatter.Deserialize (stream);
		}
		catch{
			return;
		}
		finally{
			if(stream != null) stream.Close ();
		}
	}
}

Thanks In Advance!

I’m so lost… You’re implementing your own dictionary container but it…uses a dictionary? You should just use vexe’s implementation.

I was going to do it this way originally because I have had some issues with ISerializationCallbackReciever in the past regarding scriptable objects and it just seemed easier to have it Serialize to a byte[ ] when the values are added/changed
then deserialize when needed. But I was afraid the formatter and memory stream would use too much resource which is why I was asking.

vexe’s implementation seems confusing as I am a fairly new programmer but interesting all the same and I may try that instead. Thanks for that link!

What that also taught me is that GUI.Button will work for putting buttons on my prop. drawer. Where as before I thought I had to use EditorGUI (Which doesn’t have “Button”) so that’s pretty insightful I must say…

RickGamez <---- Total noob :stuck_out_tongue: