• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by komodor · Mar 07, 2014 at 03:09 PM · guieditoreditorwindoweditorguidraganddrop

DragAndDrop EditorGUILayout.Box ?

is it possible to somehow make DragAndDrop functionality for boxes in EditorGUI ? i'd like to make nodes but i can't figure the dragndrop part

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by vexe · Mar 07, 2014 at 03:19 PM

Here, I use these everywhere.

 public static void DragDropArea<T>(string label, Action<T> onDrop, Action onMouseUp, float h = 25) where T : Object
 {
     HorizontalBlock(() =>
     {
         // draw a box area for our drag-drop
         GUILayout.Space(15f);
         Rect dropArea = GUILayoutUtility.GetRect(0, h);
         GUI.Box(dropArea, label);

         // cache the current event
         Event currentEvent = Event.current;

         // if our mouse isn't contained within that box area, exit out
         if (!dropArea.Contains(currentEvent.mousePosition)) return;

         if (onMouseUp != null)
             if (currentEvent.type == EventType.MouseUp)
                 onMouseUp();

         if (onDrop != null) {
             if (currentEvent.type == EventType.DragUpdated ||
                 currentEvent.type == EventType.DragPerform) {

                 // set the visual mode to copy
                 DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                 // if we dropped something
                 if (currentEvent.type == EventType.DragPerform) {

                     // register that this drag-drop event has been handled by this control
                     DragAndDrop.AcceptDrag();

                     // loop over the dropped items
                     foreach (var item in DragAndDrop.objectReferences) {
                         onDrop(item as T);
                     }
                 }
                 // since we've used the DragPerform event, we'll mark it as used
                 // (its type will change to EventType.Used)
                 // so that other controls ignore it
                 Event.current.Use();
             }
         }
     });
 }

Use these if you want manually register drag/drop for a custom field (Have a read at the DragAndDrop class to see what the methods do)

 /// <summary>
 /// Registers fieldRect for drop operations.
 /// sets the fieldValue to whatever that's been dropped, returns fieldValue in either cases.
 /// (could have used a ref parameter instead of returning the field)
 /// </summary>
 public static T RegisterFieldForDrop<T>(Rect fieldRect, T fieldValue, Func<Object[], Object> getDroppedObject) where T : Object
 {
     Event e = Event.current;
     EventType eType = e.type;
     if (fieldRect.Contains(e.mousePosition) && eType == EventType.DragUpdated || eType == EventType.DragPerform) {
         DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
         if (eType == EventType.DragPerform) {
             DragAndDrop.AcceptDrag();
             fieldValue = getDroppedObject(DragAndDrop.objectReferences) as T;
             Event.current.Use();
         }
     }
     return fieldValue;
 }

 /// <summary>
 /// Registers fieldRect for drag operations. dragObject is what's being dragged out of that field.
 /// </summary>
 public static void RegisterFieldForDrag(Rect fieldRect, Object dragObject)
 {
     if (dragObject == null) return; // can't drag a null wtf!
     Event e = Event.current;
     if (fieldRect.Contains(e.mousePosition) && e.type == EventType.MouseDrag) {
         DragAndDrop.PrepareStartDrag();
         DragAndDrop.objectReferences = new[] { dragObject };
         DragAndDrop.StartDrag("drag");
         Event.current.Use();
     }
 }

 public static void HorizontalBlock(Action block)
 {
     GUILayout.BeginHorizontal();
     block();
     GUILayout.EndHorizontal();
 }

See this video of mine to see what I mean by "registering for a drag"

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zombience · Sep 02, 2014 at 06:07 AM 0
Share

thank you! these are great!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editor GUI Foldout header style customization 0 Answers

Modify editor GUI content using mouse click 1 Answer

Detect when the Build button was pressed 2 Answers

Horizontal Line 11 Answers

Arrow button-ish GUI control? (it's in Mecanim) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges