StandaloneWebPlugin.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
  17. using System;
  18. using UnityEngine;
  19. namespace Vuplex.WebView {
  20. /// <summary>
  21. /// The base class for `WindowsWebPlugin` and `MacWebPlugin`.
  22. /// </summary>
  23. class StandaloneWebPlugin : MonoBehaviour {
  24. public void ClearAllData() {
  25. StandaloneWebView.ClearAllData();
  26. }
  27. public void CreateTexture(float width, float height, Action<Texture2D> callback) {
  28. StandaloneWebView.CreateTexture(width, height, callback);
  29. }
  30. public void CreateMaterial(Action<Material> callback) {
  31. CreateTexture(1, 1, texture => {
  32. var material = Utils.CreateDefaultMaterial();
  33. material.mainTexture = texture;
  34. callback(material);
  35. });
  36. }
  37. public void CreateVideoMaterial(Action<Material> callback) {
  38. callback(null);
  39. }
  40. public void EnableRemoteDebugging() {
  41. var platform = Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor ? "Windows" : "macOS";
  42. WebViewLogger.LogFormat("Enabling remote debugging for {0} on port 8080. Please visit http://localhost:8080 using a Chromium-based browser. For more info, see <em>https://support.vuplex.com/articles/how-to-debug-web-content#standalone</em>.", platform);
  43. StandaloneWebView.EnableRemoteDebugging(8080);
  44. }
  45. public void SetIgnoreCertificateErrors(bool ignore) {
  46. StandaloneWebView.SetIgnoreCertificateErrors(ignore);
  47. }
  48. public void SetStorageEnabled(bool enabled) {
  49. StandaloneWebView.SetStorageEnabled(enabled);
  50. }
  51. public void SetUserAgent(bool mobile) {
  52. StandaloneWebView.GloballySetUserAgent(mobile);
  53. }
  54. public void SetUserAgent(string userAgent) {
  55. StandaloneWebView.GloballySetUserAgent(userAgent);
  56. }
  57. #if UNITY_2018_1_OR_NEWER && !UNITY_2020_1_OR_NEWER
  58. void Start() {
  59. // In Unity 2018.1 - 2019.4, use Application.quitting instead
  60. // of OnApplicationQuit(), because the latter is called even if
  61. // the quit is cancelled by the application returning false from
  62. // Application.wantsToQuit, and the former is called only when the
  63. // application really quits.
  64. Application.quitting += () => StandaloneWebView.TerminatePlugin();
  65. }
  66. #else
  67. // Prior to Unity 2018.1, Application.quitting and Application.wantsToQuit
  68. // don't exist. In Unity 2020.1 and 2020.2, the Windows player has a bug
  69. // where Application.quitting isn't raised when the application is quit with alt+f4.
  70. // https://issuetracker.unity3d.com/issues/application-dot-quitting-event-is-not-raised-when-closing-build
  71. void OnApplicationQuit() {
  72. StandaloneWebView.TerminatePlugin();
  73. }
  74. #endif
  75. }
  76. }
  77. #endif