3D WebView's IWebView
interface is implemented by a different class on each platform:
AndroidWebView
AndroidGeckoWebView
iOSWebView
StandaloneWebView
(Windows and macOS)UwpWebView
(Universal Windows Platform)
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.
Android
The AndroidWebView
and AndroidGeckoWebView
classes are only accessible when building for an Android device. Here are examples of calling those classes' static methods:
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidWebView.SetAudioAndVideoCaptureEnabled(true);
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidGeckoWebView.EnableRemoteDebugging();
#endif
And here are examples of calling their instance methods:
#if UNITY_ANDROID && !UNITY_EDITOR
(webViewPrefab.WebView as AndroidWebView).Pause();
#endif
#if UNITY_ANDROID && !UNITY_EDITOR
(webViewPrefab.WebView as AndroidGeckoWebView).SetSurface(surface);
#endif
iOS
The iOSWebView
class is only accessible when building for an iOS device. Here's an example of calling one of its instance methods:
#if UNITY_IOS && !UNITY_EDITOR
(webViewPrefab.WebView as iOSWebView).SetUserAgent("Custom Browser/1.0");
#endif
Windows and macOS
The StandaloneWebView
class is available when running in the editor or building for Standalone. So, #define directives generally aren't needed in order to access its methods:
StandaloneWebView.EnableRemoteDebugging(8080);
(webViewPrefab.WebView as StandaloneWebView).SetZoomLevel(2.0f);
However, if you build your app for other platforms (like Android and iOS), your application will need to use #define directives to prevent StandaloneWebView
from being referenced on those platforms:
#if UNITY_STANDALONE || UNITY_EDITOR
StandaloneWebView.EnableRemoteDebugging(8080);
#endif
#if UNITY_STANDALONE || UNITY_EDITOR
(webViewPrefab.WebView as StandaloneWebView).SetZoomLevel(2.0f);
#endif