- Home /
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
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 ^^
hi zina i also have the same problem did u eventually get a solution pls help me out
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
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.
i'm also struggling with the same thing can you pls send me an example of code
Your answer
Follow this Question
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