MockWebPlugin.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.Threading.Tasks;
  16. using UnityEngine;
  17. using Vuplex.WebView.Internal;
  18. namespace Vuplex.WebView {
  19. /// <summary>
  20. /// Mock IWebPlugin implementation used for running in the Unity editor.
  21. /// </summary>
  22. class MockWebPlugin : IWebPlugin {
  23. public ICookieManager CookieManager { get; } = MockCookieManager.Instance;
  24. public static MockWebPlugin Instance {
  25. get {
  26. if (_instance == null) {
  27. _instance = new MockWebPlugin();
  28. }
  29. return _instance;
  30. }
  31. }
  32. public WebPluginType Type { get; } = WebPluginType.Mock;
  33. public void ClearAllData() {}
  34. // Deprecated
  35. public void CreateMaterial(Action<Material> callback) {
  36. var material = new Material(Resources.Load<Material>("MockViewportMaterial"));
  37. // Create a copy of the texture so that an Exception won't be thrown when the prefab destroys it.
  38. // Also, explicitly use RGBA32 here so that the texture will be converted to RGBA32 if the editor
  39. // imported it as a different format. For example, when Texture Compression is set to ASTC in Android build settings,
  40. // the editor automatically imports new textures as ASTC, even though the Windows editor doesn't support that format.
  41. var texture = new Texture2D(material.mainTexture.width, material.mainTexture.height, TextureFormat.RGBA32, true);
  42. texture.SetPixels((material.mainTexture as Texture2D).GetPixels());
  43. texture.Apply();
  44. material.mainTexture = texture;
  45. Dispatcher.RunOnMainThread(() => callback(material));
  46. }
  47. public virtual IWebView CreateWebView() => MockWebView.Instantiate();
  48. public void EnableRemoteDebugging() {}
  49. public void SetAutoplayEnabled(bool enabled) {}
  50. public void SetCameraAndMicrophoneEnabled(bool enabled) {}
  51. public void SetIgnoreCertificateErrors(bool ignore) {}
  52. public void SetStorageEnabled(bool enabled) {}
  53. public void SetUserAgent(bool mobile) {}
  54. public void SetUserAgent(string userAgent) {}
  55. static MockWebPlugin _instance;
  56. }
  57. }