For my mobile Unity app, I need a web view element and use the UniWebView plugin. As shown in the code below, I wrote a script to dynamically load a html string. In my app the containing scene will be loaded many times, based on user interaction.
In the editor and on iOS it works as expected. On Android the web view is shown for the first scene load after app start. But later reloads sometimes work and sometimes fail arbitrarily shwoing a white area only. This happens although the loading spinner works, so it just seems to be a visualization failure.
The script is attached below. I put the script on a simple panel gameobject which is only used to define the screen area.
Any idea how I can solve this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YouTubePlayer : MonoBehaviour
{
private UniWebView uniWebView;
public void Start()
{
uniWebView = GetComponent<UniWebView>();
if (uniWebView == null)
{
Debug.Log("YOUTUBE PLAYER: Adding uniwebview Component");
uniWebView = gameObject.AddComponent<UniWebView>();
}
uniWebView.OnPageStarted += (view, url) =>
{
Debug.Log("YOUTUBE PLAYER: OnPageStarted with url: " + url);
};
uniWebView.OnPageFinished += (view, statusCode, url) =>
{
Debug.Log("YOUTUBE PLAYER: OnPageFinished with status code: " + statusCode);
};
uniWebView.OnPageErrorReceived += (UniWebView webView, int errorCode, string errorMessage) =>
{
Debug.Log("YOUTUBE PLAYER: OnPageErrorReceived errCode: " + errorCode
+ "\n\terrMessage: " + errorMessage);
};
uniWebView.SetShowSpinnerWhileLoading(true);
uniWebView.ReferenceRectTransform = gameObject.GetComponent<RectTransform>();
uniWebView.LoadHTMLString(YoutubeHTMLString, "https://www.youtube.com/");
uniWebView.Show(true);
}
private static string YoutubeHTMLString =
@"<html>
<head></head>
<body style=""margin:0\"">
<iframe width = ""100%"" height=""100%""
src=""https://www.youtube.com/embed/Ccj_H__4KGQ"" frameborder=""0""
allow=""autoplay; encrypted-media"" allowfullscreen>
</iframe>
</body>
</html>";
}