Home

Awesome

WebViewHook

Warning: WebViewHook is obsolete for Unity 2020 and Above due to removal of Asset Store Browsing in Unity Editor. This project is soon to be archived.

This is a code snippet to access a hidden WebView API from Unity Editor, and load it to EditorWindow.

Screenshot

Code

Download WebViewHook. See usage example in WebWindow or WebEditor. You may download the whole project if necessary.

Usage

Minimum code to get WebView working:

class WebViewDemo : EditorWindow
{

    WebViewHook webView;

    void OnEnable()
    {
        if (!webView)
        {
            // create webView
            webView = CreateInstance<WebViewHook>();
        }
    }

    public void OnBecameInvisible()
    {
        if (webView)
        {
            // signal the browser to unhook
            webView.Detach();
        }
    }

    void OnDestroy()
    {
        //Destroy web view
        DestroyImmediate(webView);
    }

    void OnGUI()
    {
        // hook to this window
        if (webView.Hook(this))
            // do the first thing to do
            webView.LoadURL("https://google.com");

        if (ev.type == EventType.Repaint)
        {
            // keep the browser aware with resize
            webView.OnGUI(new Rect(Vector2.zero, this.position.size));
        }
    }
}

Two Way Communication

Using LoadURL, LoadHTML and ExecuteJavascript you only can send information to WebView.

To extract informations from WebView you need external communication, and the easiest way to do this is by using Web Socket.

To get web socket server running you need to download WebSocket-Sharp and a script utility from this repo.

See WebData for minimal working example of using WebSocketHook to hook C# into javascript variable.

More Information and Caveats

This is an editor only solution. If you're looking to add WebView to a game build then this is not the repo you're looking for.

The technology behind WebView is Chrome Embedded Framework, version 37. In Windows it's contained inside the gigantic libcef.dll.

WebView in Unity is just like Chrome, with background-color default to darkgrey and no plugins (Flash/Java/PDF) allowed.

I solving bugs as I can, but of course limited. If you can't open a specific website, mostly comes down to the fact that Unity haven't upgrade their CEF browser.