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.
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); });
}
}
Your answer
Follow this Question
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