AndroidGeckoWebView is the IWebView implementation used by 3D WebView for Android with Gecko Engine. It also includes additional APIs for Gecko-specific functionality.
static void EnsureBuiltInExtension(string uri, string id)
Installs an extension using GeckoView's WebExtensionController.ensureBuiltIn() method. The extension is not re-installed if it's already present and has the same version.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.EnsureBuiltInExtension(
"resource://android/assets/your-extension/",
"example@example.com"
);
#endif
static AndroidJavaObject GetGeckoRuntime()
Gets the native GeckoRuntime used by 3D WebView. If the GeckoRuntime hasn't been created yet, calling this method causes it to be created.
#if UNITY_ANDROID && !UNITY_EDITOR
var geckoRuntime = await AndroidGeckoWebView.GetGeckoRuntime();
var settings = geckoRuntime.Call<AndroidJavaObject>("getSettings");
var fontSizeScaleFactor = settings.Call<float>("getFontSizeFactor");
Debug.Log("font size scale factor: " + fontSizeScaleFactor);
#endif
Warning: Adding code that interacts with the native GeckoRuntime 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 GetGeckoRuntime() if an equivalent C# API doesn't exist.
AndroidJavaObject GetNativeWebView()
Returns the instance's underlying native GeckoSession. The application can use this to utilize native Android APIs for which 3D WebView doesn't yet have dedicated C# equivalents.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
var geckoSession = androidGeckoWebView.GetNativeWebView();
// Call the GeckoSession.purgeHistory() to purge the back / forward history.
// https://mozilla.github.io/geckoview/javadoc/mozilla-central/org/mozilla/geckoview/GeckoSession.html#purgeHistory()
// Most native GeckoSession methods must be called on the Android UI thread.
AndroidGeckoWebView.RunOnAndroidUIThread(() => {
geckoSession.Call("purgeHistory");
});
#endif
Warning: Adding code that interacts with the native GeckoSession 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 an equivalent C# API doesn't exist.
string GetSessionState()
Gets the serialized session state, which can be used to restore the session at a later time using RestoreSessionState().
#if UNITY_ANDROID && !UNITY_EDITOR
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
var serializedSessionState = androidGeckoWebView.GetSessionState();
#endif
void Pause()
Pauses media and rendering for this webview instance until Resume() is called.
#if UNITY_ANDROID && !UNITY_EDITOR
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.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.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.PauseAll();
#endif
static void RestoreAudioFocus()
Manually triggers the audio workaround described here. In some cases, 3D WebView may fail to automatically detect the audio issue, and in that scenario, the application can call this method to manually trigger the workaround.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.RestoreAudioFocus();
#endif
void RestoreSessionState()
Restores a previous session using the serialized state returned by calling GetSessionState() on this webview or a previous webview.
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.RestoreSessionState(serializedSessionState);
#endif
void Resume()
Resumes rendering for this webview instance after a previous call to Pause().
#if UNITY_ANDROID && !UNITY_EDITOR
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.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
AndroidGeckoWebView.ResumeAll();
#endif
static void RunOnAndroidUIThread(Action function)
Runs the given function on the Android UI thread.
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.RunOnAndroidUIThread(() => {
// TODO: Do something on the Android UI thread.
});
#endif
static void SetConsoleOutputEnabled(bool enabled)
Sets whether or not web console messages are output to the Android Logcat logs. The default is false
. Unlike IWebView.ConsoleMessageLogged, this method includes console messages from iframes and console message like uncaught errors that aren't explicitly passed to a log method like console.log().
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetConsoleOutputEnabled(true);
#endif
}
If enabled, Gecko performance may be negatively impacted if content makes heavy use of the console API.
static void SetDebugLoggingEnabled(bool enabled)
By default, the Gecko browser engine outputs debug messages to the Logcat logs, but you can use this method to disable that. Note that this method can only be called prior to initializing any webviews, so it's recommended to call it from Awake().
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetDebugLoggingEnabled(false);
#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
AndroidGeckoWebView.SetDrmEnabled(true);
#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
AndroidGeckoWebView.SetGeolocationEnabled(true);
#endif
Geolocation doesn't work on Meta Quest devices because they lack GPS support.
static void SetLocales(string[] locales)
Gecko automatically sets the browser engine's locale (which is used for things like the Accept-Language request header) based on the device's system language. However, you can override the locale using this method.
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetLocales(new string[] {"es-MX", "es-ES"});
#endif
}
static void SetPixelDensity(float pixelDensity)
By default, Gecko uses a pixel density of 1.0, but the application can call this method at startup to globally set a different pixel density in order to reduce pixelation and make web content appear more crisp. Note that this method can only be called prior to initializing any webviews, so it's recommended to call it from Awake().
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetPixelDensity(2.0f);
#endif
}
static void SetPreferences(Dictionary<string, string> preferences)
Sets Gecko preferences, which can be used to optionally modify the browser engine's settings. Note that this method can only be called prior to initializing any webviews, so it's recommended to call it from Awake()..
The engine's current settings can be viewed by loading the url "about:config" in a webview. The available Gecko preferences aren't well-documented, but the following pages list some of them:
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetPreferences(new Dictionary<string, string> {
["security.fileuri.strict_origin_policy"] = "false",
["formhelper.autozoom"] = "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 androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.SetSurface(surface);
#endif
static void SetXRContext(IntPtr externalXRContext)
Passes an external XR context to GeckoVRManager.setExernalContext(). WebXR isn't yet implemented in 3D WebView, but this method is provided in case others want to attempt to implement it. For more details on implementing WebXR, please see this article. Note that this method can only be called prior to initializing any webviews, so it's recommended to call it from Awake().
void Awake() {
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetXRContext(yourExternalXRContext);
#endif
}
EventHandler<AndroidGeckoClientCertificateRequestedEventArgs> ClientCertificateRequested
Indicates that the web page has requested a client certificate for authentication, and allows the application to select which client certificate to use.
If the application attaches a handler to this event, the handler must call the eventArgs.Select() callback, passing either the alias of a certificate to use or null
to continue without using a certificate. To determine the certificate alias to pass to Select(), the application can use native Android APIs, like KeyChain.choosePrivateKeyAlias().
await webViewPrefab.WaitUntilInitialized();
#if UNITY_ANDROID && !UNITY_EDITOR
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.ClientCertificateRequested += (sender, eventArgs) => {
Debug.Log("Client certificate requested for host: " + eventArgs.Host);
// TODO: This example passes `null` to Select() 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.choosePrivateKeyAlias())
// to get the alias of the certificate to use and pass that alias to Select() instead.
eventArgs.Select(null);
};
#endif
EventHandler<FileSelectionEventArgs> FileSelectionRequested
Indicates that the page requested a file selection dialog. This can happen, for example, when a file input is activated. Call the event args' Continue(filePaths) callback to provide a file selection or call Cancel() to cancel file selection.
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 androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.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 androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.ScriptConfirmRequested += (sender, eventArgs) => {
Debug.Log("Script confirm requested: " + eventArgs.Message);
eventArgs.Continue(true);
};
#endif