MicrosoftEdge / MicrosoftEdge/WebView2Feedback

Unity Mousewheel crash. How do I implement scroll pages in HoloLens 2 Unity apps?

Open
#3,705 10 comments 1 reaction 1 assignee View on GitHub

Nobody has claimed this yet.

bug tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

I went through the tutorial and downloaded the examples.

Following the example, I implemented OnPointerDown() and OnPointerUp().

But found that can only achieve click operations and can not scroll the web page.

I want to make it easy for users to scroll and navigate the web using gestures. How do I do that?

(I tried to implement MouseWheel operation in OnPointerDragged(), but couldn't find any instructions or examples, and every time I tried to send a MouseEvent myself, Unity crashed.)

Here is part of my code :

bool isMouseDown = false;
Vector3 startPos;

public void OnPointerDown(MixedRealityPointerEventData eventData)
{
    TranslateToWebViewMouseEvent(eventData, WebViewMouseEventData.EventType.MouseDown);
    UnityEngine.Ray leftHandRay;
    bool isGetSuccess = InputRayUtils.TryGetHandRay(Microsoft.MixedReality.Toolkit.Utilities.Handedness.Right, out leftHandRay);
    if (isGetSuccess)
    {
        startPos = leftHandRay.origin;
    }
    isMouseDown = true;
}

public void OnPointerUp(MixedRealityPointerEventData eventData)
{
    TranslateToWebViewMouseEvent(eventData, WebViewMouseEventData.EventType.MouseUp);
    isMouseDown = false;
}

private void TranslateToWebViewMouseEvent(MixedRealityPointerEventData eventData, WebViewMouseEventData.EventType evenType)
{
    var hitCoord = NormalizeWorldPoint(eventData.Pointer.Result.Details.Point);

    hitCoord.x *= _webView.Width;
    hitCoord.y *= _webView.Height;

    var mouseEventsWebView = _webView as IWithMouseEvents;
    WebViewMouseEventData mouseEvent = new WebViewMouseEventData
    {
        X = (int)hitCoord.x,
        Y = (int)hitCoord.y,
        Device = WebViewMouseEventData.DeviceType.Pointer,
        Type = evenType,
        Button = WebViewMouseEventData.MouseButton.ButtonLeft,
        TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
    };

    mouseEventsWebView.MouseEvent(mouseEvent);
}

private Vector2 NormalizeWorldPoint(Vector3 worldPoint)
{
    // Convert the world point to our control's local space.
    Vector3 localPoint = transform.InverseTransformPoint(worldPoint);

    var boundsSize = Collider.sharedMesh.bounds.size;
    var boundsExtents = Collider.sharedMesh.bounds.max;

    // Adjust the point to be based on a 0,0 origin.
    var uvTouchPoint = new Vector2((localPoint.x + boundsExtents.x), -1.0f * (localPoint.y - boundsExtents.y));

    // Normalize the point so it can be mapped to the WebView's texture.
    var normalizedPoint = new Vector2(uvTouchPoint.x / boundsSize.x, uvTouchPoint.y / boundsSize.y);

    return normalizedPoint;
}

public void OnPointerDragged(MixedRealityPointerEventData eventData) 
{
    if (isMouseDown)
    {
        UnityEngine.Ray leftHandRay;
        bool isGetSuccess = InputRayUtils.TryGetHandRay(Microsoft.MixedReality.Toolkit.Utilities.Handedness.Right, out leftHandRay);
        if (isGetSuccess)
        {
            var distance = Vector3.Distance(startPos, leftHandRay.origin);
            if (distance > 0.01)
            {
                var mouseEventsWebView = _webView as IWithMouseEvents;
                WebViewMouseEventData mouseEvent = new WebViewMouseEventData
                {
                    WheelX = 0,
                    WheelY = distance,
                    Device = WebViewMouseEventData.DeviceType.Pointer,
                    Type = WebViewMouseEventData.EventType.MouseWheel,
                    Button = WebViewMouseEventData.MouseButton.ButtonMiddle,
                    TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
                };

                mouseEventsWebView.MouseEvent(mouseEvent);
            }
        }
    }
}

AB#46192317

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.