Exposed Unity Editor WebView API
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Assets 🐱‍👤 0.7.0! Feb 1, 2018
ProjectSettings 🐱‍👤 0.7.0! Feb 1, 2018
Screenshots 0.6.0! Jan 28, 2018
.gitattributes Initial commit Jan 23, 2018
.gitignore 0.6.0! Jan 28, 2018
LICENSE Initial commit Jan 23, 2018
README.md Add warn in readme Feb 21, 2018

README.md

WebViewHook

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.

WebView is hidden for a good reason. The editor might crash if done improperly so follow the examples and don't experiment with project unsaved.

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 allowed (hence you can't open Flash/Java/PDF inside it).

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.

For safety reason, the WebViewHook uses AssemblyReloadEvents which only available in Unity version 2017.1 or higher.