Usage
All web view features can be accessed through WebView class. Before using it, we need to import the namespace.
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.
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.
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
Set in terms of screen size
Set full screen
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.
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.
For getting an event when a webview is shown, you can listen to WebView.OnShow event.
Hide
Call Hide in-case if you want to hide the webview. Calling a hidden webview is a no-operation.
For getting an event when a webview is hidden, you can listen to WebView.OnHide event.\
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 mode doesn't have any controls and this appearance is ideal for ads like use-case
Load Content
For loading a url, you need to pass well qualified url to LoadURL method.
Load local file url
Load web url
Load Html string
Load data
You can also load data content in the web view. Ex : Loading byte data of a file
Load texture
Loading texture loads the texture into the web view.
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.
Controlling data
Reload
Reload the current web page data with Reload method. It reloads the total web page and resets to default.
Stop Loading
Stop loading if you want to abort the current loading of web url.
Clear Cache
Clear the cached content with ClearCache. This will clear the complete browsing history.
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:password@host: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
Once after adding the scheme you can get all of the url's which are getting loaded with unity as scheme. The url's to unity are passed through OnURLSchemeMatchFound event. So, make sure you register to WebView.OnURLSchemeMatchFound ahead.
Additional settings
There are additional properties which can be controlled with in a web view.
Last updated