AndroidWebView is the IWebView implementation used by 3D WebView for Android. It also includes additional APIs for Android-specific functionality.
void ClearHistory()
Clears the webview's back / forward navigation history.
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.ClearHistory();
#endif
AndroidJavaObject GetNativeWebView()
Returns the instance's underlying native android.webkit.WebView. The application can use this to utilize native Android APIs for which 3D WebView doesn't yet have dedicated C# equivalents.
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
var nativeWebView = androidWebView.GetNativeWebView();
// Call the android.webkit.WebView.findAll() method to search for the letter "a".
// https://developer.android.com/reference/android/webkit/WebView#findAll(java.lang.String)
// Most native WebView methods must be called on the Android UI thread.
AndroidWebView.RunOnAndroidUIThread(() => {
nativeWebView.Call<int>("findAll", "a");
});
#endif
Warning: Adding code that interacts with the native WebView 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.
static bool IsWebViewAvailable()
Indicates whether the Android System WebView package is installed on the system and available.
3D WebView internally depends on Android's System WebView package, which is normally installed as part of the operating system. In rare circumstances, the Android System WebView package may be unavailable. For example, this can happen if the user used developer tools to delete the System WebView package or if updates to the System WebView package are currently being installed.
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("WebView is available: " + AndroidWebView.IsWebViewAvailable());
#endif
void LoadHtml(string html, string baseUrl)
Like IWebView.LoadHtml(), but also allows a virtual base URL to be specified. Setting a base URL allows, for example, for additional resources like CSS and JavaScript files to be referenced via a relative path.
// Load some HTML that references a javascript.js file
// located in Application.persistentDataPath.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
var persistentDataPathFileUrl = "file://" + Application.persistentDataPath;
androidWebView.LoadHtml(
@"<div>
<script src='javascript.js'></script>
<h1>Hello!</h1>
</div>",
persistentDataPathFileUrl
);
#endif
void Pause()
Pauses media and rendering for this webview instance until Resume() is called.
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.Pause();
#endif
static void PauseAll()
Pauses media and rendering for all webview instances until ResumeAll() is called. 3D WebView automatically calls this method when the application is paused. Note that this method does not pause JavaScript. To pause JavaScript globally, use PauseTimers().
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.PauseAll();
#endif
static void PauseTimers()
Pauses all layout, parsing, and JavaScript timers for all webviews until ResumeTimers() is called. This is a global request that affects all Android WebView instances in the application, even those not created by 3D WebView (for example, webviews used by other 3rd party libraries such as ad SDKs).
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.PauseTimers();
#endif
void PostUrl(string url, byte[] data)
Loads the given URL using an HTTP POST request and the given application/x-www-form-urlencoded data.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.PostUrl("https://postman-echo.com/post", Encoding.Unicode.GetBytes("foo=bar"));
#endif
void Resume()
Resumes rendering for this webview instance after a previous call to Pause().
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.Resume();
#endif
static void ResumeAll()
Resumes processing and rendering for all webview instances after a previous call to PauseAll(). 3D WebView automatically calls this method when the application resumes after having been paused.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.ResumeAll();
#endif
static void ResumeTimers()
Resumes all layout, parsing, and JavaScript timers for all webviews after a previous call to PauseTimers().
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.ResumeTimers();
#endif
static void RunOnAndroidUIThread(Action function)
Runs the given function on the Android UI thread.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.RunOnAndroidUIThread(() => {
// TODO: Do something on the Android UI thread.
});
#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_ANDROID && !UNITY_EDITOR
AndroidWebView.SetCameraEnabled(true);
#endif
}
static void SetDrmEnabled(bool enabled)
Enables WideVine DRM. DRM is disabled by default because it could potentially be used for tracking. You can verify that DRM is enabled by using the DRM Stream Test on this page.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetDrmEnabled(true);
#endif
void SetForceDark(ForceDark forceDark)
Sets the force dark mode for this WebView. Note that this API is only supported on Android API level >= 29 and is ignored in older versions of Android.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetForceDark(ForceDark.On);
#endif
void SetFullscreenEnabled(bool enabled)
Sets whether web pages can use the JavaScript Fullscreen API to make an HTML element occupy the entire webview in 3D rendering mode or the entire device screen in Native 2D Mode. The default is true
, meaning that the JavaScript Fullscreen API is enabled by default.
#if UNITY_ANDROID && !UNITY_EDITOR
await webViewPrefab.WaitUntilInitialized();
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetFullscreenEnabled(false);
#endif
static void SetGeolocationEnabled(bool enabled)
By default, web pages cannot access the device's geolocation via JavaScript, even if the user has granted the app permission to access location. Invoking SetGeolocationEnabled(true)
allows all web pages to access the geolocation if the user has granted the app location permissions via the standard Android permission dialogs.
The following Android permissions must be included in the app's AndroidManifest.xml and also requested by the application at runtime:
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetGeolocationEnabled(true);
#endif
Geolocation doesn't work on Meta Quest devices because they lack GPS support.
void SetInitialScale(float scale)
Sets the initial scale for web content, where 1.0 is the default scale.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetInitialScale(1.75f);
#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_ANDROID && !UNITY_EDITOR
AndroidWebView.SetMicrophoneEnabled(true);
#endif
}
static void SetMidiSysexEnabled(bool enabled)
Enables sysex messages to be sent to or received from MIDI devices. The default is disabled. These messages are privileged operations, e.g. modifying sound libraries and sampling data, or even updating the MIDI device's firmware.
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetMidiSysexEnabled(true);
#endif
}
void SetMixedContentMode(MixedContentMode mode)
Configures the webview's behavior when a secure origin attempts to load a resource from an insecure origin. The default mode is MixedContentMode.CompatibilityMode.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetMixedContentMode(MixedContentMode.AlwaysAllow);
#endif
void SetNativeFileSelectionEnabled(bool enabled)
By default, a native file picker is shown for file inputs, but this method can be used to disable it. Note that the screen orientation of the native file picker UI is determined by the "Auto-rotate screen" preference in the device's Settings app.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetNativeFileSelectionEnabled(false);
#endif
static void SetNativeParentView(AndroidJavaObject nativeParentView)
3D WebView for Android is powered by native android.webkit.WebView instances, and it must add those instances to the native Android view hierarchy in order for them to work correctly. By default, 3D WebView adds the native WebView instances as children of the Unity game's ViewGroup, which is obtained using this approach in Java:
ViewGroup parentViewGroup = (ViewGroup)UnityPlayer.currentActivity.getWindow().getDecorView().getRootView();
However, you can call this method at the start of the app to override the ViewGroup to which
3D WebView adds the native WebView instances. For example, you may need to do this if your app
embeds Unity as a library, which may cause UnityPlayer.currentActivity
to return a different
activity than expected.
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetNativeParentView(nativeParentView);
#endif
}
static void SetRemoteDebuggingEnabled(bool enabled)
When using 3D rendering mode (i.e. when Native 2D Mode is disabled), remote debugging is enabled by default on Android because some APIs like MovePointer() and SendKey() work better with remote debugging enabled. However, this method can be used to explicitly disable remote debugging. When running in Native 2D Mode, remote debugging is disabled by default.
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetRemoteDebuggingEnabled(false);
#endif
}
void SetScrollbarsEnabled(bool enabled)
When Native 2D Mode is enabled, this method sets whether scrollbars are enabled. The default is true
. When Native 2D Mode is not enabled, this method has no effect.
await canvasWebViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = canvasWebViewPrefab.WebView as AndroidWebView;
androidWebView.SetScrollbarsEnabled(false);
#endif
void SetSurface(IntPtr surface)
Sets the Surface to which the webview renders. This can be used, for example, to render to a Meta Quest OVROverlay. When the application invokes this method with a valid surface, the webview renders to that given surface instead of rendering to its original texture surface (so IWebView.Texture is no longer updated). If the application invokes this method with a null parameter, it causes the webview to revert back to rendering to its original texture surface.
await webViewPrefab.WaitUntilInitialized();
var surface = ovrOverlay.externalSurfaceObject;
webViewPrefab.Resize(ovrOverlay.externalSurfaceWidth, ovrOverlay.externalSurfaceHeight);
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetSurface(surface);
#endif
void SetTextZoom(int textZoom)
Sets the text zoom of the page in percent. For example, the browser engine automatically adjusts the size of web pages' text by default based on the "Font size" preference in the device's Settings app, but you can use SetTextZoom(100)
to force that system font size preference to be ignored.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.SetTextZoom(100);
#endif
See also: android.webkit.WebSettings.setTextZoom()
void ZoomBy(float zoomFactor)
Zooms in or out by the given factor, which is multiplied by the current zoom level to reach the new zoom level. zoomFactor
must be in the range from 0.01 to 100.0. Note that the zoom level gets reset when a new page is loaded.
// Zoom by 1.75 after the page finishes loading.
await webViewPrefab.WaitUntilInitialized();
webViewPrefab.WebView.LoadProgressChanged += (sender, eventArgs) => {
if (eventArgs.Type == ProgressChangeType.Finished) {
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.ZoomBy(1.75f);
#endif
}
};
EventHandler<EventArgs<AndroidJavaObject>> ClientCertificateRequested
An event that maps directly to the native Android onReceivedClientCertRequest() API for handling client certificate requests. EventArgs.Value is an AndroidJavaObject for the native ClientCertRequest object. For details on how to handle certificate requests, see Android's documentation for onReceivedClientCertRequest() and ClientCertRequest.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.ClientCertificateRequested += (sender, eventArgs) => {
var clientCertRequest = eventArgs.Value;
var host = clientCertRequest.Call<string>("getHost");
Debug.Log("Client certificate requested for host: " + host);
// TODO: This example calls ClientCertRequest.ignore() to ignore the request,
// but you're probably intending to use this event to pass a client certificate,
// in which case you'll want to use native Android APIs (like KeyChain.getPrivateKey)
// to look up a private key and call ClientCertRequest.proceed() instead.
clientCertRequest.Call("ignore");
};
#endif
EventHandler<ScriptDialogEventArgs> ScriptAlerted
Event raised when a script in the page calls window.alert(). If no handler is attached to this event, then window.alert()
will return immediately and the script will continue execution. If a handler is attached to this event, then script execution will be paused until the event args' Continue() callback is called.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.ScriptAlerted += (sender, eventArgs) => {
Debug.Log("Script alerted: " + eventArgs.Message);
eventArgs.Continue();
};
#endif
EventHandler<ScriptDialogEventArgs<bool>> ScriptConfirmRequested
Event raised when a script in the page calls window.confirm(). If no handler is attached to this event, then window.confirm()
will return false
immediately and the script will continue execution. If a handler is attached to this event, then script execution will be paused until the event args' Continue() callback is called, and window.confirm()
will return the value passed to Continue().
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.ScriptConfirmRequested += (sender, eventArgs) => {
Debug.Log("Script confirm requested: " + eventArgs.Message);
eventArgs.Continue(true);
};
#endif