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 antonkudin · Dec 06, 2014 at 06:30 AM · buttondrageventdrag-and-dropunity4.6

Unity 4.6: Drag and drop

Is it possible to make drag&drop inventory with new UI system, with new tools?

If not, is there a way to detect the event when user presses on a button? All i can do right now is detect when button is let go (using Button (script) / On Click () fuctionality), not pressed down.

(csharp code only please!)

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
3
Best Answer

Answer by Kiwasi · Dec 06, 2014 at 06:34 AM

Sure. Add an event trigger. Add a new event. Choose OnDrag. Add a listener that calls your Drag method. Typically a drag method would look like this.

 public void Drag(){
     transform.position = Input.mousePosition;
 }

Edit: That method is rather hackish, there are plenty of issues with it. Here is a better one.

Add using UnityEngine.EventsSystem.

Implement the interfaces for IBeginDrag, IDragHandler and IEndDrag.

Set the mouse position as above inside the IDragHandler method.

You can also use IDropHandler to figure out what you have dropped the object on.

Video tutorial, also has a few other relevant tricks.

Comment
Add comment · Show 6 · 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 antonkudin · Dec 06, 2014 at 12:12 PM 0
Share

It will transmit the start of the drag, but not the end. So I went from there: I made couple more event triggers for "pointer enter" and "pointer up": to detect if i dragged something on to another button. It's hacky, but sort of works.

Wondering if there's a better way?

avatar image Kiwasi · Dec 06, 2014 at 08:57 PM 0
Share

I think there is an OnDrop event or similar in the event system.

avatar image antonkudin · Dec 06, 2014 at 09:26 PM 0
Share

you're a life saver :)

avatar image Kiwasi · Dec 12, 2014 at 08:10 PM 0
Share

So there is a much better way. I'll edit my answer with a linked video.

avatar image antonkudin · Dec 12, 2014 at 09:12 PM 0
Share

Cool stuff, learned a lot. Thanks!

I've implemented something already, its a bit hacky, since i'm new to program$$anonymous$$g. I'm sure in next version I'm going to do something like you've shown.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you make a button trigger an OnBeginDrag? 0 Answers

Initiating a Drag event by code 1 Answer

Instantiate and Scale with Mouse Click. 1 Answer

How to disable button OnClick when drag on the screen with OnMouseDrag 2 Answers

Event system or UI Button ? 0 Answers


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