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 zina-design · May 22, 2015 at 02:53 PM · c#drag-and-drop

drag and drop in specific area Unity

I want to implement a drag and drop of an image but in spesific area as this picture alt text

so when someone drag for example the picture of the "orange" and drop it on the area "Apple" it will not work and the picture "oragne" come back to its place , but if he drop it on the area "orange" it will work ..

I have a code that drag and drop an image in any place which is not my aim , can someone help me to add some code to drop the right image on the right area please ??

DragHandeler.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class DragHandeler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
 public static GameObject itemBeingDraged;
 Vector3 startPosition;
 Transform startParent;
 
 #region IBeginDragHandler implementation
 public void OnBeginDrag (PointerEventData eventData)
 {
     itemBeingDraged = 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)
 {
     itemBeingDraged = null;
     GetComponent<CanvasGroup>().blocksRaycasts = true;
     if (transform.parent != startParent) {
         transform.position = startPosition;
     }
 }
 
 #endregion
 }

inventory.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class inventory : MonoBehaviour, IHasChanged {
 
 [SerializeField] Transform slots;
 [SerializeField] Text inventoryText;
 
 // Use this for initialization
 void Start () {
     HasChanged ();
 }
 
 #region IHasChanged implementation
 
 public void HasChanged ()
 {
     System.Text.StringBuilder builder = new System.Text.StringBuilder();
     builder.Append (" - ");
     foreach (Transform slotTransform in slots) {
         GameObject item = slotTransform.GetComponent<Slot>().item;
         if (item){
             builder.Append (item.name);
             builder.Append (" - ");
         }
     }
     inventoryText.text = builder.ToString ();
 }
 
 #endregion
 }
 
 namespace UnityEngine.EventSystems {
 public interface IHasChanged : IEventSystemHandler {
     void HasChanged();
 }
 }

Slot.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class Slot : MonoBehaviour, IDropHandler {
     public GameObject item {
         get{
             if(transform.childCount>0){
                 return transform.GetChild(0).gameObject;
             }
             return null;
         }
 }
 
 #region IDropHandler implementation
 
 public void OnDrop (PointerEventData eventData)
 {
     if (!item) {
         DragHandeler.itemBeingDraged.transform.SetParent (transform);
         ExecuteEvents.ExecuteHierarchy<IHasChanged>(gameObject,null,(x,y) => x.HasChanged ());
     }
 }
 
 #endregion
 }

Thanx ^^

untitled-1.png (58.7 kB)
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 buhle · Sep 29, 2015 at 09:13 AM 0
Share

hi zina i also have the same problem did u eventually get a solution pls help me out

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by gordonkoehn · May 23, 2015 at 07:51 AM

I havend looked at your code, but take a look on this one.

Unity UI Sampel: https://www.assetstore.unity3d.com/en/#!/content/25468

It's a free sampel for the new UI system.

There is one scene with a Drag and Drop view.

Gordon

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
1

Answer by MadDevil · Sep 29, 2015 at 10:36 AM

@ zina-design, there are many ways to do what you want.

1) you can use OnCollisionEnter or OnTriggerEnter. 2) you can use bounds and check whether the object is inside the bounds so that you can drop object at its particular place.

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 buhle · Sep 29, 2015 at 12:28 PM 0
Share

i'm also struggling with the same thing can you pls send me an example of code

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to add a score when the correct object drags to the correct box? 1 Answer

Drag and Drop on RTS camera 1 Answer

Renderer on object disabled after level reload 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