Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

Modify editor GUI content using mouse click 1 Answer

Editor GUI Foldout header style customization 0 Answers

Custom Material Editor 1 Answer

OnInspectorGUI - Using the default Object Selection popup. 1 Answer

How to make the camera zoom in editor work properly? (2D) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges