• 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
1
Question by spaceshooter · Dec 21, 2010 at 02:27 PM · guidrag

GUI Drag and Drop

I'd like to drag a texture from one GUI.Box to another GUI.Box. My initial thought is I can attach the texture to the mouse cursor using if(GUI.Box...) but I don't think that is the same as OnMouseDown. Past that, I'm not really sure how to make use of OnMouseDrag/OnMouseUp type functions in the GUI. Thanks.

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

2 Replies

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

Answer by Mike 3 · Dec 21, 2010 at 02:55 PM

You'll need to make extensive use of the Event class

Some things which will help:

Event.current.type will give you the EventType, which you will need to check for EventType.MouseDrag, EventType.MouseUp and EventType.MouseDown

Event.current.mousePosition you will need to find the current mouse position in gui space, relative to the current GUI.Group.

yourBoxRect.Contains(Event.current.mousePosition) will return true if your mouse cursor is inside the defined rect, also in GUI space, relative to the current group (so will always match with your box rect if you call it right before/after)

Besides that, just register if you mouse down inside the first box, then draw a texture during the drag event, and finally check if you mouse up on the second box

Comment
Add comment · Show 4 · 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 spaceshooter · Dec 21, 2010 at 03:23 PM 0
Share

Thanks! That makes sense, I just had to wrap my head around the fact that OnGui() is called for mouse events too. Should I redraw everything prior to handling events like On$$anonymous$$ouseDown?

avatar image Mike 3 · Dec 21, 2010 at 03:48 PM 0
Share

Yeah, don't worry too much about filtering the drawing depending on the event type - just make things happen when the event is fired

avatar image spaceshooter · Dec 21, 2010 at 05:44 PM 0
Share

I have it nearly working, but when I draw the texture to the mouse location, it ends up behind behind all of the other GUI elements.

avatar image Mike 3 · Dec 21, 2010 at 06:03 PM 0
Share

The code needs to be done at the end of the script to draw on top (or you need to specify GUI.depth if it's in another script)

avatar image
0

Answer by Berenger · Dec 21, 2010 at 02:59 PM

I don't know how, neither if, you can do it with OnMouse functions, but I can tell you how I Did it. Take look at this code

var firstBoxTexture : Texture2D; var secondBoxTexture : Texture2D; var currentTexture : Texture2D;

if( GUI.Box( rect, firstBoxTexture ) )
{
   // Drag
   if( firstBoxTexture != null )
   {
      currentTexture = firstBoxTexture;
      firstBoxTexture = null;
   }
   // Drop
   else
   {
      firstBoxTexture = currentTexture ;
      currentTexture = null;
   }
}
// Same thing for the other one, with secondBoxTexture

If you get the idea, it should work

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 spaceshooter · Dec 21, 2010 at 03:21 PM 0
Share

Thanks, but I will actually have a dozen or so places to drag and drop. Looks like the event system might be the way to go.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Activate function when GUI.Window is drag into area 0 Answers

GUI Note taking box 0 Answers

Set dragging boundaries for a self-made slider 1 Answer

What is the mathematical formula for GUI dragging ? 2 Answers

How to make a drag and drop menu using unityGUI 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