问题:
我在 Unity 项目中设置了 WebView2。现在我想将我的点击从画布转移到 WebView。然而,通过
WebViewMouseEventData
后,什么也没有发生。我是否忘记了什么或者我必须自己再次更新什么?
我已阅读以下文章,最后它大致展示了如何做到这一点。
即使我发送了错误的坐标,我也必须在某个时刻发送随机可用的坐标,以便点击网页上的某些内容。但这并没有发生。
除了交出
MouseEvent
之外,还需要做更多的事情吗?
我的设置:
public void OnPointerClicked(MixedRealityPointerEventData eventData)
{
var result = eventData.Pointer.Result;
if (result.CurrentPointerTarget.layer != 30)
{
Debug.Log("OnPointerClicked>layer != 30");
return;
}
var clickPosition = result.Details.PointLocalSpace;
var x = (int)Math.Ceiling(clickPosition.x);
var y = (int)Math.Ceiling(clickPosition.y);
Debug.Log("OnPointerClicked > X: " + x + " Y: " + y);
var mouseEventsWebView = _webView as IWithMouseEvents;
WebViewMouseEventData mouseEvent = new WebViewMouseEventData
{
X = x,
Y = y,
Device = WebViewMouseEventData.DeviceType.Pointer,
Type = WebViewMouseEventData.EventType.MouseDown,
Button = WebViewMouseEventData.MouseButton.ButtonLeft,
TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
};
mouseEventsWebView.MouseEvent(mouseEvent);
}
不幸的是,文档没有提供有关该主题的任何信息。
我的问题的正确答案由 Michael Farnsworth发布在 Github:
public void OnPointerClicked(MixedRealityPointerEventData eventData)
{
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 = WebViewMouseEventData.EventType.MouseDown,
Button = WebViewMouseEventData.MouseButton.ButtonLeft,
TertiaryAxisDeviceType = WebViewMouseEventData.TertiaryAxisDevice.PointingDevice
};
mouseEventsWebView.MouseEvent(mouseEvent);
// To register as a click, the WebView needs to be a mouse-up event.
mouseEvent.Type = WebViewMouseEventData.EventType.MouseUp;
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;
}