[Solved] Scroll not working when elements inside have click events
I'm using the new Unity UI for a mobile game and in the menu I have a Scroll Rect with a Vertical Layout Group inside, with Text elements inside the group. Like this:
The thing is that the scroll works fine until I set pointer events to the text elements (Pointer Up, Pointer Click, etc), then when I swipe above an element I think the UI is "paying attention" to the event manager i.e. checking if I release my finger to trigger Pointer Up event and therefore it does not scroll. But it still scrolls if I click outside a Text element but inside the Vertical Layout Group.
So I don't know if there's a way to make the group scroll even if the elements have this events.
Answer by marucu · Feb 22, 2015 at 02:00 PM
I found what was happening! The point is that if you use Event Trigger it will eat all other inputs because it implements all the EventSystem interfaces (and the OnDrag event used by the scroll rect was eclipsed).
namespace UnityEngine.EventSystems
{
[AddComponentMenu("Event/Event Trigger")]
public class EventTrigger : MonoBehaviour, IEventSystemHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler
{
[Serializable]
public class TriggerEvent : UnityEvent<BaseEventData>
{
}
... blah, blah, blah
}
}
So, the solution, avoid using Event Trigger, instead, implement the interface you need for the purpose. My example, instead of adding OnPointerDown and OnPointerUp using the Event Trigger I added this script to the object:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Click : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public void OnPointerDown (PointerEventData eventData) {
// Do action
}
public void OnPointerUp (PointerEventData eventData) {
// Do action
}
}
Thanks @marucu, you save my day, I working on the nest scrollview passer, and your answer help me to understand a lot. https://www.youtube.com/watch?v=0CgG7WIkV0s
full setup article : http://www.clonefactor.com/wordpress/program/unity3d/1519/
I know this is old as hell, but can anyone tell me if its possible to assign the triggered actions in the inspector, just like in the Unity EventTrigger? Does it use a special editor script, or is there a way to implement this easily?
Try something like this
public class Click : $$anonymous$$onoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public UnityEvent PointerDown;
public UnityEvent PointerUp;
public void OnPointerDown(PointerEventData eventData)
{
if (PointerDown != null)
PointerDown.Invoke();
}
public void OnPointerUp(PointerEventData eventData)
{
if (PointerUp != null)
PointerUp.Invoke();
}
}
Answer by tomihr2 · Dec 24, 2016 at 11:54 PM
When I try this implementation:
public class Click : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
public void OnPointerDown (PointerEventData eventData) {
// Do action
}
public void OnPointerUp (PointerEventData eventData) {
// Do action
}
}
On mobile device OnPointerUp event is triggered almost same time as OnPointerDown, so can't do anything with this implementation, am I doing something wrong?
Answer by viciousesque · Aug 20, 2016 at 12:29 AM
A simpler solution is to just use a button. I deleted the EventTrigger components and replaced them with simple Button components and this allowed the Image component to remain clickable and propagated the scroll event up to the ScrollRect game object, so the scrolling action now works when users scroll over the image.
Just a heads up: If you do this, you may run into problems with certain mobile devices. For example my UI is working perfectly in Editor and on a OnePlus Two, but a Samsung Galaxy S6 has huge issues recognising button clicks. For now it seems like @marucu 's solution works best.
Your answer
Follow this Question
Related Questions
Special Grid layout 0 Answers
How to prevent automatic tmp ugui resizing. 0 Answers
ScrollRect content/Vertical Layout Group content resets to middle when children altered 1 Answer
Making flower of cards 1 Answer
New GUI - Order of elements? 0 Answers