MockWebPlugin.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 UnityEngine;
  18. namespace Vuplex.WebView
  19. {
  20. /// <summary>
  21. /// Mock IWebPlugin implementation used for running in the Unity editor.
  22. /// </summary>
  23. class MockWebPlugin : IWebPlugin
  24. {
  25. public static MockWebPlugin Instance
  26. {
  27. get
  28. {
  29. if (_instance == null)
  30. {
  31. _instance = new MockWebPlugin();
  32. }
  33. return _instance;
  34. }
  35. }
  36. public WebPluginType Type
  37. {
  38. get
  39. {
  40. return WebPluginType.Mock;
  41. }
  42. }
  43. public void ClearAllData() { }
  44. public void CreateTexture(float width, float height, Action<Texture2D> callback)
  45. {
  46. Dispatcher.RunOnMainThread(() => callback(new Texture2D((int)width, (int)height, TextureFormat.RGBA32, false)));
  47. }
  48. public void CreateMaterial(Action<Material> callback)
  49. {
  50. var material = new Material(Shader.Find("Unlit/Transparent"));
  51. // Create a copy of the texture so that an Exception won't be thrown when the prefab destroys it.
  52. // Also, explicitly use RGBA32 here so that the texture will be converted to RGBA32 if the editor
  53. // imported it as a different format. For example, when Texture Compression is set to ASTC in Android build settings,
  54. // the editor automatically imports new textures as ASTC, even though the Windows editor doesn't support that format.
  55. var texture = new Texture2D(material.mainTexture.width, material.mainTexture.height, TextureFormat.RGBA32, true);
  56. texture.SetPixels((material.mainTexture as Texture2D).GetPixels());
  57. texture.Apply();
  58. material.mainTexture = texture;
  59. Dispatcher.RunOnMainThread(() => callback(material));
  60. }
  61. public void CreateVideoMaterial(Action<Material> callback)
  62. {
  63. callback(null);
  64. }
  65. public virtual IWebView CreateWebView()
  66. {
  67. return MockWebView.Instantiate();
  68. }
  69. public void EnableRemoteDebugging() { }
  70. public void SetIgnoreCertificateErrors(bool ignore) { }
  71. public void SetStorageEnabled(bool enabled) { }
  72. public void SetUserAgent(bool mobile) { }
  73. public void SetUserAgent(string userAgent) { }
  74. static MockWebPlugin _instance;
  75. }
  76. }