Cross Platform Native Plugins
A true cross platform tool for Unity which provides unique and unified way to access native functionality on mobile platforms.
Public Member Functions | Properties | Events | List of all members
WebView Class Reference

Provides a cross-platform interface to display web contents inside your application. More...

Public Member Functions

delegate void WebViewEvent (WebView _webview)
 Delegate for web view's generic callbacks. More...
 
delegate void WebViewFailedLoad (WebView _webview, string _error)
 Delegate that will be called when web view fails to load requested content. More...
 
delegate void WebViewFinishedEvaluatingJS (WebView _webview, string _result)
 Delegate that will be called when web view finishes running a JavaScript script. More...
 
delegate void WebViewReceivedMessage (WebView _webview, WebViewMessage _message)
 Delegate that will be called when web view sends a message to Unity. More...
 
void Destroy ()
 Destroys the web view instance. More...
 
void Show ()
 Displays the web view on the top of Unity view. More...
 
void Hide ()
 Dismisses the web view. More...
 
void LoadRequest (string _URL)
 Connects to a given URL and asynchronous loads the content. More...
 
void LoadHTMLStringContentsOfFile (string _HTMLFilePath, string _baseURL)
 Loads the webpage contents from specified file. More...
 
void LoadHTMLStringWithJavaScript (string _HTMLString, string _javaScript, string _baseURL=null)
 Loads the webpage contents and runs JavaScript string. More...
 
void LoadHTMLString (string _HTMLString, string _baseURL=null)
 Loads the webpage contents. More...
 
void LoadFile (string _filepath, string _MIMEType, string _textEncodingName, string _baseURL)
 Loads the webpage contents from specified file. More...
 
void LoadData (byte[] _byteArray, string _MIMEType, string _textEncodingName, string _baseURL)
 Loads the webpage contents from the given data. More...
 
void EvaluateJavaScriptFromString (string _javaScript)
 Executes a JavaScript string. More...
 
void Reload ()
 Reloads the current page. More...
 
void StopLoading ()
 Stops loading the current page contents. More...
 
void AddNewURLSchemeName (string _URLSchemeName)
 Registers the specified scheme, after which web view will start to listen for custom URL. More...
 
void SetFullScreenFrame ()
 Sets the web view frame to full screen size. More...
 
void ClearCache ()
 Clears all stored cached URL responses. More...
 

Properties

string URL [get]
 The web view content URL. (read-only) More...
 
bool CanHide [get, set]
 A Boolean value indicating whether the web view can be dismissed on close button click. More...
 
bool CanBounce [get, set]
 A Boolean value that controls whether the web view bounces past the edge of content and back again. More...
 
eWebviewControlType ControlType [get, set]
 An enum value that determines the appearence of web view. More...
 
bool ShowSpinnerOnLoad [get, set]
 A boolean value indicating whether an activity spinner should be displayed while loading webpage. More...
 
bool AutoShowOnLoadFinish [get, set]
 A boolean value indicating whether web view can auto show itself when load request is finished. More...
 
bool ScalesPageToFit [get, set]
 A boolean value indicating whether web view scales webpages to fit the view and the user can change the scale. More...
 
Rect Frame [get, set]
 The frame rectangle, which describes the web view’s position and size. More...
 
Color BackgroundColor [get, set]
 The web view’s background color. More...
 

Events

static WebViewEvent DidShowEvent
 Event that will be called when web view is first displayed. More...
 
static WebViewEvent DidHideEvent
 Event that will be called when web view is dismissed. More...
 
static WebViewEvent DidDestroyEvent
 Event that will be called when web view is destroyed. More...
 
static WebViewEvent DidStartLoadEvent
 Event that will be called when web view begins load request. More...
 
static WebViewEvent DidFinishLoadEvent
 Event that will be called when web view has finished loading. More...
 
static WebViewFailedLoad DidFailLoadWithErrorEvent
 Event that will be called when web view has failed to load requested content. More...
 
static WebViewFinishedEvaluatingJS DidFinishEvaluatingJavaScriptEvent
 Event that will be called when web view has finished executing JavaScript script. More...
 
