WebPluginFactory.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. using System;
  15. using System.Linq;
  16. using UnityEngine;
  17. using Vuplex.WebView.Internal;
  18. namespace Vuplex.WebView {
  19. public class WebPluginFactory {
  20. public virtual IWebPlugin GetPlugin() => GetPlugin(null);
  21. public virtual IWebPlugin GetPlugin(WebPluginType[] preferredPlugins) {
  22. #if UNITY_SERVER
  23. _logMockWarningOnce("3D WebView for Windows doesn't support the \"Server Build\" option because it uses a null graphics device (GraphicsDeviceType.Null)");
  24. return _mockPlugin;
  25. #elif UNITY_EDITOR
  26. #if UNITY_EDITOR_WIN
  27. if (_windowsPlugin != null) {
  28. return _windowsPlugin;
  29. }
  30. _logMockWarningOnce("The 3D WebView for Windows plugin is not currently installed");
  31. return _mockPlugin;
  32. #elif UNITY_EDITOR_OSX
  33. if (_macPlugin != null) {
  34. return _macPlugin;
  35. }
  36. _logMockWarningOnce("The 3D WebView for macOS plugin is not currently installed");
  37. return _mockPlugin;
  38. #else
  39. _logMockWarningOnce("There is not currently a 3D WebView plugin for the current editor platform");
  40. return _mockPlugin;
  41. #endif
  42. #elif UNITY_STANDALONE_WIN
  43. if (_windowsPlugin != null) {
  44. return _windowsPlugin;
  45. }
  46. throw new WebViewUnavailableException("The 3D WebView for Windows plugin is not currently installed." + MORE_INFO_TEXT);
  47. #elif UNITY_STANDALONE_OSX
  48. if (_macPlugin != null) {
  49. return _macPlugin;
  50. }
  51. throw new WebViewUnavailableException("The 3D WebView for macOS plugin is not currently installed." + MORE_INFO_TEXT);
  52. #elif UNITY_IOS
  53. if (_iosPlugin != null) {
  54. return _iosPlugin;
  55. }
  56. throw new WebViewUnavailableException("The 3D WebView for iOS plugin is not currently installed." + MORE_INFO_TEXT);
  57. #elif UNITY_ANDROID
  58. var preferChromiumAndroidPlugin = preferredPlugins != null && preferredPlugins.Contains(WebPluginType.Android);
  59. if (_androidPlugin != null && (_androidGeckoPlugin == null || preferChromiumAndroidPlugin)) {
  60. return _androidPlugin;
  61. }
  62. if (_androidGeckoPlugin != null) {
  63. return _androidGeckoPlugin;
  64. }
  65. throw new WebViewUnavailableException("The 3D WebView for Android plugin is not currently installed." + MORE_INFO_TEXT);
  66. #elif UNITY_WSA
  67. if (_uwpPlugin != null) {
  68. return _uwpPlugin;
  69. }
  70. throw new WebViewUnavailableException("The 3D WebView for UWP plugin is not currently installed." + MORE_INFO_TEXT);
  71. #elif UNITY_WEBGL
  72. if (_webGLPlugin != null) {
  73. return _webGLPlugin;
  74. }
  75. throw new WebViewUnavailableException("The 2D WebView for WebGL plugin is not currently installed." + MORE_INFO_TEXT);
  76. #else
  77. throw new WebViewUnavailableException("This version of 3D WebView does not support the current build platform." + MORE_INFO_TEXT);
  78. #endif
  79. }
  80. public static void RegisterAndroidPlugin(IWebPlugin plugin) {
  81. _androidPlugin = plugin;
  82. }
  83. public static void RegisterAndroidGeckoPlugin(IWebPlugin plugin) {
  84. _androidGeckoPlugin = plugin;
  85. }
  86. public static void RegisterIOSPlugin(IWebPlugin plugin) {
  87. _iosPlugin = plugin;
  88. }
  89. public static void RegisterMacPlugin(IWebPlugin plugin) {
  90. _macPlugin = plugin;
  91. }
  92. public static void RegisterMockPlugin(IWebPlugin plugin) {
  93. _mockPlugin = plugin;
  94. }
  95. public static void RegisterUwpPlugin(IWebPlugin plugin) {
  96. _uwpPlugin = plugin;
  97. }
  98. public static void RegisterWebGLPlugin(IWebPlugin plugin) {
  99. _webGLPlugin = plugin;
  100. }
  101. public static void RegisterWindowsPlugin(IWebPlugin plugin) {
  102. _windowsPlugin = plugin;
  103. }
  104. protected static IWebPlugin _androidPlugin;
  105. protected static IWebPlugin _androidGeckoPlugin;
  106. protected static IWebPlugin _iosPlugin;
  107. protected static IWebPlugin _macPlugin;
  108. protected static IWebPlugin _mockPlugin = MockWebPlugin.Instance;
  109. bool _mockWarningLogged;
  110. const string MORE_INFO_TEXT = " For more info, please visit https://developer.vuplex.com.";
  111. protected static IWebPlugin _uwpPlugin;
  112. protected static IWebPlugin _webGLPlugin;
  113. protected static IWebPlugin _windowsPlugin;
  114. /// <summary>
  115. /// Logs the warning once so that it doesn't spam the console.
  116. /// </summary>
  117. void _logMockWarningOnce(string reason) {
  118. if (!_mockWarningLogged) {
  119. _mockWarningLogged = true;
  120. WebViewLogger.LogWarning($"{reason}, so the mock webview will be used{(Application.isEditor ? " while running in the editor" : "")}. For more info, please see <em>https://support.vuplex.com/articles/mock-webview</em>.");
  121. }
  122. }
  123. }
  124. }