- Home /
I can't get OnMouseDrag to work With GUI elements.
for some reason i can't get on mouse drag to work with GUI elements. i have a dimple dragg script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DraggScript : MonoBehaviour {
// userInputSimple user;
[SerializeField] Vector2 startPoint;
[SerializeField] Vector2 endPoint;
[SerializeField] bool drag;
void OnMouseDrag()
{
drag = true;
// user.dragging(false);
endPoint = Input.mousePosition;
// rend.material.color -= Color.white * Time.deltaTime;
}
void OnMouseUp() {
user.dragging(true);
drag =false;
}
void OnMouseDown() {
if(!drag){
startPoint = Input.mousePosition;
}
}
}
i should be able drop it on any guielement and it should work but onMouseDragg is never called on gui elements.
if you right-click in the scene hierarchy create new panel and drag this script onto the panel it should work?
-by default it wont be in the ignore raycast layer it is in the UI layer. -panels are gui elements i tried with buttons still did not work.
if i toss it on a plane then attach it to the camera it works but for some reason it does not work for GUIelements
even though in the documentation clearly states "This event is sent to all scripts attached to the Collider or GUIElement."
i am Missing something i dont know what it is.
Answer by masterhero · Jul 17, 2015 at 12:35 AM
You should use IDragHandler, IBeginDragHandler and IEndDragHandler interfaces for GUI. I tried to rewrite your code, it should work:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class DraggScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[SerializeField]
Vector2 startPoint;
[SerializeField]
Vector2 endPoint;
[SerializeField]
bool drag;
void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
{
startPoint = eventData.pressPosition;
}
public void OnDrag(PointerEventData eventData)
{
drag = true;
endPoint = eventData.position;
}
public void OnEndDrag(PointerEventData eventData)
{
drag = false;
}
}
thank you so much this was exactly what i was missing. here is the completed product drag it on to a panel, and a child panel set the child as the variable create a user input to handle selection from this scripts start and stop points by listing the on screen units.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;
public class DraggPanel : $$anonymous$$onoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler{
[SerializeField] userInputSimple user;
[SerializeField] Vector2 clickPosition;
[SerializeField] Vector2 currentPosition;
[SerializeField] Vector2 startPoint;
[SerializeField] Vector2 endPoint;
[SerializeField] bool drag;
[SerializeField] RectTransform DragImage;
[SerializeField] Vector2 $$anonymous$$anchor;
[SerializeField] Vector2 maxanchor;
[SerializeField] Vector2 v1;
[SerializeField] Vector2 v2;
public void OnPointerClick(PointerEventData eventData)
{
}
void IBeginDragHandler.OnBeginDrag(PointerEventData eventData){
startPoint = eventData.position;
clickPosition = eventData.position;
startPoint = eventData.position;
endPoint = eventData.position;
}
public void OnDrag(PointerEventData eventData){
drag = true;
endPoint = eventData.position;
currentPosition = eventData.position;
v1 = clickPosition;
v2 = currentPosition;
if(currentPosition.x < clickPosition.x){
v1.x = currentPosition.x;
v2.x = clickPosition.x;
}
if(currentPosition.y < clickPosition.y){
v1.y = currentPosition.y;
v2.y = clickPosition.y;
}
maxanchor = new Vector2(v2.x/ Screen.width, v2.y / Screen.height);
$$anonymous$$anchor = new Vector2(v1.x/ Screen.width, v1.y / Screen.height);
user.dragging(v1,v2,false);
DragImage.anchor$$anonymous$$in = $$anonymous$$anchor;
DragImage.anchor$$anonymous$$ax = maxanchor;
}
public void OnEndDrag(PointerEventData eventData){
drag = false;
user.dragging(v1,v2,true);
startPoint = new Vector2(0f,0f);
endPoint = new Vector2(0f,0f);
currentPosition = new Vector2(0f,0f);
$$anonymous$$anchor = new Vector2(0f,0f);
maxanchor = new Vector2(0f,0f);
DragImage.anchor$$anonymous$$in = new Vector2(0f,0f);
DragImage.anchor$$anonymous$$ax = new Vector2(0f,0f);
v1 = new Vector2(0f,0f);
v2 = new Vector2(0f,0f);
drag = false;
}
}
Answer by OmarMoya · Jul 17, 2015 at 12:07 AM
Yes, you are right, OnMouseDow and Up are called for GUIElements adn colliders, but not UnityEngine.UI elements... u have to use the EventSystem and EventTrigger Drag stuff to work with a drag inside a Canvas game object(or in other words UI game object).
Your answer
![](https://koobas.hobune.stream/wayback/20220611231635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
onMouseDrag scroll 0 Answers
slingshot style mouse mechanic, don't want inverted when player facing left 1 Answer
Dragging object in constrained area 1 Answer
addforce and onmousedrag ? 0 Answers