How to make copy/paste for PropertyDrawer

I am trying to make a copy/paste context menu for a custom PropertyDrawer. i couldn’t find something native api to do it.
I found how to add ContextualMenuManipulator and EditorGUIUtility.systemCopyBuffer to save/load from the buffer. But how can I serialize/deserialize SerializedProperty in json like this:

GenericPropertyJSON:{"name":"NotTags","type":-1,"arraySize":3,"arrayType":"int","children": [{"name":"Array","type":-1,"arraySize":3,"arrayType":"int","children":[{"name":"size","type":12,"val":3},{"name":"data","type":0,"val":2},{"name":"data","type":0,"val":9},{"name":"data","type":0,"val":5}]}]}

Have you checked JsonUtility ?

1 Like

JsonUtility and EditorJsonUtility return:

{}

Check that you are following the Serialization Rules (both “Serailization Rules” and “Serialization of custom classes” chapters )

Serialization works with SerializedProperty.boxedValue. But looks different (without metadata about property type and name).

That is because you are serializing the “value” of the property, not the property itself. I dont think you really want the “name” and “type” data.

As i see it, a Copy/Paste for a PropertyDrawer would copy the “value” of the property, so you can paste it onto another property of the same type (not onto the “same” property, of another instance).

So

public class Foo {
     public int someInt;
     public float someFloat;
}

would produce (ill give it some random values):

{
   "someInt" = 1,
   "someFloat" = 3.14
}

What i think you are saying, is that if you have some type that holds an instance of Foo

public class SomeImportantType
{
    public Foo myFooProperty;
}

and seems that you want the “Copy·” to produce something like

"myFooProperty" = {
   "type" = "MyNamespace.Foo, MyAssembly",
   "properties" = {
       "someInt" = 1,
       "someFloat" = 3.14
    }
}

But first, this would not be valid Json, because there is no { } wrapper, and if there were, then that json would represent "An object with a ‘myFooProperty’ " of type Foo with this fields.

But do you really need all this?

If you just go for the first approach (just serializing the inner properties), and copy that to the systemCopyBuffer. Then for “Paste” you would check the type you are pasting into, and see if the Json can produce a value of that type, like

public bool IsJsonOfType<T>(string myJsonString)
{
   T foo;
   try{
      foo = JsonUtility.FromJson<T>(myJsonString);
      return foo != null;
   }
   catch{
       // the contents of the "myJsonString" are not a T object or not even valid Json
      return false;
    }
}