Dear Magic Leap Team,
I'm trying to make the MLWebView work in our OpenXR (MLSDK 2.3) unity project. It seems that either the deprecated MLWebView does not work with OpenXR or i might have missed some setup steps.
These are the versions i used:
Unity Editor version: 2022.3.19f1
ML2 OS version: 1.9
Unity SDK version: 2.3
Host OS: Windows
Unfortunately, I didn't find any WebView Example project which is using the OpenXR XR Provider. The only example project i found was for MLSDK version 1.12 which was unfortunately not using OpenXR, so i took that as a starting point.
My approach was the following:
I tried to replicate minimal gameobjects and scripts needed from the 1.12. example project, because this overview states that i can still use the deprecated MLSDK for WebViews.
I set up the MLWebViewScreenBehaviour exactly like in the 1.12. example project as you can see in the images below. Permissions, Manifest and other project settings should also be correct.
My Problem now is that the MLWebViewScreenBehaviour.WebView stays null after CreateWebViewWindow() was called which probably is also the reason why the WebView does not render and stays white/default material.
WebViewML script:
This script simply tries to navigate to the homeUrl which is just "https://google.de/". It is a very shortened and slightly modified version of the WebViewExample script from the 1.12. example project.
Notice that I also tried to wait for the webview to become initialized, but that still didn't work.
public class WebViewML : MonoBehaviour
{
private event Action OnSetupComplete;
[SerializeField]
private MLWebViewScreenBehavior _mlWebViewScreenBehavior;
[SerializeField] private string _homeUrl = "https://google.de/";
private CancellationTokenSource _cancellationTokenSource;
private void OnEnable()
{
OnSetupComplete += HandleSetupCompleted;
}
private void OnDisable()
{
OnSetupComplete -= HandleSetupCompleted;
}
private void Start()
{
try
{
if (Permissions.CheckPermission(Permissions.WebView))
{
Debug.Log("WEBVIEW: WebView Permission is granted.");
}
else
{
Debug.LogError("WEBVIEW: WebView Permission is missing.");
}
if (_mlWebViewScreenBehavior.CreateWebViewWindow())
{
Debug.Log("WEBVIEW: Successfully created web view window");
_cancellationTokenSource = new CancellationTokenSource();
StartSetupTask(_cancellationTokenSource.Token).Forget(ex => UnityEngine.Debug.LogException(ex));
}
else
{
Debug.LogError("WEBVIEW: Failed to create web view window");
}
}
catch (Exception ex)
{
Debug.LogError($"WEBVIEW: {ex.Message}");
}
}
private async UniTask StartSetupTask(CancellationToken cancellationToken)
{
while (_mlWebViewScreenBehavior.WebView == null && !cancellationToken.IsCancellationRequested)
{
// ERROR HERE
Debug.LogError("WEBVIEW: WebView still null.");
await UniTask.WaitForSeconds(1, cancellationToken: cancellationToken);
}
Debug.LogError("WEBVIEW: Setup completed.");
OnSetupComplete?.Invoke();
}
private void HandleSetupCompleted()
{
Debug.Log("WEBVIEW: Handle Setup Completed");
_mlWebViewScreenBehavior.WebView.OnServiceConnected += HandleOnServiceConnected;
_mlWebViewScreenBehavior.WebView.OnServiceDisconnected += HandleOnServiceDisconnected;
_mlWebViewScreenBehavior.WebView.OnServiceFailed += HandleOnServiceFailed;
HomePage();
}
private void HandleOnServiceFailed(UnityEngine.XR.MagicLeap.MLWebView webView, UnityEngine.XR.MagicLeap.MLResult result)
{
Debug.Log("WEBVIEW: OnServiceFailed");
}
private void HandleOnServiceDisconnected(UnityEngine.XR.MagicLeap.MLWebView webView)
{
Debug.Log("WEBVIEW: OnServiceDisconnected");
}
private void HandleOnServiceConnected(UnityEngine.XR.MagicLeap.MLWebView webView)
{
Debug.Log("WEBVIEW: OnServiceConnected");
}
/// <summary>
/// Load the home page.
/// </summary>
public void HomePage()
{
GoToUrl(_homeUrl);
}
/// <summary>
/// Change current web page to the provided Url address.
/// </summary>
public void GoToUrl(String url)
{
if (_mlWebViewScreenBehavior.WebView != null)
{
if (!_mlWebViewScreenBehavior.WebView.GoTo(url).IsOk)
{
Debug.LogError("WEBVIEW: Failed to navigate to url " + url);
}
}
else
{
Debug.LogError("WEBVIEW: WebView is null.");
}
}
public void OnDestroy()
{
if (_mlWebViewScreenBehavior != null)
{
var webView = _mlWebViewScreenBehavior.WebView;
if (webView != null)
{
webView.OnServiceConnected -= HandleOnServiceConnected;
webView.OnServiceDisconnected -= HandleOnServiceDisconnected;
webView.OnServiceFailed -= HandleOnServiceFailed;
}
_mlWebViewScreenBehavior.DestroyWebViewWindow();
}
if (_cancellationTokenSource != null)
{
_cancellationTokenSource.Cancel();
}
}
Another approach i tried out was to update the 1.12. example project from ML XR Provider to OpenXR XR Provider and also update to the latest MLSDK version, but unfortunately i did not manage to get the WebView running this way.
Both ways i was not able to make the WebView work with the OpenXR XR Provider and the latest MLSDK versions. Is there maybe something else you would recommend?
Thank you for your help and kind regards