Web.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using UnityEngine;
  18. #if NET_4_6 || NET_STANDARD_2_0
  19. using System.Threading.Tasks;
  20. #endif
  21. namespace Vuplex.WebView {
  22. /// <summary>
  23. /// `Web` is the top-level static class for the 3D WebView plugin.
  24. /// It contains static methods for configuring the module and creating resources.
  25. /// </summary>
  26. public static class Web {
  27. /// <summary>
  28. /// Indicates the default 3D WebView plugin among those
  29. /// installed for the current platform.
  30. /// </summary>
  31. public static WebPluginType DefaultPluginType {
  32. get {
  33. return _pluginFactory.GetPlugin().Type;
  34. }
  35. }
  36. /// <summary>
  37. /// Clears all data that persists between webview instances,
  38. /// including cookies, storage, and cached resources.
  39. /// </summary>
  40. /// <remarks>
  41. /// On Windows and macOS, this method can only be called prior to
  42. /// initializing any webviews.
  43. /// </remarks>
  44. public static void ClearAllData() {
  45. _pluginFactory.GetPlugin().ClearAllData();
  46. }
  47. #if NET_4_6 || NET_STANDARD_2_0
  48. /// <summary>
  49. /// Creates a material and texture that a webview can use for rendering.
  50. /// </summary>
  51. /// <remarks>
  52. /// Note that `WebViewPrefab` and `CanvasWebViewPrefab` take care of material creation for you, so you only need
  53. /// to call this method directly if you need to create an `IWebView` instance outside of a prefab with
  54. /// `Web.CreateWebView()`.
  55. /// </remarks>
  56. public static Task<Material> CreateMaterial() {
  57. var task = new TaskCompletionSource<Material>();
  58. CreateMaterial(task.SetResult);
  59. return task.Task;
  60. }
  61. #endif
  62. /// <summary>
  63. /// Like the other version of `CreateMaterial()`, except it uses a callback
  64. /// instead of a `Task` in order to be compatible with legacy .NET.
  65. /// </summary>
  66. public static void CreateMaterial(Action<Material> callback) {
  67. _pluginFactory.GetPlugin().CreateMaterial(callback);
  68. }
  69. /// <summary>
  70. /// Like `CreateMaterial`, except it creates a material that a webview
  71. /// can use for rendering video. If the platform doesn't need a separate
  72. /// material and texture for video, this method returns `null`.
  73. /// </summary>
  74. /// <remarks>
  75. /// Currently, iOS is the only platform that always uses a separate texture
  76. /// for video. Android only uses a separate video texture on versions of Android
  77. /// older than 6.0. For other platforms, video content is always integrated into
  78. /// the main texture.
  79. /// </remarks>
  80. public static void CreateVideoMaterial(Action<Material> callback) {
  81. _pluginFactory.GetPlugin().CreateVideoMaterial(callback);
  82. }
  83. #if NET_4_6 || NET_STANDARD_2_0
  84. /// <summary>
  85. /// Creates a special texture that a webview can use for rendering, using the given
  86. /// width and height in Unity units (not pixels). The webview plugin automatically
  87. /// resizes a texture when it initializes or resizes a webview, so in practice, you
  88. /// can simply use the dimensions of 1x1 like `CreateTexture(1, 1)`.
  89. /// </summary>
  90. /// <remarks>
  91. /// Note that the `WebViewPrefab` takes care of texture creation for you, so you only need
  92. /// to call this method directly if you need to create an `IWebView` instance outside of a prefab with
  93. /// `Web.CreateWebView()`.
  94. /// </remarks>
  95. public static Task<Texture2D> CreateTexture(float width, float height) {
  96. var task = new TaskCompletionSource<Texture2D>();
  97. CreateTexture(width, height, task.SetResult);
  98. return task.Task;
  99. }
  100. #endif
  101. /// <summary>
  102. /// Like the other version of `CreateTexture()`, except it uses a callback
  103. /// instead of a `Task` in order to be compatible with legacy .NET.
  104. /// </summary>
  105. public static void CreateTexture(float width, float height, Action<Texture2D> callback) {
  106. _pluginFactory.GetPlugin().CreateTexture(width, height, callback);
  107. }
  108. /// <summary>
  109. /// Creates a new webview in a platform-agnostic way. After a webview
  110. /// is created, it must be initialized by calling one of its `Init()`
  111. /// methods.
  112. /// </summary>
  113. /// <remarks>
  114. /// Note that `WebViewPrefab` takes care of creating and managing
  115. /// an `IWebView` instance for you, so you only need to call this method directly
  116. /// if you need to create an `IWebView` instance outside of a prefab
  117. /// (for example, to connect it to your own custom GameObject).
  118. /// </remarks>
  119. /// <example>
  120. /// var material = await Web.CreateMaterial();
  121. /// // Set the material attached to this GameObject so that it can display the web content.
  122. /// GetComponent&lt;Renderer>().material = material;
  123. /// var webView = Web.CreateWebView();
  124. /// webView.Init(material.mainTexture, 1, 1);
  125. /// webView.LoadUrl("https://vuplex.com");
  126. /// </example>
  127. public static IWebView CreateWebView() {
  128. return _pluginFactory.GetPlugin().CreateWebView();
  129. }
  130. /// <summary>
  131. /// Like `CreateWebView()`, except an array of preferred plugin types can be
  132. /// provided to override which 3D WebView plugin is used in the case where
  133. /// multiple plugins are installed for the same build platform.
  134. /// </summary>
  135. /// <remarks>
  136. /// Currently, Android is the only platform that supports multiple 3D WebView
  137. /// plugins: `WebPluginType.Android` and `WebPluginType.AndroidGecko`. If both
  138. /// plugins are installed in the same project, `WebPluginType.AndroidGecko` will be used by default.
  139. /// However, you can override this to force `WebPluginType.Android` to be used instead by passing
  140. /// `new WebPluginType[] { WebPluginType.Android }`.
  141. /// </remarks>
  142. public static IWebView CreateWebView(WebPluginType[] preferredPlugins) {
  143. return _pluginFactory.GetPlugin(preferredPlugins).CreateWebView();
  144. }
  145. /// <summary>
  146. /// Enables remote debugging. For more info, please
  147. /// see [this article on remote debugging](https://support.vuplex.com/articles/how-to-debug-web-content).
  148. /// </summary>
  149. public static void EnableRemoteDebugging() {
  150. _pluginFactory.GetPlugin().EnableRemoteDebugging();
  151. }
  152. /// <summary>
  153. /// By default, browsers block https URLs with invalid SSL certificates
  154. /// from being loaded. However, this method can be used to ignore
  155. /// certificate errors.
  156. /// </summary>
  157. /// <remarks>
  158. /// This method works for every package except for 3D WebView for UWP.
  159. /// For UWP, certificates must be [whitelisted in the Package.appxmanifest file](https://www.suchan.cz/2015/10/displaying-https-page-with-invalid-certificate-in-uwp-webview/).
  160. /// </remarks>
  161. public static void SetIgnoreCertificateErrors(bool ignore) {
  162. _pluginFactory.GetPlugin().SetIgnoreCertificateErrors(ignore);
  163. }
  164. /// <summary>
  165. /// Enables support for showing the native Android or iOS touch screen
  166. /// keyboard when an input field is focused.
  167. /// </summary>
  168. /// <remarks>
  169. /// This method should be called prior to initializing the desired webviews.
  170. /// This functionality is currently only supported by 3D WebView for Android
  171. /// and 3D WebView for iOS. For other packages, such as
  172. /// 3D WebView for Android with Gecko Engine, please see
  173. /// Unity's [`TouchScreenKeyboard`](https://docs.unity3d.com/ScriptReference/TouchScreenKeyboard.html) class.
  174. /// </remarks>
  175. public static void SetTouchScreenKeyboardEnabled(bool enabled) {
  176. var pluginWithTouchScreenKeyboard = _pluginFactory.GetPlugin() as IPluginWithTouchScreenKeyboard;
  177. if (pluginWithTouchScreenKeyboard != null) {
  178. pluginWithTouchScreenKeyboard.SetTouchScreenKeyboardEnabled(enabled);
  179. }
  180. }
  181. /// <summary>
  182. /// Controls whether data like cookies, localStorage, and cached resources
  183. /// is persisted between webview instances. The default is `true`, but this
  184. /// can be set to `false` to achieve an "incognito mode".
  185. /// </summary>
  186. /// <remarks>
  187. /// On Windows and macOS, this method can only be called prior to
  188. /// initializing any webviews.
  189. /// </remarks>
  190. public static void SetStorageEnabled(bool enabled) {
  191. _pluginFactory.GetPlugin().SetStorageEnabled(enabled);
  192. }
  193. /// <summary>
  194. /// By default, webviews use a User-Agent that looks that of a desktop
  195. /// computer so that servers return the desktop versions of websites.
  196. /// If you instead want the mobile versions of websites, you can invoke
  197. /// this method with `true` to use the User-Agent for a mobile device.
  198. /// </summary>
  199. /// <remarks>
  200. /// On Windows and macOS, this method can only be called prior to
  201. /// initializing any webviews.
  202. /// </remarks>
  203. public static void SetUserAgent(bool mobile) {
  204. _pluginFactory.GetPlugin().SetUserAgent(mobile);
  205. }
  206. /// <summary>
  207. /// Configures the module to use a custom User-Agent string.
  208. /// </summary>
  209. /// <remarks>
  210. /// On Windows and macOS, this method can only be called prior to
  211. /// initializing any webviews.
  212. /// </remarks>
  213. public static void SetUserAgent(string userAgent) {
  214. _pluginFactory.GetPlugin().SetUserAgent(userAgent);
  215. }
  216. static internal void SetPluginFactory(WebPluginFactory pluginFactory) {
  217. _pluginFactory = pluginFactory;
  218. }
  219. static WebPluginFactory _pluginFactory = new WebPluginFactory();
  220. }
  221. }