CanvasWebViewPrefab

class : MonoBehaviour

CanvasWebViewPrefab is a prefab that makes it easy to create and interact with an IWebView in a Unity Canvas. 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.

Important notes:

  • The Canvas's Render Mode must be set to either "Screen Space - Camera" or "World Space". "Screen Space - Overlay" currently is not supported.
  • A Physics Raycaster must be attached to the camera to enable click events.

You can create a CanvasWebViewPrefab one of the following ways:

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

CanvasWebViewPrefab 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

InitialUrl

string InitialUrl

If you drag a CanvasWebViewPrefab.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.

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

Init

void Init()

Asynchronously initializes the instance.

You only need to call this method if you place a CanvasWebViewPrefab.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, this method 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( WebViewOptions options)

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

Instantiate

static CanvasWebViewPrefab Instantiate()

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

Example:
var canvas = GameObject.Find("Canvas");
canvasWebViewPrefab = CanvasWebViewPrefab.Instantiate();
canvasWebViewPrefab.transform.parent = canvas.transform;
var rectTransform = canvasWebViewPrefab.transform as RectTransform;
rectTransform.anchoredPosition3D = Vector3.zero;
rectTransform.offsetMin = Vector2.zero;
rectTransform.offsetMax = Vector2.zero;
canvasWebViewPrefab.transform.localScale = Vector3.one;
canvasWebViewPrefab.Initialized += (sender, e) => {
    canvasWebViewPrefab.WebView.LoadUrl("https://vuplex.com");
};

Instantiate

static CanvasWebViewPrefab Instantiate( WebViewOptions options)

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

Public events

Initialized

EventHandler Initialized

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