MacWebKitWebView

class : IWebView

Namespace: Vuplex.WebView

MacWebKitWebView is the optional WebKit-based implementation of IWebView in 3D WebView for Windows and macOS. It's powered by macOS's built-in WKWebView component and also includes additional APIs for WebKit-specific functionality. For details about the WebKit plugin and how to enable it, please see this article.

Summary

Public properties

PixelDensityEnabled

static bool PixelDensityEnabled

By default, support for changing the webview's pixel density (via WebViewPrefab.PixelDensity or IWithPixelDensity) is disabled for the macOS WebKit plugin because it negatively impacts performance, particularly on Macs with Intel processors. However, if you wish to override this to enable changing the pixel density, you can do so by setting this field to true. The default is false.

Example

void Awake() {
    #if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
        MacWebKitWebView.PixelDensityEnabled = true;
    #endif
}

Public methods

GetNativeWebView

IntPtr GetNativeWebView()

Returns an Objective-C pointer to the instance's underlying native WKWebView. The application can use this to utilize native macOS APIs for which 3D WebView doesn't yet have dedicated C# equivalents. To utilize the pointer, the application must pass it to a function defined in native macOS library (.bundle file) like illustrated in the example below.

Objective-C Example

// Example of defining a native Objective-C function that sets WKWebView.allowsLinkPreview.
// Compile this function into a .bundle library file and include it in your project.
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>

void WebViewCustom_setAllowsLinkPreview(WKWebView *webView, BOOL allowsLinkPreview) {

    webView.allowsLinkPreview = allowsLinkPreview;
}

C# Example

// Example of calling the native Objective-C function from C#.
async void EnableLinkPreviews(WebViewPrefab webViewPrefab) {

    await webViewPrefab.WaitUntilInitialized();
    #if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
        var macWebView = webViewPrefab.WebView as MacWebKitWebView;
        var wkWebViewPtr = macWebView.GetNativeWebView();
        WebViewCustom_setAllowsLinkPreview(wkWebViewPtr, true);
    #endif
}

[System.Runtime.InteropServices.DllImport("YourPluginName.bundle")]
static extern void WebViewCustom_setAllowsLinkPreview(System.IntPtr webViewPtr, bool allowsLinkPreview);

Important note

Adding code that interacts with the native WKWebView directly may interfere with 3D WebView's functionality and vice versa. So, it's highly recommended to stick to 3D WebView's C# APIs whenever possible and only use GetNativeWebView() if truly necessary. If 3D WebView is missing an API that you need, feel free to contact us.

SetAllowFileAccessFromFileUrls

void SetAllowFileAccessFromFileUrls(bool allow)

Sets whether cross-origin requests in the context of a file scheme URL should be allowed to access content from other file scheme URLs. The default value is false. Note that some accesses such as image HTML elements don't follow same-origin rules and aren't affected by this setting.

Example

await webViewPrefab.WaitUntilInitialized();
#if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
    var macWebView = webViewPrefab.WebView as MacWebKitWebView;
    macWebView.SetAllowFileAccessFromFileUrls(true);
#endif

SetAllowsBackForwardNavigationGestures

void SetAllowsBackForwardNavigationGestures(bool allow)

When Native 2D Mode is enabled, this method sets whether horizontal swipe gestures trigger backward and forward page navigation. The default is false. When Native 2D Mode is disabled, this method has no effect.

Example

await canvasWebViewPrefab.WaitUntilInitialized();
#if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
    var macWebView = canvasWebViewPrefab.WebView as MacWebKitWebView;
    macWebView.SetAllowsBackForwardNavigationGestures(true);
#endif

SetCameraEnabled

static void SetCameraEnabled(bool enabled)

Like Web.SetCameraAndMicrophoneEnabled(), but enables only the camera without enabling the microphone. In addition to calling this method, you must also complete the additional steps described here in order to successfully enable the camera.

Example

void Awake() {
    #if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
        MacWebKitWebView.SetCameraEnabled(true);
    #endif
}

SetFullscreenEnabled

void SetFullscreenEnabled(bool enabled)

When Native 2D Mode is enabled, this method sets whether web pages can use the JavaScript Fullscreen API to make an HTML element occupy the device's entire screen. The default is true, meaning that the JavaScript Fullscreen API is enabled by default. When Native 2D Mode is disabled, this method has no effect because the JavaScript Fullscreen API is only supported in Native 2D Mode.

Example

#if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
    await canvasWebViewPrefab.WaitUntilInitialized();
    var macWebView = canvasWebViewPrefab.WebView as MacWebKitWebView;
    // Disable the JavaScript Fullscreen API.
    macWebView.SetFullscreenEnabled(false);
#endif

SetMicrophoneEnabled

static void SetMicrophoneEnabled(bool enabled)

Like Web.SetCameraAndMicrophoneEnabled(), but enables only the microphone without enabling the camera. In addition to calling this method, you must also complete the additional steps described here in order to successfully enable the microphone.

Example

void Awake() {
    #if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
        MacWebKitWebView.SetMicrophoneEnabled(true);
    #endif
}

SetTargetFrameRate

void SetTargetFrameRate(uint targetFrameRate)

Sets the target web frame rate. The default is 30, which is also the maximum value. This method can be used to lower the target web frame rate in order to decrease energy and CPU usage. 3D WebView's rendering speed is limited by the speed of the underlying macOS APIs, so the actual web frame rate achieved is always lower than the default target of 30 FPS. This method is only used for the default render mode and is ignored when Native 2D Mode is enabled.

Example

await webViewPrefab.WaitUntilInitialized();
#if (UNITY_STANDALONE_OSX && !UNITY_EDITOR) || UNITY_EDITOR_OSX
    var macWebView = webViewPrefab.WebView as MacWebKitWebView;
    macWebView.SetTargetFrameRate(15);
#endif