Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by xpavelos · Jun 15, 2017 at 10:08 AM · uibuttonbutton trigger eventsdraggingprevention

Cannot click on a button while Drag Events are called

Hi people.

I have created my own swiping menu for mobile and I am now facing the problem:

When I drag the screen, buttons "lose" their state (highlighted/pressed - to normal) even though a mouse is still over them, which means buttons cannot be pressed while dragging.

It seems like drag events cannot be handled together with button states at the same time. The only way to receive OnClick event is clicking on a button without moving the mouse (so that drag events are not called).

Is there a way to be able to "not lose" a button state while OnBeginDrag and OnDrag events are fired?

How would you solve that?

Thanks.

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 xpavelos · Jun 16, 2017 at 03:35 PM 0
Share

If I hold a mouse button down and then start moving the mouse - not just OnBeginDrag and OnDrag events are fired, but also OnPointerUp! (and mouse button is still held down). Why is this happening?

Is it a Unity bug?

2 Replies

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

Answer by xpavelos · Jun 17, 2017 at 10:31 AM

Okay I solved the issue.

The problem is that for some reason the OnPointerUp event is fired when drag events are in use. In order to avoid it - drag functions must be also implemented in a button (even if they are empty).

Here are more details: http://answers.unity3d.com/questions/1082179/mouse-drag-element-inside-scrollrect-throws-pointe.html

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
avatar image
0

Answer by Addyarb · Jun 16, 2017 at 12:53 AM

Good question - it does seem like this should be an included functionality in the UI system - but it just doesn't seem to support both events used together. To remedy this, we'll create our own button component that will completely replace the existing button. (Note: you can still use the existing button component alongside this if you'd like).

We'll need to use Unity's Events and EventSystem libraries. To do this, just put these two new lines at the top of your script!

     using UnityEngine.Events;
     using UnityEngine.EventSystems;

Next we'll need to add two Interfaces. These will allow us to listen to Unity's event system (the same one that you're using when you add a Button or EventTrigger component). We want to listen to two of them in particular - called IPointerDownHandler and IPointerUpHandler.

You can add them by typing a comma after Monobehavior (or whatever other class you're inheriting from) and then typing every interface you need. Just start typing IPointer... and your auto-complete options will show you the rest. Check them out!

  public class CustomButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler

Now that we've added our interfaces, we can implement the methods that they require. In a nutshell, interfaces add specific functionality (like listening for events from an event system). However, you have to agree to implement the methods that they want you to - or it won't compile.

When you implement these methods, they have to share the same Access Modifiers, method name, and parameters.

The IPointerDownHandler interface wants us to use a public method called OnPointerDown. In that method, it wants us to have a PointerEventData parameter. So lets do that.

 public void OnPointerDown(PointerEventData data)
     {
         OnButtonDown.Invoke();
     }

And a similar case with IPointerUpHandler

     public void OnPointerUp(PointerEventData data)
     {
         OnButtonUp.Invoke();
     }

As you can see I'm just invoking our stored methods when these are called from Unity's event system.

And that's all there is to it! Full script below for yours and others' convenience.

     using UnityEngine;
     using UnityEngine.Events;
     using UnityEngine.EventSystems;
     
     public class CustomButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
     {
         //Called when we click the button down
         public UnityEvent OnButtonDown;
     
         //Called when we click the button up
         public UnityEvent OnButtonUp;
     
         public void OnPointerDown(PointerEventData data)
         {
             OnButtonDown.Invoke();
         }
     
         public void OnPointerUp(PointerEventData data)
         {
             OnButtonUp.Invoke();
         }
     }
 


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 xpavelos · Jun 16, 2017 at 08:44 AM 0
Share

Thanks for broad answer and your effort - I appreciate that. Unfortunately, it doesn't solve the problem.

OnPointerUp event is fired when I start dragging (even when mouse is still down). It looks like it's Unity bug.

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

108 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 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 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 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 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

UI button mobile - want a button drag to register as a click 0 Answers

Highlighted buttons stay highlighted when I deactivate a canvas. No way to unhighlight them. 1 Answer

How to detect button presses and change child object text in the button's Parent Object script 0 Answers

Clicking a button will display that buttons text in a seperate text-field 1 Answer

Problem with UI buttons and MultiTouch 4 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