- Home /
Make sure only parent will be draggable.
Hello, I am working on Inventory/Character/Skill system. For now I have panel creation, Item generation etc. Now I am fighting with something.
I created Main Panel (215x215) Inside it another Panel with center align (215x180) And Inside (215x180) Panel I created 30 small Panels (34x34) using Grid Layout.
I want drag whole Panel (215x215) by clicking only that free area (look screen: http://screenshooter.net/102644289/ulpsrlr ). For now no matter where I click, (215x180) or (34x34) Pannels, everything is moving. How I can "block" dragging parent pannel by clicking child Panels? I want to put different script on that small (34x34) Pannels. I tried with block raycast etc. nothing helped.
Here is my dragging script attached to main Panel:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
Vector2 MousePos;
Vector2 NewPos;
public void OnBeginDrag(PointerEventData EventData)
{
MousePos = new Vector2(EventData.position.x,EventData.position.y);
MousePos.x = MousePos.x - EventData.pointerDrag.transform.position.x;
MousePos.y = MousePos.y - EventData.pointerDrag.transform.position.y;
Debug.Log (EventData.pointerDrag.name + "Started Drag.");
}
public void OnDrag(PointerEventData EventData)
{
NewPos = new Vector2(EventData.position.x, EventData.position.y);
NewPos.x = NewPos.x - MousePos.x;
NewPos.y = NewPos.y - MousePos.y;
EventData.pointerDrag.transform.position = NewPos;
}
public void OnEndDrag(PointerEventData EventData)
{
MousePos = Vector2.zero;
NewPos = Vector2.zero;
Debug.Log (EventData.pointerDrag.name + "Stopped Drag.");
}
}
Okey I figured that out by adding additional two invisible Panels on that positions and moving Draggable script on them. Then by using tags I drag their parent ($$anonymous$$ain Panel). But is there better way to solve that? $$anonymous$$ust be...
Your answer
Follow this Question
Related Questions
How to move UI image between panels 0 Answers
Drag and drop files from the assets folder to another folder not working 0 Answers
Help starting a card app 0 Answers
Duplicate Object, by dragging? (SporeCreatureCreator Like) 0 Answers
Drag, Hold, Catapult 0 Answers