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

Resolution

int Resolution

The webview's resolution in pixels per Unity unit.

Size

Vector2 Size

The webview's current size in Unity units.

SizeInPixels

Vector2 SizeInPixels

The webview's current size in pixels.

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.

CaptureScreenshot

Task<bytes[]> CaptureScreenshot()

Returns a PNG image of the content visible in the webview. Note that screenshots do not include video content, which appears black.

CaptureScreenshot

void CaptureScreenshot(Action<bytes[]> callback)

Like the other version of CaptureScreenshot(), 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.

GetRawTextureData

Task<byte[]> GetRawTextureData()

A replacement for Texture2D.GetRawTextureData() for IWebView.Texture. Note that the texture data excludes video content, which appears black.

Unity's Texture2D.GetRawTextureData() currently does not work for textures created with Texture2D.CreateExternalTexture(). So, this method serves as a replacement by providing the equivalent functionality. You can load the bytes returned by this method into another texture using Texture2D.LoadRawTextureData().

GetRawTextureData

void GetRawTextureData(Action<byte[]> callback)

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

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)

Dispatches a keystroke to the webview. A key can either be a single character representing a unicode character (e.g. "A", "b", "?") or a JavaScript Key value (e.g. "ArrowUp", "Enter").

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.

LoadUrl

void LoadUrl(string url, Dictionary<string, string> additionalHttpHeaders)

Like LoadUrl(string url), but also sends the given HTTP request headers when loading the URL.

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. Note that if you're using WebViewPrefab, you should call WebViePrefab.Resize() instead.

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.

SetResolution

void SetResolution(int pixelsPerUnityUnit)

Sets the webview's resolution in pixels per Unity unit.

The default resolution is 1300 pixels per Unity unit. This method is useful, for example, if you want web content to appear larger or smaller. Setting a lower resolution decreases the pixel density, but has the effect of making web content appear larger. Setting a higher resolution increases the pixel density, but has the effect of making content appear smaller.

ZoomIn

void ZoomIn()

Zooms into the currently loaded web content. Note that the zoom level gets reset when a new page is loaded.

ZoomOut

void ZoomOut()

Zooms back out after a previous call to ZoomIn(). Note that the zoom level gets reset when a new page is loaded.

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.