- Home /
IDragHandler goes through buttons
When I'm trying to use IDragHandler's OnDrag on my 'Popup' gameObjcet, it also gets called when I'm dragging a button:
I could fix it by adding a bool and setting it to true when OnPointerDown
is called and false when OnPointerUp
is called and check it when OnDrag gets called, this is my code:
using UnityEngine; using UnityEngine.EventSystems; public class DragAndDrop : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { private Canvas mainCanvas; private CanvasGroup canvasGroup; private RectTransform rectTransform; bool selectedDirectly = false; void Awake() { mainCanvas = GameObject.FindGameObjectWithTag("MainCanvas").GetComponent<Canvas>(); canvasGroup = gameObject.GetComponent<CanvasGroup>(); rectTransform = gameObject.GetComponent<RectTransform>(); } public void OnPointerDown(PointerEventData eventData) { selectedDirectly = true; LeanTween.scale(gameObject, new Vector3(1.09f, 1.09f, 1.0f), 0.2f); } public void OnPointerUp(PointerEventData eventData) { selectedDirectly = false; LeanTween.scale(gameObject, new Vector3(1.0f, 1.0f, 1.0f), 0.2f); } public void OnDrag(PointerEventData eventData) { if(selectedDirectly) rectTransform.anchoredPosition += eventData.delta / mainCanvas.scaleFactor; } }
I wanted to know if there is a better and cleaner way to do this without needing to add a bool and why is OnPointerDown can differentiate between the Popup and the BtnOk/Cancel but OnDrag can't.
Your answer
Follow this Question
Related Questions
Difference between EventType.ContextClick and EventType.MouseDown with Event.button == 1? 1 Answer
Creating a custom slider - should I be invoking a UnityEvent onValueChanged? 0 Answers
UI: Mouse events not working 1 Answer
Is the new UI system still not recommended for mobile? (Unity 5) 1 Answer
Ui Elements and Unity Events 1 Answer