WebGLWebView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_WEBGL && !UNITY_EDITOR
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Runtime.InteropServices;
  18. using System.Threading.Tasks;
  19. using UnityEngine;
  20. using Vuplex.WebView.Internal;
  21. namespace Vuplex.WebView {
  22. /// <summary>
  23. /// WebGLWebView is the IWebView implementation used by 2D WebView for WebGL.
  24. /// It also includes additional APIs for WebGL-specific functionality.
  25. /// </summary>
  26. public class WebGLWebView : BaseWebView,
  27. IWebView,
  28. IWithNative2DMode {
  29. /// <summary>
  30. /// Gets the unique `id` attribute of the webview's &lt;iframe&gt; element.
  31. /// </summary>
  32. /// <example>
  33. /// <code>
  34. /// await canvasWebViewPrefab.WaitUntilInitialized();
  35. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  36. /// var webGLWebView = canvasWebViewPrefab.WebView as WebGLWebView;
  37. /// Debug.Log("IFrame ID: " + webGLWebView.IFrameElementID);
  38. /// #endif
  39. /// </code>
  40. /// </example>
  41. public string IFrameElementID { get; private set; }
  42. /// <see cref="IWithNative2DMode"/>
  43. public bool Native2DModeEnabled { get => _native2DModeEnabled; }
  44. public WebPluginType PluginType { get; } = WebPluginType.WebGL;
  45. /// <see cref="IWithNative2DMode"/>
  46. public Rect Rect { get => _rect; }
  47. /// <see cref="IWithNative2DMode"/>
  48. public bool Visible { get => _visible; }
  49. /// <seealso cref="IWithNative2DMode"/>
  50. public void BringToFront() {
  51. _assertValidState();
  52. _assertNative2DModeEnabled();
  53. WebView_bringToFront(_nativeWebViewPtr);
  54. }
  55. /// <summary>
  56. /// Indicates whether 2D WebView can access the content in the
  57. /// webview's iframe. If the iframe's content can't be accessed,
  58. /// then most of the IWebView APIs become disabled. For more
  59. /// information, please see [this article](https://support.vuplex.com/articles/webgl-limitations#cross-origin-limitation).
  60. /// </summary>
  61. /// <example>
  62. /// <code>
  63. /// await canvasWebViewPrefab.WaitUntilInitialized();
  64. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  65. /// var webGLWebView = canvasWebViewPrefab.WebView as WebGLWebView;
  66. /// if (webGLWebView.CanAccessIFrameContent()) {
  67. /// Debug.Log("The iframe content can be accessed 👍");
  68. /// }
  69. /// #endif
  70. /// </code>
  71. /// </example>
  72. public bool CanAccessIFrameContent() {
  73. _assertValidState();
  74. return WebView_canAccessIFrameContent(_nativeWebViewPtr);
  75. }
  76. public override Task<bool> CanGoBack() => _canGoBackOrForward("CanGoBack");
  77. public override Task<bool> CanGoForward() => _canGoBackOrForward("CanGoForward");
  78. public override Task<byte[]> CaptureScreenshot() {
  79. WebGLWarnings.LogCaptureScreenshotError();
  80. return Task.FromResult(new byte[0]);
  81. }
  82. public override void Click(int xInPixels, int yInPixels, bool preventStealingFocus = false) {
  83. if (_verifyIFrameCanBeAccessed("Click")) {
  84. base.Click(xInPixels, yInPixels, preventStealingFocus);
  85. }
  86. }
  87. public override void Copy() => WebGLWarnings.LogCopyError();
  88. public override void Cut() => WebGLWarnings.LogCutError();
  89. public override void ExecuteJavaScript(string javaScript, Action<string> callback) {
  90. _assertValidState();
  91. if (_verifyIFrameCanBeAccessed("ExecuteJavaScript")) {
  92. var result = WebView_executeJavaScriptSync(_nativeWebViewPtr, javaScript);
  93. callback?.Invoke(result);
  94. }
  95. }
  96. /// <summary>
  97. /// Executes the given JavaScript locally in the Unity app's window and returns the result.
  98. /// </summary>
  99. /// <example>
  100. /// <code>
  101. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  102. /// // Gets the Unity app's web page title.
  103. /// var title = WebGLWebView.ExecuteJavaScriptLocally("document.title");
  104. /// Debug.Log("Title: " + title);
  105. /// #endif
  106. /// </code>
  107. /// </example>
  108. public static string ExecuteJavaScriptLocally(string javaScript) => WebView_executeJavaScriptLocally(javaScript);
  109. /// <summary>
  110. /// Focuses the Unity game view, which enables Unity to detect keyboard input. When a webview
  111. /// is focused, Unity is unable to detect keyboard input (i.e. its APIs like Input.GetKey() don't work).
  112. /// To re-enable Unity's ability to detect keyboard input, the application can use this method to programmatically
  113. /// shift focus from the webview to the Unity game view.
  114. /// For more details, see <see href="https://support.vuplex.com/articles/webgl-limitations#input-focus">this article</see>.
  115. /// </summary>
  116. /// <example>
  117. /// <code>
  118. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  119. /// WebGLWebView.FocusUnity();
  120. /// #endif
  121. /// </code>
  122. /// </example>
  123. public static void FocusUnity() => WebView_focusUnity();
  124. public override Task<byte[]> GetRawTextureData() {
  125. WebGLWarnings.LogGetRawTextureDataError();
  126. return Task.FromResult(new byte[0]);
  127. }
  128. public override void GoBack() {
  129. if (_verifyIFrameCanBeAccessed("GoBack")) {
  130. base.GoBack();
  131. }
  132. }
  133. public override void GoForward() {
  134. if (_verifyIFrameCanBeAccessed("GoForward")) {
  135. base.GoForward();
  136. }
  137. }
  138. public Task Init(int width, int height) {
  139. var message = "2D WebView for WebGL only supports 2D, so Native 2D Mode must be enabled." + WebGLWarnings.GetArticleLinkText(false);
  140. WebViewLogger.LogError(message);
  141. throw new NotImplementedException(message);
  142. }
  143. /// <see cref="IWithNative2DMode"/>
  144. public async Task InitInNative2DMode(Rect rect) {
  145. await _initInNative2DModeBase(rect);
  146. // Set IFrameElementID *after* base.Init() because it sets gameObject.name.
  147. IFrameElementID = gameObject.name;
  148. // Prior to Unity 2019.3, Unity's UI used a pixel density of
  149. // 1 instead of using window.devicePixelRatio.
  150. #if UNITY_2019_3_OR_NEWER
  151. var ignoreDevicePixelRatio = false;
  152. #else
  153. var ignoreDevicePixelRatio = true;
  154. #endif
  155. _nativeWebViewPtr = WebView_newInNative2DMode(
  156. gameObject.name,
  157. (int)rect.x,
  158. (int)rect.y,
  159. (int)rect.width,
  160. (int)rect.height,
  161. ignoreDevicePixelRatio
  162. );
  163. if (_nativeWebViewPtr == IntPtr.Zero) {
  164. throw new TrialExpiredException("Your trial of 2D WebView for WebGL has expired. Please purchase a license to continue using it.");
  165. }
  166. }
  167. public static WebGLWebView Instantiate() => new GameObject().AddComponent<WebGLWebView>();
  168. public override void LoadHtml(string html) {
  169. WebGLWarnings.LogLoadHtmlWarning();
  170. base.LoadHtml(html);
  171. }
  172. public override void LoadUrl(string url, Dictionary<string, string> additionalHttpHeaders) {
  173. WebViewLogger.LogWarning("LoadUrl(url, headers) was called, but 2D WebView for WebGL is unable to send additional headers when loading a URL due to browser limitations. So, the URL will be loaded without additional headers using LoadUrl(url) instead.");
  174. LoadUrl(url);
  175. }
  176. public override void Paste() => WebGLWarnings.LogPasteError();
  177. public override void PostMessage(string message) {
  178. _assertValidState();
  179. WebView_postMessage(_nativeWebViewPtr, message);
  180. }
  181. public override void Scroll(int scrollDeltaXInPixels, int scrollDeltaYInPixels) {
  182. if (_verifyIFrameCanBeAccessed("Scroll")) {
  183. base.Scroll(scrollDeltaXInPixels, scrollDeltaYInPixels);
  184. }
  185. }
  186. public override void Scroll(Vector2 normalizedScrollDelta, Vector2 normalizedPoint) {
  187. if (_verifyIFrameCanBeAccessed("Scroll")) {
  188. base.Scroll(normalizedScrollDelta, normalizedPoint);
  189. }
  190. }
  191. public override void SendKey(string key) {
  192. if (_verifyIFrameCanBeAccessed("SendKey")) {
  193. base.SendKey(key);
  194. }
  195. }
  196. /// <summary>
  197. /// Overrides the value of `devicePixelRatio` that 2D WebView uses to determine the correct
  198. /// size and coordinates of webviews.
  199. /// </summary>
  200. /// <summary>
  201. /// Starting in Unity 2019.3, Unity scales the UI by default based on the browser's window.devicePixelRatio
  202. /// value. However, it's possible for an application to override the `devicePixelRatio` value
  203. /// by passing a value for `config.devicePixelRatio` to Unity's `createUnityInstance()` JavaScript function. In some
  204. /// versions of Unity, the default index.html template sets `config.devicePixelRatio = 1` on mobile.
  205. /// In order for 2D WebView to size and position webviews correctly, it must determine the value of `devicePixelRatio`
  206. /// to use. Since there's no API to get a reference to the Unity instance that the application created with `createUnityInstance()`,
  207. /// 2D WebView tries to detect if `config.devicePixelRatio` was passed to `createUnityInstance()` by checking for the presence of a
  208. /// global `config` JavaScript variable. If there is a global variable named `config` with a `devicePixelRatio` property, then 2D WebView
  209. /// uses that value, otherwise it defaults to using `window.devicePixelRatio`. This approach works for Unity's default
  210. /// index.html templates, but it may not work if the application uses a custom HTML template or changes the name of the `config`
  211. /// variable in the default template. In those cases, the application can use this method to pass the overridden value of `devicePixelRatio` to
  212. /// 2D WebView.
  213. /// </summary>
  214. /// <example>
  215. /// <code>
  216. /// void Awake() {
  217. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  218. /// WebGLWebView.SetDevicePixelRatio(1);
  219. /// #endif
  220. /// }
  221. /// </code>
  222. /// </example>
  223. public static void SetDevicePixelRatio(float devicePixelRatio) {
  224. #if UNITY_2019_3_OR_NEWER
  225. WebView_setDevicePixelRatio(devicePixelRatio);
  226. #else
  227. WebViewLogger.LogWarning("The call to WebGLWebView.SetDevicePixelRatio() will be ignored because a version of Unity older than 2019.3 is being used, which always uses a devicePixelRatio of 1.");
  228. #endif
  229. }
  230. /// <summary>
  231. /// Sets whether the JavaScript Fullscreen API is enabled for webviews. The default is `false`.
  232. /// </summary>
  233. /// <example>
  234. /// <code>
  235. /// void Awake() {
  236. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  237. /// WebGLWebView.SetFullscreenEnabled(true);
  238. /// #endif
  239. /// }
  240. /// </code>
  241. /// </example>
  242. public static void SetFullscreenEnabled(bool enabled) => WebView_setFullscreenEnabled(enabled);
  243. /// <summary>
  244. /// Sets whether the JavaScript Geolocation API is enabled for webviews. The default is `false`.
  245. /// </summary>
  246. /// <example>
  247. /// <code>
  248. /// void Awake() {
  249. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  250. /// WebGLWebView.SetGeolocationEnabled(true);
  251. /// #endif
  252. /// }
  253. /// </code>
  254. /// </example>
  255. public static void SetGeolocationEnabled(bool enabled) => WebView_setGeolocationEnabled(enabled);
  256. public void SetNativeZoomEnabled(bool enabled) {
  257. WebViewLogger.LogWarning("2D WebView for WebGL doesn't support native zooming, so the call to IWithNative2DMode.SetNativeZoomEnabled() will be ignored.");
  258. }
  259. /// <see cref="IWithNative2DMode"/>
  260. public void SetRect(Rect rect) {
  261. _assertValidState();
  262. _assertNative2DModeEnabled();
  263. _rect = rect;
  264. WebView_setRect(_nativeWebViewPtr, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);
  265. }
  266. /// <summary>
  267. /// Explicitly sets the HTML element that 2D WebView should use as the Unity app container.
  268. /// 2D WebView automatically detects the Unity app container element if its ID is set to one of the default values of
  269. /// "unityContainer" or "unity-container". However, if your app uses a custom WebGL template that
  270. /// uses a different ID for the container element, you must call this method to set the container element ID.
  271. /// 2D WebView for WebGL works by adding &lt;iframe&gt; elements to the app container, so it's unable to function correctly
  272. /// if it's unable to find the Unity app container element.
  273. /// </summary>
  274. /// <example>
  275. /// <code>
  276. /// void Awake() {
  277. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  278. /// WebGLWebView.SetUnityContainerElementId("your-custom-id");
  279. /// #endif
  280. /// }
  281. /// </code>
  282. /// </example>
  283. public static void SetUnityContainerElementId(string containerId) => WebView_setUnityContainerElementId(containerId);
  284. /// <summary>
  285. /// Sets whether screen sharing is enabled for webviews. The default is `false`.
  286. /// </summary>
  287. /// <example>
  288. /// <code>
  289. /// void Awake() {
  290. /// #if UNITY_WEBGL &amp;&amp; !UNITY_EDITOR
  291. /// WebGLWebView.SetScreenSharingEnabled(true);
  292. /// #endif
  293. /// }
  294. /// </code>
  295. /// </example>
  296. public static void SetScreenSharingEnabled(bool enabled) => WebView_setScreenSharingEnabled(enabled);
  297. /// <see cref="IWithNative2DMode"/>
  298. public void SetVisible(bool visible) {
  299. _assertValidState();
  300. _assertNative2DModeEnabled();
  301. _visible = visible;
  302. WebView_setVisible(_nativeWebViewPtr, visible);
  303. }
  304. public override void ZoomIn() => WebGLWarnings.LogZoomInError();
  305. public override void ZoomOut() => WebGLWarnings.LogZoomOutError();
  306. #region Non-public members
  307. readonly WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
  308. Task<bool> _canGoBackOrForward(string methodName) {
  309. _assertValidState();
  310. if (_verifyIFrameCanBeAccessed(methodName)) {
  311. WebGLWarnings.LogCanGoBackOrForwardWarning();
  312. return Task.FromResult(WebView_canGoBackOrForward(_nativeWebViewPtr));
  313. }
  314. return Task.FromResult(false);
  315. }
  316. bool _verifyIFrameCanBeAccessed(string methodName) {
  317. if (CanAccessIFrameContent()) {
  318. return true;
  319. }
  320. // Log an error instead of throwing an exception because
  321. // exceptions are disabled by default for WebGL.
  322. WebGLWarnings.LogIFrameContentAccessWarning(methodName);
  323. return false;
  324. }
  325. [DllImport(_dllName)]
  326. static extern void WebView_bringToFront(IntPtr webViewPtr);
  327. [DllImport(_dllName)]
  328. static extern bool WebView_canAccessIFrameContent(IntPtr webViewPtr);
  329. [DllImport(_dllName)]
  330. static extern bool WebView_canGoBackOrForward(IntPtr webViewPtr);
  331. [DllImport(_dllName)]
  332. static extern string WebView_executeJavaScriptSync(IntPtr webViewPtr, string javaScript);
  333. [DllImport(_dllName)]
  334. static extern string WebView_executeJavaScriptLocally(string javaScript);
  335. [DllImport(_dllName)]
  336. static extern string WebView_focusUnity();
  337. [DllImport(_dllName)]
  338. static extern IntPtr WebView_newInNative2DMode(string gameObjectName, int x, int y, int width, int height, bool ignoreDevicePixelRatio);
  339. [DllImport (_dllName)]
  340. static extern void WebView_postMessage(IntPtr webViewPtr, string message);
  341. [DllImport (_dllName)]
  342. static extern void WebView_setDevicePixelRatio(float devicePixelRatio);
  343. [DllImport (_dllName)]
  344. static extern void WebView_setFullscreenEnabled(bool enabled);
  345. [DllImport (_dllName)]
  346. static extern void WebView_setGeolocationEnabled(bool enabled);
  347. [DllImport (_dllName)]
  348. static extern void WebView_setRect(IntPtr webViewPtr, int x, int y, int width, int height);
  349. [DllImport (_dllName)]
  350. static extern void WebView_setScreenSharingEnabled(bool enabled);
  351. [DllImport (_dllName)]
  352. static extern void WebView_setUnityContainerElementId(string containerId);
  353. [DllImport (_dllName)]
  354. static extern void WebView_setVisible(IntPtr webViewPtr, bool visible);
  355. #endregion
  356. }
  357. }
  358. #else
  359. namespace Vuplex.WebView {
  360. [System.Obsolete("To use the WebGLWebView class, you must use the directive `#if UNITY_WEBGL && !UNITY_EDITOR` like described here: https://support.vuplex.com/articles/how-to-call-platform-specific-apis#webgl . Note: WebGLWebView isn't actually obsolete. This compiler error just reports it's obsolete because 3D WebView generated the error with System.ObsoleteAttribute.", true)]
  361. public class WebGLWebView {}
  362. }
  363. #endif