PdfManager.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Paroxe.PdfRenderer;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using static LangChaoRommMinIo;
  8. public class PdfManager : BaseFilePrefabItem
  9. {
  10. private PDFViewer pdfview;
  11. public override void Init(FileConfig fileConfig)
  12. {
  13. base.Init(fileConfig);
  14. if (!string.IsNullOrEmpty(fileConfig.Url))
  15. {
  16. string url = fileConfig.Url;
  17. if (!fileConfig.Url.Contains("http"))
  18. {
  19. url = "https://" + fileConfig.Url;
  20. }
  21. showPDF(url);
  22. LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => {
  23. Debug.Log("rfd==>" + rfd.url);
  24. Debug.Log("rfd==>" + rfd.bytes.Length);
  25. string url = rfd.url;
  26. // StartCoroutine(SavePDF(Application.persistentDataPath + "/" + fileConfig.UUid + Path.GetExtension(fileConfig.FileName), rfd.bytes));
  27. });
  28. }
  29. }
  30. private IEnumerator SavePDF(string path, byte[] bytes)
  31. {
  32. Debug.Log(" Path ==> " + path);
  33. FileInfo file = new FileInfo(path);
  34. if (file.Exists)
  35. {
  36. // file.Delete();
  37. yield return new WaitForSeconds(0.02f);
  38. showPDF(path);
  39. }
  40. else
  41. {
  42. Stream sw = File.Create(path);
  43. // FileStream sw = new FileStream(path);
  44. sw.Write(bytes, 0, bytes.Length);
  45. sw.Close();
  46. sw.Dispose();
  47. yield return new WaitForSeconds(0.02f);
  48. showPDF(path);
  49. }
  50. }
  51. public void showPDF(string url)
  52. {
  53. //this.transform.localPosition = new Vector3(0,0,0.69f);
  54. pdfview = this.GetComponent<PDFViewer>();
  55. pdfview.FileURL = url;
  56. pdfview.LoadDocument();
  57. }
  58. public override void OnScaleStopped()
  59. {
  60. if (transform.localScale.x < 0.0001f)
  61. {
  62. transform.localScale = new Vector3(0.0001f, 0.0001f, 0.0001f);
  63. }
  64. }
  65. }