So, I have two scroll rects one essentially takes up my entire screen and can only scroll content horizontally. The other one is a list of items that scrolls vertically. They both work fine as long as i am not in spot that raycasts onto both of them.
The problem is if i am in the vertical scroll rect and try to move horizontally it just slightly moves my vertical one (cause its damn near impossible to not move a tad bit vertically). what i want is no matter what if i scroll horizontally first then i want that scroll rect to take over otherwise if i move vertically first then that one should be the one that moves.
An approach i am thinking about doing is simply inhering the IBeginDragHandler, IDragHandler and attemp to finagle something together in the hopes i can get it to work
So, I have solved my own problem. I did use the Interface method above but the implementation was less then 100 lines so i don’t think its a horrible solution but here it is in case others are in need.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System;
[RequireComponent(typeof(ScrollRect))]
public class SecondScrollRect : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public ScrollRect OtherScrollRect;
private ScrollRect _myScrollRect;
//This tracks if the other one should be scrolling instead of the current one.
private bool scrollOther;
//This tracks wether the other one should scroll horizontally or vertically.
private bool scrollOtherHorizontally;
void Awake()
{
//Get the current scroll rect so we can disable it if the other one is scrolling
_myScrollRect = this.GetComponent<ScrollRect>();
//If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally.
scrollOtherHorizontally = _myScrollRect.vertical;
//Check some attributes to let the user know if this wont work as expected
if (scrollOtherHorizontally)
{
if(_myScrollRect.horizontal)
Debug.Log("You have added the SecondScrollRect to a scroll view that already has both directions selected");
if (!OtherScrollRect.horizontal)
Debug.Log("The other scroll rect doesnt support scrolling horizontally");
}
else if (!OtherScrollRect.vertical)
{
Debug.Log("The other scroll rect doesnt support scrolling vertically");
}
}
//IBeginDragHandler
public void OnBeginDrag(PointerEventData eventData)
{
//Get the absolute values of the x and y differences so we can see which one is bigger and scroll the other scroll rect accordingly
float horizontal = Mathf.Abs(eventData.position.x - eventData.pressPosition.x);
float vertical = Mathf.Abs(eventData.position.y - eventData.pressPosition.y);
if (scrollOtherHorizontally)
{
if (horizontal > vertical)
{
scrollOther = true;
//disable the current scroll rect so it doesnt move.
_myScrollRect.enabled = false;
OtherScrollRect.OnBeginDrag(eventData);
}
}
else if (vertical > horizontal)
{
scrollOther = true;
//disable the current scroll rect so it doesnt move.
_myScrollRect.enabled = false;
OtherScrollRect.OnBeginDrag(eventData);
}
}
//IEndDragHandler
public void OnEndDrag(PointerEventData eventData)
{
if (scrollOther)
{
scrollOther = false;
_myScrollRect.enabled = true;
OtherScrollRect.OnEndDrag(eventData);
}
}
//IDragHandler
public void OnDrag(PointerEventData eventData)
{
if (scrollOther)
{
OtherScrollRect.OnDrag(eventData);
}
}
}