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.

Event Registration

// 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;
}

Load from Streaming Assets

m_webview - Webview Reference (Created from Prefab)

m_url - Your webpage to load. Ex : http://www.google.com

Load Request
private void LoadRequest ()
{
    // Load an URL
    m_webview.LoadRequest(m_url);
}
Loading HTML

private void LoadHTMLString ()
{
    // Load webview with html
    m_webview.LoadHTMLString(HTML_STRING);
}

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
    m_webview.Show();
}

private void HideWebView ()
{
    //Hide this webview
    m_webview.Hide();
}
Reload Webview
private void ReloadWebView ()
{
    //Reloads current webpage
    m_webview.Reload();
}
Stop loading Webview
private void StopWebView ()
{
    //Stops a current loading page.
    m_webview.StopLoading();
}
Destroy Webview

private void DestroyWebView ()
{
    //Destroy this webview
    m_webview.Destroy();
}

Load from Streaming Assets and from a path

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);

Evaluate Javascript

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.


Comminication between Unity and Webview (via Schemes)

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
m_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.


Clear Cache

Below call cleans the cache from the webview.

m_webview.ClearCache();

results matching ""

    No results matching ""