• 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
2
Question by kurukshetran · Dec 30, 2014 at 02:55 PM · dragdropnew ui

Unity new ui Drag and drop

Hi all, i am using unity's new ui to implement drag and drop feature for learning.The problem i am facing is that when i drag a image out of placeholder and drop on empty space it sticks to that as in image. alt text

But if i drag another image and drop it on above any five slots, it successfully placed.Also if i swap two images, one goes to below of that image.Like t$$anonymous$$s, alt text

What i am trying to do is to implement like card games like solitaire.Here is the code. T$$anonymous$$s script goes to image objects like in A, B etc.

public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {

 public static GameObject itemBeingDragged;
 Vector3 startPosition;
 Transform startParent;

 #region IBeginDragHandler implementation

 public void OnBeginDrag (PointerEventData eventData)
 {
     itemBeingDragged = gameObject;
     startPosition = transform.position;
     startParent = transform.parent;
     GetComponent<CanvasGroup> ().blocksRaycasts = false;
 }

 #endregion

 #region IDragHandler implementation

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

 #endregion

 #region IEndDragHandler implementation

 public void OnEndDrag (PointerEventData eventData)
 {
     itemBeingDragged = null;

     if(transform.parent != startParent)
     {
         transform.position = startPosition;
     }

     GetComponent<CanvasGroup> ().blocksRaycasts = true;
 }

 #endregion

}

t$$anonymous$$s script goes to placeholders

public class Slot : MonoBehaviour, IDropHandler {

 public GameObject item
 {
     get
     {
         if(transform.c$$anonymous$$ldCount > 0)
         {
             return transform.GetC$$anonymous$$ld(0).gameObject;
         }
         else
         {
             return null;
         }
     }
 }

 #region IDropHandler implementation
 public void OnDrop (PointerEventData eventData)
 {
     if(!item)
     {
         DragHandler.itemBeingDragged.transform.SetParent(transform);
     }
 }
 #endregion

}

image1.png (136.6 kB)
2.png (138.4 kB)
Comment
Add comment · Show 3
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 Glurth · Dec 30, 2014 at 04:00 PM 0
Share

Perhaps you simply need to check, when the item is dropped, if the mouse is in fact over a SLOT (as opposed to say a PANEL, or nothing). If not, return to original position. Or am I missing that logic somehow? Also, after you do transform.SetParent, don't you need to set the child's transform to 0,0 (or half_slot_size_x,half_slot_size_y)- to get he proper offset from the parent?

avatar image Kiwasi · Dec 30, 2014 at 07:45 PM 0
Share

I've seen this code before :). The code looks identical to mine, so I think its likely to be a set up problem in the scene, rather then a code problem.

Is the set up on all of the slots the same? I'm puzzled as to why it would work with the top slots but not the bottom ones

You might be able to fix the going behind issue adjusting the z position on your objects with the DragHandler script.

avatar image kurukshetran · Dec 31, 2014 at 01:19 AM 0
Share

no..on both top and bottom slots, this didnt work...yeah i have been following your youtube video to learn unity ui.Thanks..let me check scene.

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by bkachmar · Jun 11, 2015 at 09:10 AM

T$$anonymous$$s tutorial can help you =)

https://www.youtube.com/watch?v=c47QYgsJrWc

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
avatar image
0

Answer by JesperWH · Oct 11, 2016 at 12:13 PM

Line 31 should be if(transform.parent == startParent)

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
avatar image
0

Answer by Creer_LLC · Mar 08, 2017 at 02:53 PM

@BoredMormon Please what if i want to swap the old tile for the new one i.e the tile dragged by the mouse can be dropped on an occupied space and the tile currently occupying the space would move to the default position of the new tile.

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
avatar image
0

Answer by walidabazo · Dec 30, 2017 at 01:48 AM

Very good tutorial to how drag and drop and matc$$anonymous$$ng object

https://youtu.be/H29K5crl7zM

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
avatar image
0

Answer by moaZZama · Oct 12, 2022 at 04:18 PM

my problem is the same but slightly different I want to change the image to another image on the on-drop function, I am using the same card script that t$$anonymous$$s guy used it.

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.

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

9 People are following this question.

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

Related Questions

How to move UI image between panels 0 Answers

UI eventsystem Raycast Unity 5 does not recognize parent and child 1 Answer

Drag and Drop Inside Editor 0 Answers

How do I mimic dragging and dropping a FBX to the hierarchy window with an editor script? C# 1 Answer

Drag & Drop & Arrange 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