WindowsWebView.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2022 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_STANDALONE_WIN && !UNITY_EDITOR) || UNITY_EDITOR_WIN
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Runtime.InteropServices;
  20. using UnityEngine;
  21. using Vuplex.WebView.Internal;
  22. namespace Vuplex.WebView {
  23. /// <summary>
  24. /// The Windows IWebView implementation.
  25. /// </summary>
  26. public class WindowsWebView : StandaloneWebView, IWebView {
  27. public WebPluginType PluginType { get; } = WebPluginType.Windows;
  28. public override void Dispose() {
  29. // Cancel the render if it has been scheduled via GL.IssuePluginEvent().
  30. WebView_removePointer(_nativeWebViewPtr);
  31. base.Dispose();
  32. }
  33. public static WindowsWebView Instantiate() => new GameObject().AddComponent<WindowsWebView>();
  34. public static bool ValidateGraphicsApi() {
  35. var isValid = SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D11;
  36. if (!isValid) {
  37. WebViewLogger.LogError("Unsupported graphics API: 3D WebView for Windows requires Direct3D11. Please go to Player Settings and set \"Graphics APIs for Windows\" to Direct3D11.");
  38. }
  39. return isValid;
  40. }
  41. readonly WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
  42. protected override StandaloneWebView _instantiate() => Instantiate();
  43. // Start the coroutine from OnEnable so that the coroutine
  44. // is restarted if the object is deactivated and then reactivated.
  45. void OnEnable() => StartCoroutine(_renderPluginOncePerFrame());
  46. IEnumerator _renderPluginOncePerFrame() {
  47. while (true) {
  48. if (Application.isBatchMode) {
  49. // When Unity is launched in batch mode from the command line,
  50. // WaitForEndOfFrame() never returns, which can cause automated tests to fail.
  51. yield return null;
  52. } else {
  53. yield return _waitForEndOfFrame;
  54. }
  55. if (_nativeWebViewPtr != IntPtr.Zero && !IsDisposed) {
  56. int pointerId = WebView_depositPointer(_nativeWebViewPtr);
  57. GL.IssuePluginEvent(WebView_getRenderFunction(), pointerId);
  58. }
  59. }
  60. }
  61. [DllImport(_dllName)]
  62. static extern int WebView_depositPointer(IntPtr pointer);
  63. [DllImport(_dllName)]
  64. static extern IntPtr WebView_getRenderFunction();
  65. [DllImport(_dllName)]
  66. static extern void WebView_removePointer(IntPtr pointer);
  67. }
  68. }
  69. #endif