ViveSoftware / ViveInputUtility-Unity

A toolkit that helps developing/prototyping VR apps.
http://u3d.as/uF7
Other
353 stars 82 forks source link

All items under ScrollRect receive no events #37

Closed nantianliao closed 5 years ago

nantianliao commented 6 years ago

For example, a button under ScrollRect, although it responds to clicking animation, can't trigger anything other than that. My device is Vive Focus.

lawwong commented 6 years ago

You can try adding a "drag event blocker" to the button object.

using UnityEngine;
using UnityEngine.EventSystems;

public class DragEventBlocker : MonoBehaviour
    , IDragHandler
{
    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("Drag event blocked");
    }
}

This blocker will block the drag event so it can't be sent to their parent (ScrollRect).

nantianliao commented 6 years ago

Thank you. The button has responded. In the same way, ScrollRect will not respond. Is there a better way to solve the conflict between them?

lawwong commented 6 years ago

You mean you can't drag ScrollRect now? I suggest add a background image to the buttons as a draggable zone.

If you have to drag & click at the same time (which I don't recommend, it's kind of an odd behavior). Try this

using HTC.UnityPlugin.Pointer3D;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MyButton : Button
    , IPointer3DPressEnterHandler
    , IPointer3DPressExitHandler
{
    public override void OnPointerDown(PointerEventData eventData) { }

    public override void OnPointerUp(PointerEventData eventData) { }

    public override void OnPointerClick(PointerEventData eventData) { }

    public virtual void OnPointer3DPressEnter(Pointer3DEventData eventData)
    {
        // Perform OnPointerDown transition
        base.OnPointerDown(eventData);
    }

    public virtual void OnPointer3DPressExit(Pointer3DEventData eventData)
    {
        // Perform OnPointerUp transition
        base.OnPointerUp(eventData);
        // Click only if button is released
        if (!eventData.GetPress())
        {
            // Perform OnClick event
            base.OnPointerClick(eventData);
        }
    }
}

UnityEngine.UI.Button only response to IPointerClickHandler, which will be canceled if it's parent received Drag event. So here override OnPointerClick with OnPointer3DPressExit event. Notice that IPointer3DPressEnterHandler and IPointer3DPressExitHandler only defined in VIU, built-in handler doesn't include them.