WindowsWebView.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #if (UNITY_STANDALONE_WIN && !UNITY_EDITOR) || UNITY_EDITOR_WIN
  17. using System;
  18. using System.Collections;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Runtime.InteropServices;
  22. using UnityEngine;
  23. namespace Vuplex.WebView {
  24. /// <summary>
  25. /// The Windows `IWebView` implementation.
  26. /// </summary>
  27. public class WindowsWebView : StandaloneWebView, IWebView {
  28. public WebPluginType PluginType {
  29. get {
  30. return WebPluginType.Windows;
  31. }
  32. }
  33. public static WindowsWebView Instantiate() {
  34. return (WindowsWebView) new GameObject().AddComponent<WindowsWebView>();
  35. }
  36. public override void Dispose() {
  37. // Cancel the render if it has been scheduled via GL.IssuePluginEvent().
  38. WebView_removePointer(_nativeWebViewPtr);
  39. base.Dispose();
  40. }
  41. public static bool ValidateGraphicsApi() {
  42. var isValid = SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D11;
  43. if (!isValid) {
  44. WebViewLogger.LogError("Unsupported graphics API: 3D WebView for Windows requires Direct3D11. Please go to Player Settings and set \"Graphics APIs for Windows\" to Direct3D11.");
  45. }
  46. return isValid;
  47. }
  48. delegate void LogFunction(string message);
  49. readonly WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
  50. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  51. static void _initializeWindowsPlugin() {
  52. // The generic `GetFunctionPointerForDelegate<T>` is unavailable in .NET 2.0.
  53. var logInfo = Marshal.GetFunctionPointerForDelegate((LogFunction)_logInfo);
  54. var logWarning = Marshal.GetFunctionPointerForDelegate((LogFunction)_logWarning);
  55. var logError = Marshal.GetFunctionPointerForDelegate((LogFunction)_logError);
  56. WebView_setLogFunctions(logInfo, logWarning, logError);
  57. WebView_logVersionInfo();
  58. }
  59. protected override StandaloneWebView _instantiate() {
  60. return Instantiate();
  61. }
  62. [AOT.MonoPInvokeCallback(typeof(LogFunction))]
  63. static void _logInfo(string message) {
  64. WebViewLogger.Log(message, false);
  65. }
  66. [AOT.MonoPInvokeCallback(typeof(LogFunction))]
  67. static void _logWarning(string message) {
  68. WebViewLogger.LogWarning(message, false);
  69. }
  70. [AOT.MonoPInvokeCallback(typeof(LogFunction))]
  71. static void _logError(string message) {
  72. WebViewLogger.LogError(message, false);
  73. }
  74. void OnEnable() {
  75. // Start the coroutine from OnEnable so that the coroutine
  76. // is restarted if the object is deactivated and then reactivated.
  77. StartCoroutine(_renderPluginOncePerFrame());
  78. }
  79. IEnumerator _renderPluginOncePerFrame() {
  80. while (true) {
  81. if (Application.isBatchMode) {
  82. // When Unity is launched in batch mode from the command line,
  83. // WaitForEndOfFrame() never returns, which can cause automated tests to fail.
  84. yield return null;
  85. } else {
  86. yield return _waitForEndOfFrame;
  87. }
  88. if (_nativeWebViewPtr != IntPtr.Zero && !IsDisposed) {
  89. int pointerId = WebView_depositPointer(_nativeWebViewPtr);
  90. GL.IssuePluginEvent(WebView_getRenderFunction(), pointerId);
  91. }
  92. }
  93. }
  94. [DllImport(_dllName)]
  95. static extern int WebView_depositPointer(IntPtr pointer);
  96. [DllImport(_dllName)]
  97. static extern IntPtr WebView_getRenderFunction();
  98. [DllImport(_dllName)]
  99. static extern void WebView_logVersionInfo();
  100. [DllImport(_dllName)]
  101. static extern void WebView_removePointer(IntPtr pointer);
  102. [DllImport(_dllName)]
  103. static extern int WebView_setLogFunctions(
  104. IntPtr logInfoFunction,
  105. IntPtr logWarningFunction,
  106. IntPtr logErrorFunction
  107. );
  108. }
  109. }
  110. #endif