TextureDownloader.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace Imagine.WebAR
  6. {
  7. public class TextureDownloader : MonoBehaviour
  8. {
  9. [DllImport("__Internal")] private static extern void DownloadWebGLTexture(byte[] img, int size, string filename, string extension);
  10. private enum FileExtension { PNG, JPEG};
  11. [SerializeField] private FileExtension fileExt = FileExtension.PNG;
  12. public void DownloadTexture(Texture2D texture)
  13. {
  14. if(fileExt == FileExtension.PNG)
  15. {
  16. var bytes = texture.EncodeToPNG();
  17. #if UNITY_WEBGL && !UNITY_EDITOR
  18. DownloadWebGLTexture(bytes, bytes.Length, texture.name, ".png");
  19. #endif
  20. }
  21. else if (fileExt == FileExtension.JPEG)
  22. {
  23. var bytes = texture.EncodeToJPG();
  24. #if UNITY_WEBGL && !UNITY_EDITOR
  25. DownloadWebGLTexture(bytes, bytes.Length, texture.name, ".jpeg");
  26. #endif
  27. }
  28. #if !UNITY_WEBGL || UNITY_EDITOR
  29. Debug.Log("Texture downloads only available in WebGL builds");
  30. #endif
  31. }
  32. }
  33. }