static WebViewReceivedMessage DidReceiveMessageEvent
 Event that will be called when web view passes a message to Unity. More...
 

Detailed Description

Provides a cross-platform interface to display web contents inside your application.

To do so, drag and drop the WebView prefab into scene heirarchy, which is placed under folder Assets/VoxelBusters/NativePlugins/Prefab. And then send it a request to display web content. You can also use this class to move back and forward in the history, just like web browser by setting control type to eWebviewControlType.TOOLBAR.

The following code illustrates how to load webpage using web view.

using System.Collections;
public class ExampleClass : MonoBehaviour
{
public WebView webView;
private void Start ()
{
// Set web view properties
webView.AutoShowOnLoadFinish = true; // Webview will show itself when request completes
webView.ControlType = eWebviewControlType.CLOSE_BUTTON; // Creates webview with close button for dismissing it
// Set to full screen
webView.SetFullScreenFrame(); // Sets frame to full screen size
// Start request
webView.LoadRequest("http://www.google.com");
}
private void OnEnable ()
{
// Registering for event
WebView.DidStartLoadEvent += OnDidStartLoadEvent;
WebView.DidFinishLoadEvent += OnDidFinishLoadEvent;
WebView.DidFailLoadWithErrorEvent += OnDidFailLoadWithErrorEvent;
}
private void OnDisable ()
{
// Unregistering event
WebView.DidStartLoadEvent -= OnDidStartLoadEvent;
WebView.DidFinishLoadEvent -= OnDidFinishLoadEvent;
WebView.DidFailLoadWithErrorEvent -= OnDidFailLoadWithErrorEvent;
}
private void DidStartLoadEvent (WebView _webview)
{
if (this.webView == _webview)
{
Debug.Log("Webview did start loading request.");
}
}
private void DidFinishLoadEvent (WebView _webview)
{
if (this.webView == _webview)
{
Debug.Log("Webview did finish loading request.");
}
}
private void DidFailLoadWithErrorEvent (WebView _webview, string _error)
{
if (this.webView == _webview)
{
Debug.Log("Webview did fail with error: " + _error);
}
}
}

Member Function Documentation

delegate void WebViewEvent ( WebView  _webview)

Delegate for web view's generic callbacks.

Parameters
_webviewThe web view that caused this event.
delegate void WebViewFailedLoad ( WebView  _webview,
string  _error 
)

Delegate that will be called when web view fails to load requested content.

Parameters
_webviewThe web view that failed to load content.
_errorThe error that occurred during loading.
delegate void WebViewFinishedEvaluatingJS ( WebView  _webview,
string  _result 
)

Delegate that will be called when web view finishes running a JavaScript script.

Parameters
_webviewThe web view that has finished executing script.
_resultThe result received upon running the JavaScript script. You can even get null value, if web view fails to run the script.
delegate void WebViewReceivedMessage ( WebView  _webview,
WebViewMessage  _message 
)

Delegate that will be called when web view sends a message to Unity.

When a webpage with registered scheme starts loading, webview raises WebView.DidReceiveMessageEvent along with WebViewMessage object. Using WebViewMessage object, you can get additional information about URL such as host, scheme and arguments.

You can register your own scheme, by calling WebView.AddNewURLSchemeName before starting load request.

Parameters
_webviewThe web view that sent this message.
_messageThe object holds information about URL.
void Destroy ( )

Destroys the web view instance.

DidDestroyEvent event will be called when object is destroyed.

void Show ( )

Displays the web view on the top of Unity view.

void Hide ( )

Dismisses the web view.

void LoadRequest ( string  _URL)

Connects to a given URL and asynchronous loads the content.

Parameters
_URLA URL identifying the location of the content to load.
Note
Don’t use this method to load local HTML files, instead use LoadHTMLStringContentsOfFile.
void LoadHTMLStringContentsOfFile ( string  _HTMLFilePath,
string  _baseURL 
)

Loads the webpage contents from specified file.

Parameters
_HTMLFilePathPath of the target file, to use as contents of the webpage.
_baseURLThe base URL for the content.
void LoadHTMLStringWithJavaScript ( string  _HTMLString,
string  _javaScript,
string  _baseURL = null 
)

