Implementing Feature

Once you add webview prefab to your scene hierarchy, set its properties and refer its instance from your script.

First, register for the events in OnEnable to get the callbacks.

// Registering callbacks
void OnEnable ()
//Event triggered when webview window is shown
WebView.DidShowEvent += DidShowEvent;
//Event triggered when webview window is hidden
WebView.DidHideEvent += DidHideEvent;
//Event triggered when webview window is destroyed
WebView.DidDestroyEvent += DidDestroyEvent;
//Event triggered when webview started loading a webpage
WebView.DidStartLoadEvent += DidStartLoadEvent;
//Event triggered when webview finished loading web page
WebView.DidFinishLoadEvent += DidFinishLoadEvent;
//Event triggered when webview failed loading
WebView.DidFailLoadWithErrorEvent += DidFailLoadWithErrorEvent;
//Event triggered when webview finished evaluating JS
WebView.DidFinishEvaluatingJavaScriptEvent += DidFinishEvaluatingJavaScriptEvent;
//Event triggered when webview received a message (for registered schemes)
WebView.DidReceiveMessageEvent += DidReceiveMessageEvent;
//Disable the events that were registered earlier
void OnDisable ()
// Deregistering callbacks
WebView.DidShowEvent -= DidShowEvent;
WebView.DidHideEvent -= DidHideEvent;
WebView.DidDestroyEvent -= DidDestroyEvent;
WebView.DidStartLoadEvent -= DidStartLoadEvent;
WebView.DidFinishLoadEvent -= DidFinishLoadEvent;
WebView.DidFailLoadWithErrorEvent -= DidFailLoadWithErrorEvent;
WebView.DidFinishEvaluatingJavaScriptEvent -= DidFinishEvaluatingJavaScriptEvent;
WebView.DidReceiveMessageEvent -= DidReceiveMessageEvent;

m_webview - Webview Reference (Created from Prefab)

m_url - Your webpage to load. Ex :

Load Request

private void LoadRequest ()
// Load an URL

Loading HTML

private void LoadHTMLString ()
// Load webview with html
private void LoadHTMLStringWithJavaScript ()
// Load webview with html & JS script
m_webview.LoadHTMLStringWithJavaScript(HTML_STRING, JS_SCRIPT);

Loading a File in Webview

private void LoadFile ()
//Load a file in webview
m_webview.LoadFile(Demo.Utility.GetScreenshotPath(), "image/png", null, null);

Show / Hide Webview

private void ShowWebView ()
//Show webview
private void HideWebView ()
//Hide this webview

Reload Webview

private void ReloadWebView ()
//Reloads current webpage

Stop loading Webview

private void StopWebView ()
//Stops a current loading page.

Destroy Webview

private void DestroyWebView ()
//Destroy this webview

As streaming assets path is not consistent on different platforms (ex : Android), Its advised to use Application.PersistentPath for the files. In the coming versions we add support for Streaming Assets path by default.

Below code explains on how to load a file into webview from streaming assets.

//If you want to load from streaming assets, currently this is the way to do it.
private void LoadHTMLStringContentsOfFile ()
string _fileName = "test.html";
IEnumerator ReadFromStreamingAssets(string _fileName)
string _streamingAssetsPath = Application.StreamingAssetsPath;
string _filePath = _streamingAssetsPath + "/" + _fileName;
WWW _www = new WWW(_filePath);
yield return _www;

If the path is other than from Streaming Assets, provide the direct link as below :

m_webView.LoadHTMLStringContentsOfFile(PATH_TOHTML_FILE, BASE_URL);

This allows to pass JS script and use/access methods with in HTML page. Once evaluation is done, this triggers WebView.DidFinishEvaluatingJavaScriptEvent.

<!DOCTYPE html>
function Concat (str1, str2)
return str1.concat(str2);

After loading a HTML_STRING (see sample html above), we can refer the methods and evaluate with this method.



In this example, we use concat method of above html sample.

Concat("Voxel", "Busters")

Once after evaluation, DidFinishEvaluatingJavaScriptEvent event is triggered with the required data.

This allows message passing from Webview to Unity via Schemes registered.

Registering for Schemes

Schemes allow to monitor webpage URL format and retrieve information from webpage.

For a URL unity://action?arg1=val1&arg2=val2, unity is the scheme name.

If user registers this scheme, when ever a url is loaded of this format in webview, it passes the information to Unity as a WebViewMessage.

//Add a scheme to monitor

Once the url loaded matches the scheme, WebView.DidReceiveMessageEvent event with WebViewMessage instance is received.

If above example url is loaded, WebViewMessage instance holds SchemeName as “unity”, Host as “action”, Arguments as dictionary of arg/val pairs.

Please note that, if registered sceme is found, webview don't load the url with the registered scheme.

Below call cleans the cache from the webview.