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 /
  • Help Room /
avatar image
1
Question by Phedg1 · Nov 27, 2017 at 08:32 AM · drageventsystemscrollview

How Can I Get An Event To Do It's Original Base Action As Well As My Code

I want to run specific functions when I click a button, to better detect the clicking of that button when deployed on mobile. The Buttons in question are in a ScrollRect, in Unity 5.6.3. I first tried using the EventTrigger on my buttons, with PointerDown and PointerUp instead of Button.OnClick(). This allowed for better click detection, but would not allow my ScrollRect to scroll. Another answer on here said to use OnPointerDown and OnPointerUp in a script instead. I noticed that OnPointerUp was being called whenever I'd first move my mouse/touch. The answer to this, these forums said, was to create an empty OnDrag event. Now Up, Down and Drag all fire at the correct times, but once again I cannot scroll in my ScrollRect.

How can I the original OnDrag event to be called AS WELL as my code in the event. I would like it to do both. Or, if anyone else has a solution that is different, I'd love to know. Thanks!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class ClickInput : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler {
 
     public void OnPointerDown(PointerEventData eventData) {
         Debug.Log("a");
     }
 
     public void OnDrag(PointerEventData eventData) {
         Debug.Log("b");
     }
 
     public void OnPointerUp(PointerEventData eventData) {
         Debug.Log("c");
     }
 }

Also, I can't seem to figure how to add a line break for this post. Sorry about that. I'm also not sure whether I should be using Unity Answers or Forums for this sort of thing. I've been using forums lately, but have not got any replies.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Hatsuko · Dec 02, 2017 at 12:30 AM

Disclaimer: I'm unfamiliar with these UI Event things

I just got it working, but I don't fully understand it as well. You might need to look into it further more. I noticed that before I implemented Drag interfaces, my OnPointerUp was fired whenever I started dragging. After I implemented Drag interfaces, my OnPointerUp was fired after done dragging.

Put this script on your Button.

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.Events;
 // https://answers.unity.com/questions/902929/scroll-not-working-when-elements-inside-have-click.html
 // https://answers.unity.com/questions/981692/is-there-a-way-to-pass-on-ui-events.html
 // https://forum.unity.com/threads/nested-scrollrect.268551/
 public class Test : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
 {
     // This is from the third url, from CaptainSchnittchen's post
     private void DoForParents<T>(System.Action<T> action) where T:IEventSystemHandler
     {
         Transform parent = transform.parent;
         while (parent != null) {
             foreach (var component in parent.GetComponents<Component>()) {
                 if (component is T)
                     action((T)(IEventSystemHandler)component);
             }
             parent = parent.parent;
         }
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         Debug.Log("OnPointerDown");
         DoForParents<IPointerDownHandler>((parent) => { parent.OnPointerDown(eventData); });
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         Debug.Log("OnPointerUp");
         DoForParents<IPointerUpHandler>((parent) => { parent.OnPointerUp(eventData); });
     }
 
     public void OnBeginDrag(PointerEventData eventData)
     {
          Debug.Log("OnBeginDrag");
          DoForParents<IBeginDragHandler>((parent) => { parent.OnBeginDrag(eventData); });
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         // Debug.Log("OnDrag");
         DoForParents<IDragHandler>((parent) => { parent.OnDrag(eventData); });
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
          Debug.Log("OnEndDrag");
          DoForParents<IEndDragHandler>((parent) => { parent.OnEndDrag(eventData); });
     }
 }


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 Hatsuko · Dec 02, 2017 at 12:40 AM 0
Share

Oh the line break thing, just enter more new line. alt text

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

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

Camera click and drag pan with UI elements blocking raycast 0 Answers

Disable current drag of UI.Slider 2 Answers

How to stop the current dragged GameObject? 0 Answers

How to stop object from going to center of mouse when dragging starts 0 Answers

PointerEventData.hovered blank when used with Right mouse button drag. 0 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