WebViewPrefab

class : MonoBehaviour

WebViewPrefab is a prefab that makes it easy to create and interact with an IWebView in world space. It takes care of creating and initializing an IWebView, displaying its texture, and handling click and scroll interactions from the user. So, all you need to do is load some web content from a URL or HTML string, and then the user can view and interact with it.

You can create a WebViewPrefab one of the following ways:

  • By dragging WebViewPrefab.prefab into a scene via the editor and setting its "Initial URL" property.
  • By instantiating an instance at runtime with WebViewPrefab.Instantiate() and then waiting for its Initialized event to be raised, after which you can call methods on its WebView property.

WebViewPrefab handles standard events from Unity's input system (like IPointerDownHandler), so it works with input modules that extend Unity's BaseInputModule, like Unity's Standalone Input Module and third-party modules.

If your use case requires a high degree of customization, you can instead create an IWebView outside of the prefab with Web.CreateWebView().

Summary

Public properties

ClickingEnabled

bool ClickingEnabled

Clicking is enabled by default, but can be disabled by setting this property to false.

DragToScrollThreshold

float DragToScrollThreshold

In order to prevent dragging-to-scroll from triggering unwanted clicks, a click is discarded if the pointer drags beyond this threshold before a "pointer up" event occurs. The default threshold is 0.15.

InitialUrl

string InitialUrl

If you drag a WebViewPrefab.prefab into the scene via the editor, you can set this property in the editor to make it so that the instance automatically initializes itself with the given URL.

ScrollSensitivity

static float ScrollSensitivity

Allows the scroll sensitivity to be adjusted. The default sensitivity is 0.001.

ScrollingEnabled

bool ScrollingEnabled

Scrolling is enabled by default, but can be disabled by setting this property to false.

Visible

bool Visible

Controls whether the instance is visible or hidden.

WebView

IWebView WebView

A reference to the prefab's IWebView instance, which is available after the Initialized event is raised. Before initialization is complete, this property is null.

Public methods

Destroy

void Destroy()

Destroys the instance and its children. Note that you don't need to call this method if you destroy the instance's parent with GameObject.Destroy().

Init

void Init()

Asynchronously initializes the instance using the width and height set via the Unity editor.

You only need to call this method if you place a WebViewPrefab.prefab in your scene via the Unity editor and don't set its "Initial URL" property. You don't need to call this method if you set the "Initial URL" property in the editor or if you instantiate the prefab programmatically at runtime using Instantiate(). In those cases, Init() is called automatically for you. This method asynchronously initializes the WebView property, which is available for use after the Initialized event is raised.

Init

void Init(float width, float height)

Like Init(), except it initializes the instance to the specified width and height in Unity units.

Init

void Init(float width, float height, WebViewOptions options)

Like Init(float width, float height), except it also accepts an object of options flags that can be used to alter the webview's behavior.

Instantiate

static WebViewPrefab Instantiate(float width, float height)

Creates a new instance with the given dimensions in Unity units and initializes it asynchronously. The WebView property is available after initialization completes, which is indicated by the Initialized event.

Example:
// Create a 0.5 x 0.5 instance
var webViewPrefab = WebViewPrefab.Instantiate(0.5f, 0.5f);
// Position the prefab how we want it
webViewPrefab.transform.parent = transform;
webViewPrefab.transform.localPosition = new Vector3(0, 0f, 0.5f);
webViewPrefab.transform.LookAt(transform);
// Load a URL once the prefab finishes initializing
webViewPrefab.Initialized += (sender, e) =>; {
    webViewPrefab.WebView.LoadUrl("https://vuplex.com");
};

Instantiate

static WebViewPrefab Instantiate(float width, float height, WebViewOptions options)

Like Instantiate(float width, float height), except it also accepts an object of options flags that can be used to tweak the generated webview's behavior.

Resize

void Resize(float width, float height)

Resizes the prefab mesh and webview to the given dimensions in Unity units.

Public events

Clicked

EventHandler Clicked
Indicates that the prefab was clicked. Note that the prefab automatically calls the IWebView.Click() method for you.

Initialized

EventHandler Initialized

Indicates that the prefab finished initializing, so its WebView property is ready to use.