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 callbacksvoid OnEnable (){//Event triggered when webview window is shownWebView.DidShowEvent += DidShowEvent;//Event triggered when webview window is hiddenWebView.DidHideEvent += DidHideEvent;//Event triggered when webview window is destroyedWebView.DidDestroyEvent += DidDestroyEvent;//Event triggered when webview started loading a webpageWebView.DidStartLoadEvent += DidStartLoadEvent;//Event triggered when webview finished loading web pageWebView.DidFinishLoadEvent += DidFinishLoadEvent;//Event triggered when webview failed loadingWebView.DidFailLoadWithErrorEvent += DidFailLoadWithErrorEvent;//Event triggered when webview finished evaluating JSWebView.DidFinishEvaluatingJavaScriptEvent += DidFinishEvaluatingJavaScriptEvent;//Event triggered when webview received a message (for registered schemes)WebView.DidReceiveMessageEvent += DidReceiveMessageEvent;}//Disable the events that were registered earliervoid OnDisable (){// Deregistering callbacksWebView.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 : http://www.google.com
private void LoadRequest (){// Load an URLm_webview.LoadRequest(m_url);}
private void LoadHTMLString (){// Load webview with htmlm_webview.LoadHTMLString(HTML_STRING);}private void LoadHTMLStringWithJavaScript (){// Load webview with html & JS scriptm_webview.LoadHTMLStringWithJavaScript(HTML_STRING, JS_SCRIPT);}
private void LoadFile (){//Load a file in webviewm_webview.LoadFile(Demo.Utility.GetScreenshotPath(), "image/png", null, null);}
private void ShowWebView (){//Show webviewm_webview.Show();}private void HideWebView (){//Hide this webviewm_webview.Hide();}
private void ReloadWebView (){//Reloads current webpagem_webview.Reload();}
private void StopWebView (){//Stops a current loading page.m_webview.StopLoading();}
private void DestroyWebView (){//Destroy this webviewm_webview.Destroy();}
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";StartCoroutine(ReadFromStreamingAssets(_fileName));}IEnumerator ReadFromStreamingAssets(string _fileName){string _streamingAssetsPath = Application.StreamingAssetsPath;string _filePath = _streamingAssetsPath + "/" + _fileName;WWW _www = new WWW(_filePath);yield return _www;m_webview.LoadHTMLString(_www.text);}
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><html><body><script>function Concat (str1, str2){return str1.concat(str2);}</script></body></html>
After loading a HTML_STRING (see sample html above), we can refer the methods and evaluate with this method.
m_webview.EvaluateJavaScriptFromString(EVAL_STRING);
EVAL_STRING Example :
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.
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 monitorm_webview.AddNewURLSchemeName(m_URLSchemeName);
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.
m_webview.ClearCache();