iOSWebView is the IWebView implementation used by 3D WebView for iOS. It also includes additional APIs for iOS-specific functionality.
IntPtr GetNativeWebView()
Returns a pointer to the instance's native Objective-C WKWebView.
// Example of defining a native Objective-C function that sets WKWebView.allowsLinkPreview.
// Place this in a .m file in your project, like Assets/Plugins/WebViewCustom.m
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
void WebViewCustom_SetAllowsLinkPreview(WKWebView *webView, BOOL allowsLinkPreview) {
webView.allowsLinkPreview = allowsLinkPreview;
}
// Example of calling the native Objective-C function from C#.
async void EnableLinkPreviews(WebViewPrefab webViewPrefab) {
await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var wkWebViewPtr = (webViewPrefab.WebView as iOSWebView).GetNativeWebView();
WebViewCustom_SetAllowsLinkPreview(wkWebViewPtr, true);
#endif
}
[System.Runtime.InteropServices.DllImport("__Internal")]
static extern void WebViewCustom_SetAllowsLinkPreview(System.IntPtr webViewPtr, bool allowsLinkPreview);
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.
void SetAllowsBackForwardNavigationGestures(bool allow)
Sets whether horizontal swipe gestures trigger backward and forward page navigation. The default is false
.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = webViewPrefab.Webview as iOSWebView;
iOSWebViewInstance.SetAllowsBackForwardNavigationGestures(true);
#endif
static void SetAllowsInlineMediaPlayback(bool allow)
Sets whether HTML5 videos play inline or use the native full-screen controller. The default is true
. This method is static because the WKWebView's configuration cannot be modified at runtime after the webview is created.
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetAllowsInlineMediaPlayback(false);
#endif
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.
void Awake() {
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetCameraEnabled(true);
#endif
}
void SetLongPressGesturesEnabled(bool enabled)
When Native 2D Mode is enabled, this method sets whether long press gestures are enabled. The default is true
. When Native 2D Mode is not enabled, this method has no effect.
await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetLongPressGesturesEnabled(false);
#endif
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.
void Awake() {
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetMicrophoneEnabled(true);
#endif
}
static void SetNativeParentView(IntPtr nativeParentView)
3D WebView for iOS is powered by native WKWebView instances, and it must add those instances to the native iOS view hierarchy in order for them to work correctly. By default, 3D WebView adds the native WKWebView instances as children of the Unity view controller's view. However, you can call this method at the start of the app to override the native UIView to which 3D WebView adds the native WKWebView instances. For example, you may need to do this if your app embeds Unity as a library. The nativeParentView
parameter is a pointer to an Objective-C UIView (UIView *
).
void Awake() {
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetNativeParentView(nativeParentView);
#endif
}
void SetScrollViewBounces(bool bounces)
When Native 2D Mode is enabled, this method sets whether the scroll view bounces past the edge of content and back again. The default is true
. When Native 2D Mode is not enabled, this method has no effect.
await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = canvasWebViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetScrollViewBounces(false);
#endif
See also: UIScrollView.bounces
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 iOS 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.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_IOS && !UNITY_EDITOR
var iOSWebViewInstance = webViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetTargetFrameRate(15);
#endif