Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Slider does not drag for ExecuteEvents.dragHandler

Discussion in 'Unity UI & TextMesh Pro' started by trappist-1, May 17, 2018.

  1. trappist-1

    trappist-1

    Joined:
    Mar 26, 2017
    Posts:
    287
    I think I have read all articles on Google pertaining to making a slider drag when a pointer hovers over it but am unable to make anything work.

    I am using a VRTK UI pointer but no matter what I try, the slider handle just sits there. I see it change colors when it is focused but it never moves.

    This is one of the ways I have tried to do it but I have no idea what is really going on and no documentation seems to explain the ExecuteEvents or their actual usage.

    Code (csharp):
    1. IEnumerator DoWhileDraggingSliderRoutine(VRTK_UIPointerHandPointerReactor sender, Slider slider, float maxDistance=1.0f) {//This CoRoutine is called after a pointer enter event.
    2.     var pointer = ((VRTK_UIPointerHandPointerReactor)sender).pointerEventData;
    3.     yield return null;
    4.  
    5.     ExecuteEvents.Execute(slider.gameObject, sender.pointerEventData, ExecuteEvents.pointerDownHandler);
    6.     ExecuteEvents.Execute(slider.gameObject, sender.pointerEventData, ExecuteEvents.beginDragHandler);
    7.  
    8.     while (sender.hoveringElement == slider.gameObject && sender.pointerEventData.pointerCurrentRaycast.distance < maxDistance) {
    9.         ExecuteEvents.Execute(slider.gameObject, sender.pointerEventData, ExecuteEvents.dragHandler);
    10.         yield return null;
    11.     }
    12.  
    13.     ExecuteEvents.Execute(slider.gameObject, sender.pointerEventData, ExecuteEvents.endDragHandler);
    14.     ExecuteEvents.Execute(slider.gameObject, sender.pointerEventData, ExecuteEvents.pointerUpHandler);
    15. }
    I get no errors or warnings. Also I have debugged the "While" loop and it does loop and the raycast's worldPosition shows the correct hit position, so the raycasting looks ok...

    I'm completely stumped, still tryin though.

    [EDIT]

    Either I am doing this completely wrong or VRTK pointer or the UI system is broken. I have tried so many things and none of which ever work.

    I thought this would be trivial, but now I'm clueless as to how to raycast to the UI and click or drag or whatever...

    [UPDATE]
    So I was curious and tried to see if the slider could even handle the event I was sending it.
    I printed the results of CanHandleEvent and the result was True:
    Code (csharp):
    1. print(ExecuteEvents.CanHandleEvent<IDragHandler>(slider.gameObject));
    2. //Prints: True
    So I'm still completely confused as to why it won't move.
     
    Last edited: May 19, 2018
  2. trappist-1

    trappist-1

    Joined:
    Mar 26, 2017
    Posts:
    287
    Finally figured it out!
    And all I had to set was the pointerPressRaycast parameter:
    Code (csharp):
    1. IEnumerator DoWhileDraggingSliderRoutine(VRTK_UIPointer sender, Slider slider) {
    2.     var pointer = sender.pointerEventData;
    3.     pointer.pointerPressRaycast = pointer.pointerCurrentRaycast; //THIS IS CRITICAL!
    4.  
    5.     while (sender.hoveringElement == slider.gameObject) {
    6.         ExecuteEvents.Execute<IDragHandler>(slider.gameObject, pointer, ExecuteEvents.dragHandler);
    7.         yield return null;
    8.     }
    9.     ...
    Couldn't find any mention of this on any search engine, but I finally stumbled across it in some backyard example somewhere.
     
    Last edited: May 20, 2018