MockWebPlugin.cs 2.8 KB

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