Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example Drag & Drop area in a custom inspector for the Unity editor
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor (typeof(TriggerContainer))]
public class TriggerContainerEditor : Editor
{
private SerializedObject obj;
public void OnEnable ()
{
obj = new SerializedObject (target);
}
public override void OnInspectorGUI ()
{
DrawDefaultInspector ();
EditorGUILayout.Space ();
DropAreaGUI ();
}
public void DropAreaGUI ()
{
Event evt = Event.current;
Rect drop_area = GUILayoutUtility.GetRect (0.0f, 50.0f, GUILayout.ExpandWidth (true));
GUI.Box (drop_area, "Add Trigger");
switch (evt.type) {
case EventType.DragUpdated:
case EventType.DragPerform:
if (!drop_area.Contains (evt.mousePosition))
return;
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (evt.type == EventType.DragPerform) {
DragAndDrop.AcceptDrag ();
foreach (Object dragged_object in DragAndDrop.objectReferences) {
// Do On Drag Stuff here
}
}
break;
}
}
}
@serkber

This comment has been minimized.

Copy link

serkber commented Dec 21, 2017

I love you

@YoungXiang

This comment has been minimized.

Copy link

YoungXiang commented Jun 2, 2018

Couldn't love you more.

@zombience

This comment has been minimized.

Copy link

zombience commented Jun 22, 2018

Everyone, everyone... just calm down. I love bzgeb more than any of you.

@DuffyScottC

This comment has been minimized.

Copy link

DuffyScottC commented Jul 22, 2018

Thank you for this. You have saved me hours--no DAYS of searching.

@3Dmajid

This comment has been minimized.

Copy link

3Dmajid commented Aug 2, 2018

Thanks a world !!

@o2deprived

This comment has been minimized.

Copy link

o2deprived commented Sep 24, 2018

@bzgeb - thank you for this!

For anyone experiencing issues using this method to capture and process Sprite types (or any other type that does not properly cast from Object, try using DragAndDrop.paths and then process using AssetDatabase.LoadResources<Type>(path). Refer to this forum post: https://stackoverflow.com/questions/39474689/how-to-drag-and-drop-multiple-sprites-in-inspector

I had to do this to properly collect and cast Sprites dropped on the field.

@keenanwoodall

This comment has been minimized.

Copy link

keenanwoodall commented Oct 19, 2018

You're a legend! Is there a way to only draw the box if a drag is occuring?

@CSaratakij

This comment has been minimized.

Copy link

CSaratakij commented May 15, 2019

I love you~

@quizcanners

This comment has been minimized.

Copy link

quizcanners commented May 15, 2019

Guys, what are you turning this comment section into! Can you please be serious?

bzgeb My love for you is like the raging sea,
So powerful and deep it will forever be.
Through storm, wind, and heavy rain,
It will withstand every pain.

@shielf

This comment has been minimized.

Copy link

shielf commented Apr 2, 2020

Oh I love you so much!

@shielf

This comment has been minimized.

Copy link

shielf commented Apr 2, 2020

@bzgeb - thank you for this!

For anyone experiencing issues using this method to capture and process Sprite types (or any other type that does not properly cast from Object, try using DragAndDrop.paths and then process using AssetDatabase.LoadResources<Type>(path). Refer to this forum post: https://stackoverflow.com/questions/39474689/how-to-drag-and-drop-multiple-sprites-in-inspector

I had to do this to properly collect and cast Sprites dropped on the field.

Love you too!!!

@magiquep

This comment has been minimized.

Copy link

magiquep commented May 16, 2020

This is nice, but how do you get a reference to the component being dragged instead of the entire object? For example, I want to drag a specific MonoBehavior component attached to the object so that I can add it to a list of behaviors to enable or disable, but the dragged_object only references the GameObject that the behavior is attached to.

@bzgeb

This comment has been minimized.

Copy link
Owner Author

bzgeb commented May 17, 2020

Could you call GetComponent on the dragged game object?

@Elijahdanie

This comment has been minimized.

Copy link

Elijahdanie commented May 19, 2020

Thanks a big Bunch!!!!

@Elijahdanie

This comment has been minimized.

Copy link

Elijahdanie commented May 19, 2020

This is nice, but how do you get a reference to the component being dragged instead of the entire object? For example, I want to drag a specific MonoBehavior component attached to the object so that I can add it to a list of behaviors to enable or disable, but the dragged_object only references the GameObject that the behavior is attached to.

How about Getting the GameObject and then call Getcomponent on it.
Gameobject obj = dragged_object as GameObject;
T type = obj.GetComponent< T >();

Give it a try

@magiquep

This comment has been minimized.

Copy link

magiquep commented May 19, 2020

No, GetComponent can't be called on Object. Anyway, I figured out my original issue which led me to try this solution so I don't need it anymore.

@Idle1man

This comment has been minimized.

Copy link

Idle1man commented Jun 15, 2020

goodWork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.