Search Unity

Fix for ScrollRect multi-touch?

Discussion in 'Editor & General Support' started by daniel_lochner, May 15, 2019.

  1. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    175
    Please note: There is an open bounty for this question on StackOverflow: https://stackoverflow.com/questions/56221113/fix-for-scrollrect-multi-touch-in-unity

    Currently, ScrollRect is extremely buggy when it comes to multi-touch on mobile devices.

    If you try it out yourself, you will see that whenever you place two fingers on the screen, the content will jump around, and produce some unexpected behaviour.

    Are there any solutions to this? Currently, this only solution I have found, but it is still buggy in some cases, and most importantly, does not determine the average input position for all your fingers on the screen.

    Here is my modified version of the MultiTouchScrollRect.cs script from the UnityUIExtensions bitbucket, but it jumps every-time I place my next finger on the screen:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class MultiTouchScrollRect : ScrollRect
    6. {
    7.     private int minimumTouchCount = 1, maximumTouchCount = 2, pointerId = -100;
    8.  
    9.     public Vector2 MultiTouchPosition
    10.     {
    11.         get
    12.         {
    13.             Vector2 position = Vector2.zero;
    14.             for (int i = 0; i < Input.touchCount && i < maximumTouchCount; i++)
    15.             {
    16.                 position += Input.touches[i].position;
    17.             }
    18.             position /= ((Input.touchCount <= maximumTouchCount) ? Input.touchCount : maximumTouchCount);
    19.             return position;
    20.         }
    21.     }
    22.  
    23.     public override void OnBeginDrag(PointerEventData eventData)
    24.     {
    25.         if (Input.touchCount >= minimumTouchCount)
    26.         {
    27.             pointerId = eventData.pointerId;
    28.             eventData.position = MultiTouchPosition;
    29.             base.OnBeginDrag(eventData);
    30.         }
    31.     }
    32.     public override void OnDrag(PointerEventData eventData)
    33.     {
    34.         if (Input.touchCount >= minimumTouchCount)
    35.         {
    36.             eventData.position = MultiTouchPosition;
    37.             if (pointerId == eventData.pointerId)
    38.             {
    39.                 base.OnDrag(eventData);
    40.             }
    41.         }
    42.     }
    43.     public override void OnEndDrag(PointerEventData eventData)
    44.     {
    45.         if (Input.touchCount >= minimumTouchCount)
    46.         {
    47.             pointerId = -100;
    48.             eventData.position = MultiTouchPosition;
    49.             base.OnEndDrag(eventData);
    50.         }
    51.     }
    52. }
    Thanks for your time!
     
    Last edited: May 25, 2019
  2. MrMagister

    MrMagister

    Joined:
    Jan 8, 2019
    Posts:
    9
    Supporting this post. Also have this issue and looking for solution
     
  3. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    175
  4. Danief_

    Danief_

    Joined:
    Feb 2, 2019
    Posts:
    20
    Hi Daniel, do you know if any quality solution has been found for this?
     
  5. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    175
    Henry_Sun likes this.
  6. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    daniel_lochner likes this.