Usage
All web view features can be accessed through WebView class. Before using it, we need to import the namespace.
1
using VoxelBusters.CoreLibrary;
2
using VoxelBusters.EssentialKit;
Copied!

Register to events

Once after importing the namespace, for getting the events fired from Web View, it's required to register to the events first.
1
private void OnEnable()
2
{
3
// register for events
4
WebView.OnShow += OnWebViewShow;
5
WebView.OnHide += OnWebViewHide;;
6
WebView.OnLoadStart += OnWebViewLoadStart;;
7
WebView.OnLoadFinish += OnWebViewLoadFinish;;
8
}
9
10
private void OnDisable()
11
{
12
// register for events
13
WebView.OnShow -= OnWebViewShow;
14
WebView.OnHide -= OnWebViewHide;;
15
WebView.OnLoadStart -= OnWebViewLoadStart;;
16
WebView.OnLoadFinish -= OnWebViewLoadFinish;;
17
}
Copied!
Events get fired for various reasons such as when web view hides/shows, when a page loading starts/finishes.

Create Web View Instance

Create an instance of WebView with CreateInstance. This instance holds the properties of how you want to present and operate with your webview.
1
WebView webview = WebView.CreateInstance();
Copied!

Set Frame Size

Once after registering the events and creating the WebView instance with CreateInstance, you need to set the frame size of the webview. You can set the frame size in multiple ways.

Set normalized size

1
webview.SetNormalizedFrame(new Rect(0f, 0f, 1f, 1.0f));
Copied!

Set in terms of screen size

1
webview.Frame = new Rect(0f, 0f, Screen.width, Screen.height);
Copied!

Set full screen

1
webview.SetFullScreen();
Copied!

Show

For showing webview, you need to call Show method. Make sure you set the frame size of the webview else, it won't be visible.
1
webview.Show();
Copied!
You can show the webview anytime but optionally you show only once the page/url is loaded successfully. AutoShowOnLoadFinish property of WebView allows to show the webview once the page is loaded.
1
webview.AutoShowOnLoadFinish = true;
Copied!
For getting an event when a webview is shown, you can listen to WebView.OnShow event.
1
//Register to WebView.OnShow event
2
3
private void OnWebViewShow(WebView result)
4
{
5
Debug.Log("Webview is being displayed : " + result);
6
}
Copied!

Hide

Call Hide in-case if you want to hide the webview. Calling a hidden webview is a no-operation.
1
webview.Hide();
Copied!
For getting an event when a webview is hidden, you can listen to WebView.OnHide event.\
1
//Register to WebView.OnHide event
2
3
private void OnWebViewHide(WebView result)
4
{
5
Debug.Log("Webview is hidden : " + result);
6
}
Copied!

Appearance Styles

It's possible to set different controls to show up with the webview. These controls can help to use WebView based on your use-cases.
Default
Popup
Browser
Default mode doesn't have any controls and this appearance is ideal for ads like use-case
1
webview.style = WebViewStyle.Default;
Copied!
Popup mode adds a close button at top-right corner of the web view. On clicking the button, the web view will get hidden.
1
webview.style = WebViewStyle.Popup;
Copied!
Browser mode provides a browser like appearance with 4 buttons for easy navigation.
Button
Description
Back
On pressing, it takes back to the previous browsed webpage
Forward
On pressing, it loads the next url in the browser webpage history list
Reload
On pressing, it re-loads the current webpage
Done/Close
On pressing, it dismisses the current displayed web view
1
webview.style = WebViewStyle.Popup;
Copied!

Load Content

For loading a url, you need to pass well qualified url to LoadURL method.

Load local file url

1
webview.LoadURL(URLString.FileURLWithPath("local device path"));//Ex: Load from Application.PersistentPath
Copied!

Load web url

1
webview.LoadURL(URLString.URLWithPath("https://www.google.com"));
Copied!

Load Html string

1
string htmlString = "<html><body><p><b>This text is bold</b></p><p><i>This text is italic</i></p><p>This is<sub> subscript</sub> and <sup>superscript</sup></p></body></html>";
2
webview.LoadHtmlString(htmlString);
Copied!

Load data

You can also load data content in the web view. Ex : Loading byte data of a file
1
Texture2D texture; // Load the texture
2
string mimeType, textEncodingName;
3
byte[] data = texture.Encode(TextureEncodingFormat.JPG, out mimeType, out textEncodingName);
4
webView.LoadData(data, mimeType, textEncodingName);
Copied!

Load texture

Loading texture loads the texture into the web view.
1
Texture2D texture; // Load the texture
2
webView.LoadTexture(texture, TextureEncodingFormat.JPG);
Copied!

Run Java Script code

You can run javascript code using RunJavaScript method. But before using this method, you need set JavaScriptEnabled property of web view to true.
1
// Enable java script first
2
webview.JavaScriptEnabled = true;
3
4
/*
5
<!DOCTYPE html>
6
<html>
7
<body>
8
<script>
9
function Concat (str1, str2)
10
{
11
return str1.concat(str2);
12
}
13
</script>
14
</body>
15
</html>
16
*/
17
18
string htmlString = "<!DOCTYPE html><html><body> <script> function Concat (str1, str2) { return str1.concat(str2); } </script></body></html>"
19
20
// Load html data
21
webview.LoadHtmlString(htmlString);
22
23
// Run Java Script
24
webview.RunJavaScript("Concat("Voxel", "Busters")", (result, error) => {
25
if (error == null)
26
{
27
Debug.Log("Javascript was executed successfully. Result: " + result.Result);
28
}
29
else
30
{
31
Debug.Log("Could not run javascript. Error: " + error);
32
}
33
});
34
Copied!

Controlling data

Reload

Reload the current web page data with Reload method. It reloads the total web page and resets to default.
1
webview.Reload();
Copied!

Stop Loading

Stop loading if you want to abort the current loading of web url.
1
webview.StopLoading();
Copied!

Clear Cache

Clear the cached content with ClearCache. This will clear the complete browsing history.
1
webview.ClearCache();
Copied!

Receive messages from web view to unity

For getting messages from web view to unity, you need to register a scheme ahead. Schemes are the first part of a valid url.
For ex: https://www.google.com has https as scheme. ftp://user:[email protected]:post/path has ftp as scheme
In-case if you need to pass data from web view, you can have your own custom scheme and register it.
Let's say you want to send some data on pressing a button. You can href to a custom url with scheme unity. For ex: unity://accesstoken?key=value
So first you need to add the scheme
1
webview.AddScheme("unity");
Copied!
Once after adding the scheme you can get all of the url's which are getting loaded with unity as scheme.
//OnURLSchemeMatchFound (pending)

Additional settings

There are additional properties which can be controlled with in a web view.
Property
Description
ScalesPageToFit
A boolean value indicating whether web view scales webpages to fit the view and the user can change the scale.
CanBounce
A Boolean value that controls whether the web view bounces past the edge of content and back again.
BackgroundColor
The background color of the webview
Progress
The value indicates the progress of load request
IsLoading
A boolean value indicating whether this webview is loading content

Last modified 1mo ago