WebPluginFactory.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 System.Linq;
  18. using UnityEngine;
  19. namespace Vuplex.WebView {
  20. class WebPluginFactory {
  21. public virtual IWebPlugin GetPlugin() {
  22. return GetPlugin(null);
  23. }
  24. public virtual IWebPlugin GetPlugin(WebPluginType[] preferredPlugins) {
  25. #if 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. #else
  72. throw new WebViewUnavailableException("This version of 3D WebView does not support the current build platform." + MORE_INFO_TEXT);
  73. #endif
  74. }
  75. public static void RegisterAndroidPlugin(IWebPlugin plugin) {
  76. _androidPlugin = plugin;
  77. }
  78. public static void RegisterAndroidGeckoPlugin(IWebPlugin plugin) {
  79. _androidGeckoPlugin = plugin;
  80. }
  81. public static void RegisterIOSPlugin(IWebPlugin plugin) {
  82. _iosPlugin = plugin;
  83. }
  84. public static void RegisterMacPlugin(IWebPlugin plugin) {
  85. _macPlugin = plugin;
  86. }
  87. public static void RegisterMockPlugin(IWebPlugin plugin) {
  88. _mockPlugin = plugin;
  89. }
  90. public static void RegisterUwpPlugin(IWebPlugin plugin) {
  91. _uwpPlugin = plugin;
  92. }
  93. public static void RegisterWindowsPlugin(IWebPlugin plugin) {
  94. _windowsPlugin = plugin;
  95. }
  96. protected static IWebPlugin _androidPlugin;
  97. protected static IWebPlugin _androidGeckoPlugin;
  98. protected static IWebPlugin _iosPlugin;
  99. protected static IWebPlugin _macPlugin;
  100. protected static IWebPlugin _mockPlugin = MockWebPlugin.Instance;
  101. bool _mockWarningLogged;
  102. const string MORE_INFO_TEXT = " For more info, please visit https://developer.vuplex.com.";
  103. protected static IWebPlugin _uwpPlugin;
  104. protected static IWebPlugin _windowsPlugin;
  105. /// <summary>
  106. /// Logs the warning once so that it doesn't spam the console.
  107. /// </summary>
  108. void _logMockWarningOnce(string reason) {
  109. if (!_mockWarningLogged) {
  110. _mockWarningLogged = true;
  111. WebViewLogger.LogWarning(reason + ", so the mock webview will be used while running in the editor. For more info, please see <em>https://support.vuplex.com/articles/mock-webview</em>.");
  112. }
  113. }
  114. }
  115. }