Loads the webpage contents and runs JavaScript string.

Parameters
_HTMLStringThe contents of the webpage.
_javaScriptThe JavaScript string to run.
_baseURLThe base URL for the content.
void LoadHTMLString ( string  _HTMLString,
string  _baseURL = null 
)

Loads the webpage contents.

Parameters
_HTMLStringThe contents of the webpage.
_baseURLThe base URL for the content.
void LoadFile ( string  _filepath,
string  _MIMEType,
string  _textEncodingName,
string  _baseURL 
)

Loads the webpage contents from specified file.

Parameters
_filepathPath of the target file to use as contents of the webpage.
_MIMETypeThe MIME type of the content.
_textEncodingNameThe content's character encoding name.
_baseURLThe base URL for the content.
void LoadData ( byte[]  _byteArray,
string  _MIMEType,
string  _textEncodingName,
string  _baseURL 
)

Loads the webpage contents from the given data.

Parameters
_byteArrayThe data to use as the contents of the webpage.
_MIMETypeThe MIME type of the content.
_textEncodingNameThe content's character encoding name.
_baseURLThe base URL for the content.
void EvaluateJavaScriptFromString ( string  _javaScript)

Executes a JavaScript string.

Parameters
_javaScriptThe JavaScript string to evaluate.
void Reload ( )

Reloads the current page.

void StopLoading ( )

Stops loading the current page contents.

void AddNewURLSchemeName ( string  _URLSchemeName)

Registers the specified scheme, after which web view will start to listen for custom URL.

This approach is used for communicating web view with Unity. When web view starts loading contents, it will check against registered schemes. And incase if a match is found, web view will raise DidReceiveMessageEvent along with URL information.

Parameters
_URLSchemeNameThe scheme name of the URL.
void SetFullScreenFrame ( )

Sets the web view frame to full screen size.

void ClearCache ( )

Clears all stored cached URL responses.

Property Documentation

string URL
get

The web view content URL. (read-only)

bool CanHide
getset

A Boolean value indicating whether the web view can be dismissed on close button click.

true if web view can be dismissed; otherwise, false.

bool CanBounce
getset

A Boolean value that controls whether the web view bounces past the edge of content and back again.

true if web view can bounce; otherwise, false.

eWebviewControlType ControlType
getset

An enum value that determines the appearence of web view.

bool ShowSpinnerOnLoad
getset

A boolean value indicating whether an activity spinner should be displayed while loading webpage.

true if web view can show spinner while loading webpage; otherwise, false.

bool AutoShowOnLoadFinish
getset

A boolean value indicating whether web view can auto show itself when load request is finished.

true if web view can auto show itself; otherwise, false.

bool ScalesPageToFit
getset

A boolean value indicating whether web view scales webpages to fit the view and the user can change the scale.

true if web view scales page to fit; otherwise, false.

Rect Frame
getset

The frame rectangle, which describes the web view’s position and size.

Color BackgroundColor
getset

The web view’s background color.

Event Documentation

WebViewEvent DidShowEvent
static

Event that will be called when web view is first displayed.

WebViewEvent DidHideEvent
static

Event that will be called when web view is dismissed.

WebViewEvent DidDestroyEvent
static

Event that will be called when web view is destroyed.

WebViewEvent DidStartLoadEvent
static

Event that will be called when web view begins load request.

WebViewEvent DidFinishLoadEvent
static

Event that will be called when web view has finished loading.

WebViewFailedLoad DidFailLoadWithErrorEvent
static

Event that will be called when web view has failed to load requested content.

WebViewFinishedEvaluatingJS DidFinishEvaluatingJavaScriptEvent
static

Event that will be called when web view has finished executing JavaScript script.

WebViewReceivedMessage DidReceiveMessageEvent
static

Event that will be called when web view passes a message to Unity.

This approach is used for communicating web view with Unity. To do so, register your scheme by calling WebView.AddNewURLSchemeName. The web view will then listen to the URL's being loaded and raises DidReceiveMessageEvent event whenever a matching scheme is found.