Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
2
Question by Koelho · Dec 03, 2014 at 11:14 PM · ui4.6draggable

C# - Draggable 4.6 UI?

How can i make an object of the new UI, like an button, panel or image draggable? That's for an inventory system, someone can help me with that? :)

Comment
Add comment · Show 1
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 Fattie · May 27, 2016 at 06:50 PM 0
Share

Complete solution for 2016 ...

http://stackoverflow.com/a/37473953/294884

extremely elegant, amazingly simple. Copy and paste.

3 Replies

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

Answer by Kiwasi · Dec 03, 2014 at 11:17 PM

  • Make your UI object

  • Choose Add Component -> Event -> Event Trigger

  • Add a new trigger, choose OnDrag

  • Add a listener and call the following script on the dragable object

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

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 cenk5355 · Feb 22, 2015 at 04:43 PM 0
Share

thanks,this working!!!

avatar image anniyan137 · Jul 12, 2015 at 01:34 PM 0
Share

Thanks a TON!!! $$anonymous$$ade my task SO much simpler.

avatar image Crixu · Aug 24, 2015 at 07:09 AM 0
Share

OnDrag doesn't appear anymore in the Event Trigger list, what would be the correct one now?

avatar image turbanov Crixu · Feb 03, 2016 at 07:53 PM 1
Share

"Drag" works for me.

avatar image
9

Answer by Justin L · Aug 24, 2015 at 11:40 AM

This works, but it breaks because it assumes the drag event was instigated by the mouse. For reference, here is the component I wrote:

 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(RectTransform))]
 public class PanelDragBehavior : MonoBehaviour
 {
     private RectTransform rect;
 
     public void Awake()
     {
         rect = GetComponent<RectTransform>();
     }
 
     public void OnDrag(UnityEngine.EventSystems.BaseEventData eventData)
     {
         var pointerData = eventData as UnityEngine.EventSystems.PointerEventData;
         if (pointerData == null) { return; }
 
 
         var currentPosition = rect.position;
         currentPosition.x += pointerData.delta.x;
         currentPosition.y += pointerData.delta.y;
         rect.position = currentPosition;
     }
 }
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 Crixu · Aug 24, 2015 at 07:05 PM 0
Share

Thank you for your reply, but I need an easy way to drag a GUI element, like you would do in Windows/OSX/Linux. I've tried some things already, but my knowledge of the new UI system and C# in general is not sufficient to come up with a good solution. If you, or anyone else, has a simple solution, or something that will bring me halfway, it'll be greatly appreciated!

Little note: I do not just copy-paste, but try to learn and understand everything I read and try! ;)

avatar image
0

Answer by prasanthvel · Nov 01, 2017 at 06:12 AM

Simply you can use LeanTouch Lean Dragabble UI script. It available for free in unity store.

Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Tips and Tricks Menu Help 1 Answer

(4.6 UI) How to detect mouse over on button? 3 Answers

Dragging UI elements in Screen Space canvas 1 Answer

how do i update specific Canvas Ui text? 1 Answer

Why button changes canvas group alpha value? 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