TLDR; How do you drop an object <T>
from the heirarchy onto a List<X>
in the inspector, where <X>
is a custom class that contains one <T>
and two integers?
I have a custom class <Holder>
.
The class has a GameObject parameter and two integer parameters.
I have a List<Holder>
object that I can see in the inspector.
In the heirarchy, I have a bunch of GameObjects.
I wish to be able to select one or multiple of the GameObjects in the heirarchy and drag them to the List<Holder>
parameter in the inspector, drop them, and have the List<Holder>
add as many elements as necessary to hold all of the dragged GameObjects, and then store the dropped GameObjects in the GameObject parameter of each <Holder>
element, and populate each of the index values of each <Holder>
with a default value (probably zero).
The attached image is the end result that I am trying to achieve. G Panel is the GameObject of the <Holder>
class.
I’ve made SOME progress towards this so far, but I’m unsure if I’m approaching it from the correct direction, or even if my goal is possible. I have managed to write an editor script (below) that detects when I DragAndDrop one or many objects from the heirarchy and can get the instance of the gameObject they’re being dropped onto. But I can’t figure out how to allow GameObjects to be dropped onto a List<Holder>
. The cursor turns in a cancel symbol and there are seemingly no Events triggered when they’re dropped onto a list of incompatible type.
My thoughts are that I need to override the default functionality of dragging and dropping onto a List<T>
, and check if the DROPPED type is GameObject and the LIST type is List<Holder>
, then do custom assignment. But that’s an uninformed guess and there seems to be no indication that that’s possible. So I’m left scratching my head here as to what the first step is in trying to add drag and drop functionality to a List<T>
where <T>
is a custom type and the dropped object is of a different type.
If anyone can help me out, that’d be handy. Script so far:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ButtonBehaviors))]
[CanEditMultipleObjects]
public class newEditorScript : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EventType eventType = Event.current.type;
if (eventType == EventType.DragUpdated ||
eventType == EventType.DragPerform)
{
// Show a copy icon on the drag
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (eventType == EventType.DragPerform)
{
ButtonBehaviors bb = (ButtonBehaviors)target;
Debug.Log(bb.gameObject.name);
Debug.Log(DragAndDrop.objectReferences.Length);
DragAndDrop.AcceptDrag();
}
Event.current.Use();
}
}
}
In the above code, ButtonBehaviors is the class/script that has the List<Holder>
. (Holder is actually called something else, but for the example I just used “Holder”)