How to serialize Dictionary and DateTime in Unity without external libraries

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ม.ค. 2025

ความคิดเห็น • 7

  • @clouleal
    @clouleal 8 หลายเดือนก่อน +1

    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 ?

    • @vamidicreations
      @vamidicreations 8 หลายเดือนก่อน

      Same issue

    • @SantaGrapa
      @SantaGrapa 6 หลายเดือนก่อน +1

      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
    @dasmartkz 11 หลายเดือนก่อน

    Why you created SerializedDataObject and didn't show how to get json from it?

    • @dasmartkz
      @dasmartkz 11 หลายเดือนก่อน

      var serializedData = JsonUtility.FromJson(json);
      Debug.Log(serializedData.customFields["Email"]); wiil be error

    • @blazeDev_vlog
      @blazeDev_vlog  11 หลายเดือนก่อน

      @@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.

  • @DigitomProductions
    @DigitomProductions ปีที่แล้ว

    Beauty thank you sir.