Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How do i disable drag input for a TMP input field? (inside a scrollview)

Discussion in 'UGUI & TextMesh Pro' started by Divinitize1, Apr 1, 2023.

  1. Divinitize1

    Divinitize1

    Joined:
    May 27, 2019
    Posts:
    101
    As the title says, when i drag a scroll view and happen to start dragging on an inputfield it just selects the input field and ignores the drag.
    Even spent a good hour with chatGPT4 trying to figure this one out but to no success.

    Any help would be greatly appreciated.
     
  2. Divinitize1

    Divinitize1

    Joined:
    May 27, 2019
    Posts:
    101
    ok this script work for now, its a little hit or miss on mobile, but it works for now,
    just a pain it replaces the origin TMP input field as i have to reapply my references and add the placeholder/text/textarea back in


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using TMPro;
    5.  
    6. public class DraggableInputField : TMP_InputField, IBeginDragHandler, IDragHandler, IEndDragHandler
    7. {
    8.     private bool isDragging = false;
    9.  
    10.     public void OnBeginDrag(PointerEventData eventData)
    11.     {
    12.         isDragging = true;
    13.  
    14.         // Disable the input field to prevent it from being selected.
    15.         interactable = false;
    16.  
    17.         // Trigger the BeginDrag event on the scroll view.
    18.         ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.beginDragHandler);
    19.     }
    20.  
    21.     public void OnDrag(PointerEventData eventData)
    22.     {
    23.         if (isDragging)
    24.         {
    25.             // Trigger the Drag event on the scroll view.
    26.             ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.dragHandler);
    27.         }
    28.     }
    29.  
    30.     public void OnEndDrag(PointerEventData eventData)
    31.     {
    32.         isDragging = false;
    33.  
    34.         // Enable the input field.
    35.         interactable = true;
    36.  
    37.         // Trigger the EndDrag event on the scroll view.
    38.         ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.endDragHandler);
    39.     }
    40. }
    41.  
    42.  
     
    MaksymNiechaiev likes this.
  3. yone-haru

    yone-haru

    Joined:
    Aug 30, 2021
    Posts:
    9
    I have same problem too