IWebView

interface

IWebView is the primary interface for loading and interacting with web content. WebViewPrefab takes care of creating one for you and hooking it up to the materials in its prefab. If you want to create an IWebView outside of the prefab (to connect to your own custom GameObject) you can use Web.CreateWebView().

Summary

Public properties

Size

Vector2 Size

The webview's current size in Unity units.

Texture

Texture2D Texture

The texture for the webview's web content.

Url

string Url

The current URL.

VideoTexture

Texture2D VideoTexture

The texture for the webview's video content.

Public methods

Blur

void Blur()

Makes the webview relinquish focus.

CanGoBack

Task<bool> CanGoBack()

Checks whether the webview can go back with a call to GoBack().

CanGoBack

void CanGoBack(Action<bool> callback)

Like the other version of CanGoBack(), except it uses a callback instead of a Task in order to be compatible with versions of .NET before 4.0.

CanGoForward

Task<bool> CanGoForward()

Checks whether the webview can go forward with a call to GoForward().

CanGoForward

void CanGoForward(Action<bool> callback)

Like the other version of CanGoForward(), except it uses a callback instead of a Task in order to be compatible with versions of .NET before 4.0.

Click

void Click(Vector2 point)

Clicks at the given point in the webpage. The x and y components of the point are values between 0 and 1 that are normalized to the width and height, respectively. For example, point.x = x in Unity units / width in Unity units. Like in the browser, the origin is in the upper-left corner, the positive direction of the y-axis is down, and the positive direction of the x-axis is right.

DisableViewUpdates

void DisableViewUpdates()

Disables the webview from rendering to its texture.

Dispose

void Dispose()

Destroys the webview, releasing all of its resources.

EnableViewUpdates

void EnableViewUpdates()

Re-enables rendering after a call to DisableViewUpdates().

ExecuteJavaScript

void ExecuteJavaScript(string javaScript)

Executes the given script in the context of the webpage's main frame.

Focus

void Focus()

Makes the webview take focus. Currently, this method is only needed on iOS.

GoBack

void GoBack()

Navigates back to the previous page in the webview's history.

GoForward

void GoForward()

Navigates forward to the next page in the webview's history.

HandleKeyboardInput

void HandleKeyboardInput(string key)

Executes the given script in the context of the webpage's main frame. A key is either a single character representing a unicode character (e.g. 'A', 'b', '?') or multiple characters representing a control key (e.g. 'ArrowUp', 'ArrowLeft').

Init

void Init(Texture2D viewportTexture, float width, float height, Texture2D videoTexture)

Initializes a newly created webview with the given viewport and video textures created with Web.CreateTexture() and the dimensions in Unity units. Note that WebViewPrefab automatically calls this method for you, so you only need to invoke this directly if you manually create a WebView outside of the prefab using Web.CreateWebView().

Init

void Init(Texture2D viewportTexture, float width, float height)

Like the other Init() method, but with video support disabled.

LoadHtml

void LoadHtml(string html)

Loads the webpage contained in the given HTML string.

Example:

webView.LoadHtml(@"<!DOCTYPE html>
  <html>
    <head>
      <title>Test Page</title>
      <style>
        h1 {
          font-family: Helvetica, Arial, Sans-Serif;
        }
      </style>
    </head>
    <body>
      <h1>LoadHtml Example</h1>
      <script>
        console.log('This page was loaded!');
      </script>
    </body>
  </html>"
);

LoadUrl

void LoadUrl(string url)

Navigates to the given URL. Note that this can also be a file URL that points to a local file in the Android or iOS application bundle. For example, you can assemble a file URL on Android if you know the path to the resource:

file:///android_asset/my-static-files/my-webpage.html

On iOS, you must ask the system for a file URL to a given bundle resource. iOSWebView has a static GetFileUrlForBundleResource() method that can do this for you.

PostMessage

void PostMessage(string data)

Posts a message that JavaScript within the webview can listen for using window.vuplex.addEventListener('message', function(message) {}). The provided data string is passed as the data property of the message object.

Reload

void Reload()

Reloads the current page.

Resize

void Resize(float width, float height)

Resizes the webview to the dimensions given in Unity units

Scroll

void Scroll(Vector2 scrollDelta)

Scrolls the webview's top-level body by the given delta.

If you want to scroll a specific section of the page, see Scroll(Vector2 scrollDelta, Vector2 mousePosition) instead.

scrollDelta is in Unity units, and because the browser's origin is in the upper-left corner, y-axis' positive direction is down, and the x-axis' positive direction is right.

Scroll

void Scroll(Vector2 scrollDelta, Vector2 mousePosition)

Scrolls by the given delta at the given mouse position.

scrollDelta is in Unity units, and because the browser's origin is in the upper-left corner, the y-axis' positive direction is down, and the x-axis' positive direction is right.

The x and y components of mousePosition are values between 0 and 1 that are normalized to the width and height, respectively. For example, point.x = x in Unity units / width in Unity units.

ZoomIn

void ZoomIn()

Zooms into the currently loaded web content. Note that the zoom level gets reset to 100% when you load a new page.

ZoomOut

void ZoomOut()

Zooms back out after a previous call to ZoomIn(). Note that the zoom level gets reset to 100% when you load a new page.

Public events

LoadProgressChanged

EventHandler<ProgressChangedEventArgs> LoadProgressChanged

Indicates that the page load percentage changed.

MessageEmitted

EventHandler<EventArgs<string>> MessageEmitted

Indicates that JavaScript running in the page used the window.vuplex.postMessage JavaScript API to emit a message to the Unity application.

JavaScript example for sending a message:

function sendMessageToCSharp() {
  // This object passed to `postMessage()` is automatically serialized as JSON
  // and is emitted via the C# MessageEmitted event. This API mimics the window.postMessage API.
  window.vuplex.postMessage({ type: 'greeting', message: 'Hello from JavaScript!' });
}

if (window.vuplex) {
  // The window.vuplex object has already been initialized after page load,
  // so we can go ahead and send the message.
  sendMessageToCSharp();
} else {
  // The window.vuplex object hasn't been initialized yet because the page is still
  // loading, so add an event listener to send the message once it's initialized.
  window.addEventListener('vuplexready', sendMessageToCSharp);
}

PageLoadFailed

EventHandler PageLoadFailed

Indicates that the page failed to load. This can happen, for instance, if DNS is unable to resolve the hostname.

TextureChanged

EventHandler<EventArgs<Texture2D>> TextureChanged

Indicates that an update occurred that caused the webview to discard its existing texture and start using the new texture emitted in this event.

Currently, this event is only used for iOS when a resize occurs (it's not used on Android). WebViewPrefab automatically handles this event by applying thew new texture to the ViewportMaterialView. If you're not using the prefab, then you'll have to manually handle this event like it does.

TitleChanged

EventHandler<EventArgs<string>> TitleChanged

Indicates that the page's title changed.

UrlChanged

EventHandler<UrlChangedEventArgs> UrlChanged

Indicates that the URL of the webview changed, either due to user interaction or JavaScript.

VideoRectChanged

EventHandler<EventArgs<Rect>> VideoRectChanged

Indicates that the rect of the playing video changed. Note that WebViewPrefab automatically handles this event for you.

VideoTextureChanged

EventHandler<EventArgs<Texture2D>> VideoTextureChanged

Indicates that an update occurred that caused the webview to discard its existing video texture and start using the new texture emitted in this event. Note that WebViewPrefab automatically handles this event for you.