GUI Editor Drag and Drop (Inspector)

Hi. I’m creating an custom Asset for Terrain, where I want to Drag and Drop Prefabs onto a GUI.Box.


Now my problem is that I’m making multiple Drag and Drop Boxes (see Code). Now I have a List> which stores which GameObjects are dragged in to the Tool.
If I’m working with simple One Group it shows perfectly every where. But if I create a new Group. The GameObjects which are dragged in are always in the first group… so that sucks and I don’t know exactly how I could make those DragAndDrop boxes standalone…


this is my Code:


	void DrawDragAndDrop(int g){

        GUIStyle GuistyleBoxDND = new GUIStyle(GUI.skin.box);
        GuistyleBoxDND.alignment = TextAnchor.MiddleCenter;
        GuistyleBoxDND.fontStyle = FontStyle.Italic; 
        GuistyleBoxDND.fontSize = 12;
        GUI.skin.box = GuistyleBoxDND;
        GuistyleBoxDND.normal.background = MakeTex( 2, 2, Color.white);

		Rect myRect = GUILayoutUtility.GetRect(0,20,GUILayout.ExpandWidth(true));
		GUI.Box(myRect,"Drag and Drop Prefabs to this Box!",GuistyleBoxDND);
		{
			if (Event.current.type == EventType.DragUpdated) {
				DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
				Debug.Log("Drag Updated!");
				Event.current.Use ();

			}   
               
            
			if (Event.current.type == EventType.DragPerform) {
				Debug.Log("Drag Perform!");
				Debug.Log(DragAndDrop.objectReferences.Length);
				for(int i = 0; i<DragAndDrop.objectReferences.Length;i++){
					myTarget.m_GameObjectGroups[g].Add(DragAndDrop.objectReferences *as GameObject);*
  •  		}*
    
  •  		Event.current.Use ();*
    
  •  	}*
    
  •  }*
    
  • }*

----------
DrawDragAndDrop(int g){…} is called for every Group in myTarget.m_GameObjectGroups…
And for Every GameObject myTarget.m_GameObjectGroups[g] I create a new EditorGUILayout.Objectfield.
Which as I said works for one Group perfectly but, after the first Group all GameObjects are Added in the first Group and not the one I intend to…
I think it’s because of the Event which starts… and I’m not creating an event foreach DragAndDrop field…
----------
might somebody is interested helping me. Thanks dan.

Well, you don’t seem to check the mouse position at all. You want to do something like:

Rect myRect = GUILayoutUtility.GetRect(0,20,GUILayout.ExpandWidth(true));
GUI.Box(myRect,"Drag and Drop Prefabs to this Box!",GuistyleBoxDND);
if (myRect.Contains(Event.current.mousePosition))
{
    if (Event.current.type == EventType.DragUpdated)
    {
        DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
        Debug.Log("Drag Updated!");
        Event.current.Use ();
    }   
    else if (Event.current.type == EventType.DragPerform)
    {
        Debug.Log("Drag Perform!");
        Debug.Log(DragAndDrop.objectReferences.Length);
        for(int i = 0; i<DragAndDrop.objectReferences.Length;i++)
        {
            myTarget.m_GameObjectGroups[groupIndex].Add(DragAndDrop.objectReferences *as GameObject);*

}
Event.current.Use ();
}
}
Finally note that you have a parameter called “i” but you also declared a variable “i” inside your for loop. That’s not possible. You have to rename one of them. I’m just guessing here since we don’t know how the rest of your code looks like but i guess this “i”
myTarget.m_GameObjectGroups*.
should be your method parameter while this “i”
.Add(DragAndDrop.objectReferences as GameObject);
should be your for loop variable.
Finally i would recommend to first check if the object the user dragged actually is a GameObject or not. Something like that:
for(int i = 0; i<DragAndDrop.objectReferences.Length;i++)
_{
GameObject go = DragAndDrop.objectReferences as GameObject;
if (go != null)
myTarget.m_GameObjectGroups[groupIndex].Add(go);
}*_