WebGLWebView

class : IWebView

Namespace: Vuplex.WebView

Additional feature interfaces: ?

WebGLWebView is the IWebView implementation used by 2D WebView for WebGL. It also includes additional APIs for WebGL-specific functionality.

Summary

Public properties

Public properties

IFrameElementID

string IFrameElementID

Gets the unique id attribute of the webview's <iframe> element.

Example

await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_WEBGL && !UNITY_EDITOR
    var webGLWebView = canvasWebViewPrefab.WebView as WebGLWebView;
    Debug.Log("IFrame ID: " + webGLWebView.IFrameElementID);
#endif

Public methods

CanAccessIFrameContent

bool CanAccessIFrameContent()

Indicates whether 2D WebView can access the content in the webview's iframe. If the iframe's content can't be accessed, then most of the IWebView APIs become disabled. For more information, please see this article.

Example

await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_WEBGL && !UNITY_EDITOR
    var webGLWebView = canvasWebViewPrefab.WebView as WebGLWebView;
    if (webGLWebView.CanAccessIFrameContent()) {
        Debug.Log("The iframe content can be accessed 👍");
    }
#endif

ExecuteJavaScriptLocally

static string ExecuteJavaScriptLocally(string javaScript)

Executes the given JavaScript locally in the Unity app's window and returns the result.

Example

#if UNITY_WEBGL && !UNITY_EDITOR
    // Gets the Unity app's web page title.
    var title = WebGLWebView.ExecuteJavaScriptLocally("document.title");
    Debug.Log("Title: " + title);
#endif

SetDevicePixelRatio

static void SetDevicePixelRatio(float devicePixelRatio)

Overrides the value of devicePixelRatio that 2D WebView uses to determine the correct size and coordinates of webviews.

Starting in Unity 2019.3, Unity scales the UI by default based on the browser's window.devicePixelRatio value. However, it's possible for an application to override the devicePixelRatio value by passing a value for config.devicePixelRatio to Unity's createUnityInstance() JavaScript function. In some versions of Unity, the default index.html template sets config.devicePixelRatio = 1 on mobile. In order for 2D WebView to size and position webviews correctly, it must determine the value of devicePixelRatio to use. Since there's no API to get a reference to the Unity instance that the application created with createUnityInstance(), 2D WebView tries to detect if config.devicePixelRatio was passed to createUnityInstance() by checking for the presence of a global config JavaScript variable. If there is a global variable named config with a devicePixelRatio property, then 2D WebView uses that value, otherwise it defaults to using window.devicePixelRatio. This approach works for Unity's default index.html templates, but it may not work if the application uses a custom HTML template or changes the name of the config variable in the default template. In those cases, the application can use this method to pass the overridden value of devicePixelRatio to 2D WebView.

Example

void Awake() {
    #if UNITY_WEBGL && !UNITY_EDITOR
        WebGLWebView.SetDevicePixelRatio(1);
    #endif
}

SetFullscreenEnabled

static void SetFullscreenEnabled(bool enabled)

Sets whether the JavaScript Fullscreen API is enabled for webviews. The default is false.

Example

void Awake() {
    #if UNITY_WEBGL && !UNITY_EDITOR
        WebGLWebView.SetFullscreenEnabled(true);
    #endif
}

SetGeolocationEnabled

static void SetGeolocationEnabled(bool enabled)

Sets whether the JavaScript Geolocation API is enabled for webviews. The default is false.

Example

void Awake() {
    #if UNITY_WEBGL && !UNITY_EDITOR
        WebGLWebView.SetGeolocationEnabled(true);
    #endif
}

SetScreenSharingEnabled

static void SetScreenSharingEnabled(bool enabled)

Sets whether screen sharing is enabled for webviews. The default is false.

Example

void Awake() {
    #if UNITY_WEBGL && !UNITY_EDITOR
        WebGLWebView.SetScreenSharingEnabled(true);
    #endif
}

SetUnityContainerElementId

static void SetUnityContainerElementId(string containerId)

Explicitly sets the HTML element that 2D WebView should use as the Unity app container. 2D WebView automatically detects the Unity app container element if its ID is set to one of the default values of "unityContainer" or "unity-container". However, if your app uses a custom WebGL template that uses a different ID for the container element, you must call this method to set the container element ID. 2D WebView for WebGL works by adding <iframe> elements to the app container, so it's unable to function correctly if it's unable to find the Unity app container element.

Example

void Awake() {
    #if UNITY_WEBGL && !UNITY_EDITOR
        WebGLWebView.SetUnityContainerElementId("your-custom-id");
    #endif
}