Utils.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. using UnityEngine.Rendering;
  20. namespace Vuplex.WebView
  21. {
  22. /// <summary>
  23. /// Static utility methods used internally by 3D WebView.
  24. /// </summary>
  25. public static class Utils
  26. {
  27. public static byte[] ConvertAndroidByteArray(AndroidJavaObject arrayObject)
  28. {
  29. // Unity 2019.1 and newer logs a warning that converting from byte[] is obsolete
  30. // but older versions are incapable of converting from sbyte[].
  31. #if UNITY_2019_1_OR_NEWER
  32. return (byte[])(Array)AndroidJNIHelper.ConvertFromJNIArray<sbyte[]>(arrayObject.GetRawObject());
  33. #else
  34. return AndroidJNIHelper.ConvertFromJNIArray<byte[]>(arrayObject.GetRawObject());
  35. #endif
  36. }
  37. public static Material CreateDefaultMaterial()
  38. {
  39. Material material = null;
  40. Shader shader = ResourceMgr.Instance.FindShader("Vuplex/Viewport Shader", "Assets/Shaders/WebView/Common/ViewportShader");
  41. if (shader)
  42. {
  43. material = new Material(shader);
  44. material.EnableKeyword("FLIP_Y");
  45. material.SetFloat("_FlipY", 1);
  46. material.renderQueue = 3000;
  47. }
  48. return material;
  49. }
  50. public static string GetGraphicsApiErrorMessage(GraphicsDeviceType activeGraphicsApi, GraphicsDeviceType[] acceptableGraphicsApis)
  51. {
  52. var isValid = Array.IndexOf(acceptableGraphicsApis, activeGraphicsApi) != -1;
  53. if (isValid)
  54. {
  55. return null;
  56. }
  57. var acceptableApiStrings = acceptableGraphicsApis.ToList().Select(api => api.ToString());
  58. var acceptableApisList = String.Join(" or ", acceptableApiStrings.ToArray());
  59. return String.Format("Unsupported graphics API: Vuplex 3D WebView requires {0} for this platform, but the selected graphics API is {1}. Please go to Player Settings and set \"Graphics APIs\" to {0}.", acceptableApisList, activeGraphicsApi);
  60. }
  61. public static void ThrowExceptionIfAbnormallyLarge(int width, int height)
  62. {
  63. // Anything over 14.7 megapixels (5k) is almost certainly a mistake.
  64. if (width * height > 14700000)
  65. {
  66. throw new ArgumentException(String.Format("The application specified an abnormally large webview size ({0}px x {1}px), and webviews of this size are normally only created by mistake. A webview's default resolution is 1300px per Unity unit, so it's likely that you specified a large physical size by mistake or need to adjust the resolution. For more information, please see IWebView.SetResolution: https://developer.vuplex.com/webview/IWebView#SetResolution", width, height));
  67. }
  68. }
  69. }
  70. }