- Home /
The question is answered, right answer was accepted
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[i] 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.
Answer by Bunny83 · Aug 31, 2018 at 09:37 AM
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[i] 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[i].
should be your method parameter while this "i"
.Add(DragAndDrop.objectReferences[i] 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[i] as GameObject;
if (go != null)
myTarget.m_GameObjectGroups[groupIndex].Add(go);
}
Perfect. Thank you. I did infect wit the parameter of the groupIndex. =)
void DrawDragAndDrop(int m){
Event evt = Event.current;
GUIStyle GuistyleBoxDND = new GUIStyle(GUI.skin.box);
GuistyleBoxDND.alignment = TextAnchor.$$anonymous$$iddleCenter;
GuistyleBoxDND.fontStyle = FontStyle.Italic;
GuistyleBoxDND.fontSize = 12;
GUI.skin.box = GuistyleBoxDND;
GuistyleBoxDND.normal.background = $$anonymous$$akeTex( 2, 2, Color.white);
Rect dadRect = new Rect();
dadRect = GUILayoutUtility.GetRect(0,20,GUILayout.ExpandWidth(true));
GUI.Box(dadRect,"Drag and Drop Prefabs to this Box!",GuistyleBoxDND);
if (dadRect.Contains(Event.current.mousePosition))
{
if (Event.current.type == EventType.DragUpdated)
{
DragAndDrop.visual$$anonymous$$ode = DragAndDropVisual$$anonymous$$ode.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[m].Add(DragAndDrop.objectReferences[i] as GameObject);
}
Event.current.Use ();
}
}
}
Follow this Question
Related Questions
Is there an event being fired off when the Inspector is being resized? 1 Answer
OnInspectorGUI - Using the default Object Selection popup. 1 Answer
Editor GUI Like mesh import animation inspector 0 Answers
Unexpected selection state in custom inspector? 1 Answer
Prefix label greyed out when following component disabled 1 Answer