Change which mouse button is used to scroll in ScrollRect [SOLVED]

I’ve created a new ScrollView. How do I change which mouse button is used to scroll in ScrollRect. By default it’s the left mouse button. I’d like to be able to change this to another mouse button. I’d also like to be able to use a keyboard key and disable it altogether (via script without disabling the scrollbars), but mainly I at least want to change it from the left mouse button.

Solved with help I got here:

using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MiddleScrollRect : ScrollRect
{
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Middle)
        {
            eventData.button = PointerEventData.InputButton.Left;
            base.OnBeginDrag(eventData);
        } 
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Middle)
        {
            eventData.button = PointerEventData.InputButton.Left;
            base.OnEndDrag(eventData);
        }    
    }

    public override void OnDrag(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Middle)
        {
            eventData.button = PointerEventData.InputButton.Left;
            base.OnDrag(eventData);
        }
    }
}

Hi, I used to face the same problem. This is an alternative solution I came up with, as I didn’t want to convert the middle click pointer events to left clicks (as in the answer shared by @nsfnotthrowingaway):

The solution is rather simple:

  1. Create a new class that overrides the default ScrollRect’s OnDrag() event handler.
  2. Move the content’s localPosition, using eventData.delta to determine the distance to move.
  3. Prevent the contents from moving out of the ScrollRect bounds by making use of the ScrollRect.normalizedPosition property, ensuring x and y dimensions stay between 0.0 and 1.0.

Sample code:

using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MiddleScrollRect : ScrollRect {

    public override void OnDrag(PointerEventData eventData){
        if (Input.GetMouseButton(2)) {
            content.localPosition = content.localPosition + new Vector3(eventData.delta.x, eventData.delta.y);
            normalizedPosition = new Vector2(Mathf.Clamp(normalizedPosition.x, 0.0f, 1.0f), Mathf.Clamp(normalizedPosition.y, 0.0f, 1.0f));
        }
    }
}

Hope this helps!