Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 H-Li · Jan 27, 2017 at 02:30 PM · buttonmousedragclick

How to disable button OnClick when drag on the screen with OnMouseDrag

I'm using OnMouseDrag() to change the camera view of the object on the screen. The code is like:

 void OnMouseDrag() {     
 if (isGameOver) {             
 return;         
 }         
 float rotSpeed = GameConst.rotateSpeed * 20.0f ;
 float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
 float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
 transform.RotateAround(Vector3.up, -rotX);         transform.RotateAround(Vector3.right - Vector3.forward, -rotY);
 }


Then I use button.OnClick().AddListerner() to attach a function when the button is clicked.

The problem is, everytime when I finish the drag, if the mouse up position is in the button region, the button would call the OnClick() function too. How can I disable OnClick() when it's a just a drag action?

Comment
Add comment · Show 1
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 commodore · Jan 27, 2017 at 06:14 PM 0
Share

You could use RemoveListener in the On$$anonymous$$ouseDrag function

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Aurel333 · Jan 27, 2017 at 04:43 PM

If your drag and OnClick functions are in the same script, you can add a bool variable that is set to true when on drag is called and set back to false when mouse button is released. It should look like this:

 private bool isDragging = false;
 
 void OnMouseDrag(){
 isDragging = true
 
 //Your code
 }
 
 void OnMouseUp(){
 isDragging = 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 H-Li · Jan 28, 2017 at 08:56 AM 0
Share

Not working, I guess the OnClick() executes after the On$$anonymous$$ouseUp().

avatar image
0

Answer by Arkshija · Apr 29, 2019 at 04:26 PM

I had this problem with a draggable component because that object had to be at the same time clickable but when I was dragging the component the onClick listener also was executing when the drag ends, I did this to fix that:

 public bool isClickable = true;
     
 void Start{
     GetComponent<Button>().onClick.AddListener(() => executeThis(isClickable));
 }
 
 void ExecuteThis (bool isClickable){
     if( isClickable){
         // Do something
     }
 }
 
 public void OnBeginDrag(PointerEventData eventData)
 {   
     isClickable = false;       
 }
 
 public void OnEndDrag(PointerEventData eventData)
 {      
     isClickable = true;
 }


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

7 People are following this question.

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

Related Questions

OnMouseUp() Click Display Effect. 1 Answer

Click and Drag Camera 3 Answers

Instantiate and Scale with Mouse Click. 1 Answer

GUI Repeatbutton up? 1 Answer

right mouse down on a gameObject 2 Answers


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