create "draggable reference" within custom inspector?

To illustrate, I’ll share a screenshot of the custom inspector I’m working on.

[8629-custom_editor.jpg*|8629]

I’m making logical groupings of DetailPrototype objects to paint to the terrain together at runtime. This uses two custom classes, VeggieGroups, and VeggieUnits.

Currently a VeggieUnit just has an index value referring to the DetailPrototype it represents in terrainData.detailPrototypes, and a density value to use when painting.

I’m currently manually keying in the integer “index” value of a DetailPrototype object into instances of VeggieUnit objects. The whole point of a custom inspector in the first place was to avoid this, but I’m still not sure how. I can’t seem to create any user-friendly alternative to keying integers, such as exposing the represented DetailPrototype object so I can “drag” the reference to a VeggieUnit’s DetailPrototype property.

What are my options for intuitively giving my custom class instances a reference to these DetailPrototype objects? Thanks a million,

Edit
For your consideration, the following ObjectField won’t compile, giving “can’t convert DetailPrototype to Object” or, through variations on conversion methods, “can’t convert Object to DetailPrototype”

	tvm.customDetailPrototypes *= EditorGUILayout.ObjectField(*
  •  "prototype:",*
    

_ tvm.customDetailPrototypes*,_
_
typeof(DetailPrototype),_
_
true)_
_
as DetailPrototype;_

I know the question is 3.5+ years old but since it got bumped already …

The ObjectField only works for classes which are derived from UnityEngine.Object. Such classes are always serialized seperately as assets or as instances inside a scene. References to such objects persist since they have their own instanceID.

“DetailPrototype” on the other hand is just an ordinary data class. Such data classes aren’t serialized on their own. Unity simply serializes the variables in those classes as sub-fields of the MonoBehaviour that references them. So there actually isn’t a serializable reference to such classes. Those instances shouldn’t be seen as seperate objects but as part of the script instance.

So if you want to store additional data for each DetailPrototype that exists inside the terrain data, you have only limited options. The most straight forward way would be, as you currently do, is to simply use the index of each item. This of course will give you problems when you add or remove items as this will mess up the indices. You could additionally store serializable references that the DetailPrototype instance holds (like the prototype gameobject reference or the protptypeTexture) inside your own “extension” class in a hidden field. That way you can match your extension data with the actual instance based on those references. This would allow to detect when items got removed, added or changed.

So you still would use indices but additional use the other information to verify it’s still the right object.

If the question was more about how to initially reference those DetailPrototype instances there’s no ready-to-use solution. Unity’s built-in DragAndDrop support only works for serialized assets. So you can’t “drag” them over from the terrain.

What you could do is display a tiny preview of all DetailPrototypes available in the terrain data inside your own inspector and add drag and drop support for them manually. I suggest you download ILSpy and look at the ObjectField code as well as