Webview in Unity

Recently I war working on creating a blog app by using Unity UIWidgets, actually it's not bad.
But when I click on the page's link, it refered to phone's system browser. It's not what I wanted.

So I found some webview solutions for Unity. Below is what I'm using.
https://github.com/gree/unity-webview

Why did I choose this one?

  • It was the first one working on my iPhone
  • It could work in MacOS Unity Editor, it is so wonderful that I could debug directly in my Editor.

How to use

  • Before use it directly, I wrapped it with below's code.
    public class WebViewManager : MonoSingleton<WebViewManager>
    
{
    private WebViewObject _webViewObject;
    private WebViewUI _webViewUI;
    private WebViewUI webViewUI
    {
        get
        {
            if (_webViewUI == null)
            {
                var root = Transform.FindObjectOfType<Canvas>();
                _webViewUI = GameObject.Instantiate(Resources.Load<WebViewUI>("WebViewUI"));
                var rectTransform = _webViewUI.transform as RectTransform;
                rectTransform.SetParent(root.transform);
                rectTransform.localPosition = Vector3.zero;
                rectTransform.localScale = Vector3.one;
                rectTransform.offsetMax = Vector2.zero;
                rectTransform.offsetMin = Vector2.zero;
            }

            return _webViewUI;
        }
    }
}

```
  • Then Init WebviewManager at the time it should be inited.
    public WebviewManager{
    	public override void Init()
        {
            base.Init();
    
    		#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
            webViewObject.bitmapRefreshCycle = 1;
    		#endif
    		webViewObject.SetMargins(0, (int) (Screen.height * (1 - 0.93f)), 0, 0);
        }
    }
    
  • When link is click in my blog app, the corresponding callback is:
    public void OpenURL(string url)
    {
        webViewObject.SetVisibility(true);
        webViewUI.OpenURL(url,(() =>
        {
            webViewObject.SetVisibility(false);
        }),webViewObject);
        if (url.StartsWith("http"))
        {
            webViewObject.LoadURL(url.Replace(" ", "%20"));
        }
    
        webViewObject.EvaluateJS(
            "parent.$(function() {" +
            "   window.Unity = {" +
            "       call:function(msg) {" +
            "           parent.unityWebView.sendMessage('WebViewObject', msg)" +
            "       }" +
            "   };" +
            "});");
    }
    

How to close webview

In WebViewUI class:

closeButton.onClick.AddListener(() =>
{
    if (onClickClose != null)
    {
        onClickClose();
    }

    this.gameObject.SetActive(false);
});

How to check the webview page's progress

It's also in WebViewUI class, by using an Unity Coroutine to present the processing state and checking progress:

IEnumerator DoProcessing()
{
    var target = closeButton.transform.GetChild(0);
    yield return null;
    yield return null;
    yield return null;
    target.DOLocalRotate(new Vector3(0, 0, 720), 1).SetLoops(-1).SetRelative(true).SetOptions(false);
#if UNITY_ANDROID || UNITY_IOS
    while (_webView.Progress() != 100)
#else
    while (_webView.Progress() != 0)
#endif
    {
        Debug.Log(_webView.Progress());
        yield return null;
    }


    DOTween.Kill(target);

    target.localRotation = Quaternion.identity;
}
WRITTEN BY:    Suntabu