Hi unity people…I use the new UI system to make a ScrollViewer, everything works fine until I add PointerDown event to the content gameObjects of ScrollRect. I can’t scroll when I click on the gameObject which have PointerDown event.
any one know how to fix this?
update: I find out it’s EventTrigger component on the content gameObject makes the scroll function not work.
but I have to use the PointerDown event, so the EventTrigger component is necessary.
I solved this myself.
“NOTE: Attaching this component to a GameObject will make that object intercept ALL events, and no event bubbling will occur from this object!”, this is a note about EventTrigger component from the unity manual.
so I created a custom component and implement the IPointerDown interface to replace the EventTrigger component.
@lolzrofl I’m posting my code, you just need to add any other interface you may need, and use the internal variables to subscribe to the events. The script need to be added to the object in which you want the events to be called. Please up vote my answer if it helped you.
//By: Daniel Soto
//4dsoto@gmail.com
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.Events;
namespace LSCore.Events {
public class EventTriggers : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler/*,
IBeginDragHandler, IDragHandler, IEndDragHandler*/ {
internal UnityAction<BaseEventData> m_OnPointerDown;
internal UnityAction<BaseEventData> m_OnPointerUp;
internal UnityAction<BaseEventData> m_OnPointerEnter;
internal UnityAction<BaseEventData> m_OnPointerExit;
internal UnityAction<BaseEventData> m_OnBeginDrag;
internal UnityAction<BaseEventData> m_OnDrag;
internal UnityAction<BaseEventData> m_OnEndDrag;
public void OnPointerDown (PointerEventData eventData) {
// Do action
if( m_OnPointerDown != null )
{
m_OnPointerDown( eventData );
}
}
public void OnPointerUp (PointerEventData eventData) {
// Do action
if( m_OnPointerUp != null )
{
m_OnPointerUp( eventData );
}
}
public void OnPointerEnter (PointerEventData eventData) {
// Do action
if( m_OnPointerEnter != null )
{
m_OnPointerEnter( eventData );
}
}
public void OnPointerExit (PointerEventData eventData) {
// Do action
if( m_OnPointerExit != null )
{
m_OnPointerExit( eventData );
}
}
/*public void OnBeginDrag (PointerEventData eventData) {
// Do action
if( m_OnBeginDrag != null )
{
m_OnBeginDrag( eventData );
}
}
public void OnDrag (PointerEventData eventData) {
// Do action
if( m_OnDrag != null )
{
m_OnDrag( eventData );
}
}
public void OnEndDrag (PointerEventData eventData) {
// Do action
if( m_OnEndDrag != null )
{
m_OnEndDrag( eventData );
}
}*/
}
}