3D WebView's IWebView interface is implemented by a different class on each platform:
- StandaloneWebView (Windows and macOS)
- AndroidWebView
- AndroidGeckoWebView
- iOSWebView
- VisionOSWebView
- UwpWebView (Universal Windows Platform)
- WebGLWebView
In addition to implementing IWebView, these classes also include extra platform-specific methods. As demonstrated below, an application can call a platform-specific static method by accessing it directly on the class, and it can call a platform-specific instance method by casting an IWebView instance to the specific class.
Windows and macOS
The StandaloneWebView class is available when running in the editor and building for Standalone Windows or macOS. So, an #if
directive usually isn't needed in order to reference it:
StandaloneWebView.EnableRemoteDebugging(8080);
await webViewPrefab.WaitUntilInitialized();
var standaloneWebView = webViewPrefab.WebView as StandaloneWebView;
standaloneWebView.SetNativeFileDialogEnabled(true);
However, if you build your app for other platforms (e.g. Android, iOS, WebGL, or UWP), then you'll need to use the directive #if UNITY_STANDALONE || UNITY_EDITOR
to prevent StandaloneWebView from being referenced on those other platforms:
#if UNITY_STANDALONE || UNITY_EDITOR
StandaloneWebView.EnableRemoteDebugging(8080);
#endif
#if UNITY_STANDALONE || UNITY_EDITOR
await webViewPrefab.WaitUntilInitialized();
var standaloneWebView = webViewPrefab.WebView as StandaloneWebView;
standaloneWebView.SetNativeFileDialogEnabled(true);
#endif
Android
The AndroidWebView and AndroidGeckoWebView classes are only accessible when building for an Android device, so it's necessary to use the directive #if UNITY_ANDROID && !UNITY_EDITOR
in order to reference them. Here are examples of calling those classes' static methods:
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetGeolocationEnabled(true);
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.SetDrmEnabled(true);
#endif
And here are examples of calling their instance methods:
#if UNITY_ANDROID && !UNITY_EDITOR
await webViewPrefab.WaitUntilInitialized();
var androidWebView = webViewPrefab.WebView as AndroidWebView;
androidWebView.Pause();
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
await webViewPrefab.WaitUntilInitialized();
var androidGeckoWebView = webViewPrefab.WebView as AndroidGeckoWebView;
androidGeckoWebView.SetSurface(surface);
#endif
iOS
The iOSWebView class is only accessible when building for an iOS device, so it's necessary to use the directive #if UNITY_IOS && !UNITY_EDITOR
in order to reference it. Here's an example of calling one of its static methods:
#if UNITY_IOS && !UNITY_EDITOR
iOSWebView.SetAllowsInlineMediaPlayback(false);
#endif
And here's an example of calling one of its instance methods:
#if UNITY_IOS && !UNITY_EDITOR
await webViewPrefab.WaitUntilInitialized();
var iOSWebViewInstance = webViewPrefab.WebView as iOSWebView;
iOSWebViewInstance.SetScrollViewBounces(true);
#endif
visionOS
The VisionOSWebView class is only accessible when building for a visionOS device, so it's necessary to use the directive #if UNITY_VISIONOS && !UNITY_EDITOR
in order to reference it. Here's an example of calling one of its static methods:
#if UNITY_VISIONOS && !UNITY_EDITOR
VisionOSWebView.SetCameraEnabled(true);
#endif
And here's an example of calling one of its instance methods:
await webViewPrefab.WaitUntilInitialized();
#if UNITY_VISIONOS && !UNITY_EDITOR
var visionOSWebView = webViewPrefab.WebView as VisionOSWebView;
visionOSWebView.SetTargetFrameRate(15);
#endif
UWP
The UwpWebView class is only accessible when building for Universal Windows Platform, so it's necessary to use the directive #if UNITY_WSA && !UNITY_EDITOR
in order to reference it. Here's an example of calling one of its static methods:
#if UNITY_WSA && !UNITY_EDITOR
UwpWebView.SetGeolocationEnabled(true);
#endif
WebGL
The WebGLWebView class is only accessible when building for an WebGL, so it's necessary to use the directive #if UNITY_WEBGL && !UNITY_EDITOR
in order to reference it. Here's an example of calling one of its static methods:
#if UNITY_WEBGL && !UNITY_EDITOR
// Gets the Unity app's web page title.
var title = WebGLWebView.ExecuteJavaScriptLocally("document.title");
Debug.Log("Title: " + title);
#endif
And here's an example of calling one of its instance methods:
#if UNITY_WEBGL && !UNITY_EDITOR
await canvasWebViewPrefab.WaitUntilInitialized();
var webGLWebView = canvasWebViewPrefab.WebView as WebGLWebView;
if (webGLWebView.CanAccessIFrameContent()) {
Debug.Log("The iframe content can be accessed 👍");
}
#endif