Hi, I tried to code the SerializableDictionary with the SerializableDictionaryElement, however in the inspector I am only able to add one element. The plus sign did nothing neather If I set the elements number manually . You have any idea of why it is happening ?
That's because when you click the plus sign, a new element is added that share the same key as the previous one, and dictionaries can't have duplicate keys, so it deletes itself. A way to solve it its to make a custom propertyDrawer (of an inherited class defining the generics, cause generics cant be serialized) and then you do this: if (GUI.Button(addButtonRect, "Add New Entry")) { SerializedProperty keyValuePairs = property.FindPropertyRelative("kvps"); //however you named the list keyValuePairs.InsertArrayElementAtIndex(keyValuePairs.arraySize); SerializedProperty newKeyValuePair = keyValuePairs.GetArrayElementAtIndex(keyValuePairs.arraySize - 1); SerializedProperty newKey = newKeyValuePair.FindPropertyRelative("key"); newKey.stringValue = "Key_" + keyValuePairs.arraySize; // Provide a default key value } Either way that just make it so you can change it from editor directly, but if you plan on making an editorWindow or you want to modify it anywhere else, values wont get propertly saved because of how ISerializationCallbackReceiver works.
@@dasmartkz Hey, unfortunately I removed this project few weeks ago so I cannot check what error will be thrown. Let me know what you get, maybe I will be able to help. However SerializedDataObject was created to show serialization inside editor but still it should be possible to serialize this component also, because I just show how to serialize specific types, wrapper class should not impact this.
Hi, I tried to code the SerializableDictionary with the SerializableDictionaryElement, however in the inspector I am only able to add one element. The plus sign did nothing neather If I set the elements number manually . You have any idea of why it is happening ?
Same issue
That's because when you click the plus sign, a new element is added that share the same key as the previous one, and dictionaries can't have duplicate keys, so it deletes itself. A way to solve it its to make a custom propertyDrawer (of an inherited class defining the generics, cause generics cant be serialized) and then you do this:
if (GUI.Button(addButtonRect, "Add New Entry"))
{
SerializedProperty keyValuePairs = property.FindPropertyRelative("kvps"); //however you named the list
keyValuePairs.InsertArrayElementAtIndex(keyValuePairs.arraySize);
SerializedProperty newKeyValuePair = keyValuePairs.GetArrayElementAtIndex(keyValuePairs.arraySize - 1);
SerializedProperty newKey = newKeyValuePair.FindPropertyRelative("key");
newKey.stringValue = "Key_" + keyValuePairs.arraySize; // Provide a default key value
}
Either way that just make it so you can change it from editor directly, but if you plan on making an editorWindow or you want to modify it anywhere else, values wont get propertly saved because of how ISerializationCallbackReceiver works.
Why you created SerializedDataObject and didn't show how to get json from it?
var serializedData = JsonUtility.FromJson(json);
Debug.Log(serializedData.customFields["Email"]); wiil be error
@@dasmartkz Hey, unfortunately I removed this project few weeks ago so I cannot check what error will be thrown. Let me know what you get, maybe I will be able to help. However SerializedDataObject was created to show serialization inside editor but still it should be possible to serialize this component also, because I just show how to serialize specific types, wrapper class should not impact this.
Beauty thank you sir.