UniWebView plugin for Unity arbitrarily failing on Android

1.7k views Asked by At

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>";
}
1

There are 1 answers

0
holgerm On

I found out that you have to call Show() before LoadHTMLString() on the UniWebView component. I show the corrected last lines of the Start() method below.

    ...
    uniWebView.SetShowSpinnerWhileLoading(true);
    uniWebView.ReferenceRectTransform = gameObject.GetComponent<RectTransform>();

    uniWebView.Show(true);

    uniWebView.LoadHTMLString(YoutubeHTMLString, "https://www.youtube.com/");
}

I can not tell why this solution works, but I consider it a bug (either in UniWebView, Unity or Android). It may have to do with asynchronous threads in the background that produce sometimes a problematic order on Android (I used an old Nexus 5 with Android 6.0.1).

Nevertheless, I hope this helps somebody, as I had two bad days of try and error ... ;-)