IWebView

interface

IWebView is the primary interface for loading and interacting with web content.

Summary

Public methods

Blur

void Blur()

Makes the webview relinquish focus.

Click

void Click(Vector2 point)

Clicks at the given point in the webpage. The point's coordinates are relative to the origin at the page's top left corner. Its x and y components 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.

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.

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 texture, float width, float height)

Initializes a newly created webview given a texture created by ITextureCreator and the dimensions in Unity units.

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

ZoomIn

void ZoomIn()

Zooms into the web content.

ZoomOut

void ZoomOut()

Zooms back out after a previous call to ZoomIn().

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);
}

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.