Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Solved + custom UnityEvents] Event trigger Drag problem on android.

Discussion in 'UGUI & TextMesh Pro' started by tranos, Mar 2, 2015.

  1. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    I am implementing the above trigger . When I test it on editor it works fine with mouse but when I test it on android it probably detects drag because my finger covers bigger surface..Is there a way to make it detect drag when I am moving my finger more?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Failing all else, you could code your own component that implements the IDragHandler and IBeginDragHandler interfaces, save the event position in onBeginDrag, and then check in onDrag whether you've moved more than some specified distance from the starting point.

    (And if you want, this component could expose its own UnityEvent for other objects to subscribe to.)
     
  3. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    Thank you . Is there a tutorial on how I can do this ("expose its own UnityEvent for other objects to subscribe to" and "check in on drag")?
     
    Last edited: Mar 2, 2015
  4. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    I have found a solution. With changes after Antistone's suggestions.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;  // in order to use" IBeginDragHandler,IDragHandler,etc"
    4. using UnityEngine.Events;  // in order to use  "public UnityEvent pointerDown , public UnityEvent pointerEnter ,etc"
    5.  
    6. public class UiDrag2 : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler,IPointerDownHandler,IPointerEnterHandler,IPointerUpHandler,IPointerExitHandler
    7. {
    8.     Vector2 firstPosition;
    9.     Vector2 lastPosition;
    10.     float dragDistance;
    11.     public float difference =30f;
    12.  
    13.     public UnityEvent pointerDown;  // appears in Editor as "OnClick()"
    14.     public UnityEvent pointerEnter;  //                    -//-
    15.     public UnityEvent pointerUp;     //                    -//-
    16.     public UnityEvent pointerExit;   //                   -//-
    17.  
    18.     void Start()
    19.     {
    20.    
    21.     }
    22.     void Update()
    23.     {
    24.         Debug.Log ("dragDistance " + dragDistance);
    25.         dragDistance = firstPosition.x - lastPosition.x;
    26.    
    27.         if (dragDistance > difference)
    28.         {    
    29.             PlayerControl.controlRef.hInput = -1;        
    30.         }
    31.    
    32.     }
    33.  
    34.     public void OnBeginDrag(PointerEventData eventData)
    35.     {
    36.         firstPosition = eventData.position;
    37.     }
    38.  
    39.     public void OnDrag(PointerEventData data)
    40.     {
    41.         lastPosition = data.position;
    42.     }
    43.  
    44.     public void OnEndDrag(PointerEventData data)
    45.     {
    46.         firstPosition = new Vector2 (0,0);
    47.         lastPosition = new Vector2 (0,0);
    48.     }
    49.  
    50.     public void OnPointerDown(PointerEventData eventData)
    51.     {
    52.         pointerDown.Invoke();
    53.     }
    54.  
    55.     public void OnPointerEnter(PointerEventData eventData)
    56.     {
    57.         pointerEnter.Invoke ();
    58.     }
    59.  
    60.     public void OnPointerUp(PointerEventData eventData)
    61.     {
    62.         pointerUp.Invoke ();
    63.     }
    64.  
    65.     public void OnPointerExit(PointerEventData eventData)
    66.     {
    67.         pointerExit.Invoke ();
    68.     }
    69. }
    70.  
     
    Last edited: Mar 3, 2015
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Put a public field of type UnityEvent in your class. This will let you add callbacks to this event in the editor, kind of like the button's "onClick" or the input field's "onValueChange".

    Then have your class call .Invoke() on that UnityEvent when you want the event to occur.