Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 buckius82 · Jul 16, 2015 at 09:42 PM · onmousedrag

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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;
     }
 }
 


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 buckius82 · Jul 17, 2015 at 06:25 AM 0
Share

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;
     }
 }

avatar image
1

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).

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

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to use several colliders in the same object? 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