PngJpgItem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using static LangChaoRommMinIo;
  7. public class PngJpgItem : BaseFilePrefabItem
  8. {
  9. public RawImage mainImage;
  10. public RectTransform rectTransform;
  11. public override void Init(FileConfig fileConfig)
  12. {
  13. base.Init(fileConfig);
  14. //LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => {
  15. // Debug.Log("rfd==>" + rfd.url);
  16. // Debug.Log("rfd==>" + rfd.bytes.Length);
  17. // Texture2D texture = new Texture2D(10, 10);
  18. // texture.LoadImage(rfd.bytes);//流数据转换成Texture2D
  19. // texture.Apply();
  20. // Adaption(texture);
  21. // mainImage.texture = texture;
  22. //});
  23. if (!string.IsNullOrEmpty(fileConfig.Url))
  24. {
  25. string url = fileConfig.Url;
  26. if (!fileConfig.Url.Contains("http"))
  27. {
  28. url = "https://" + fileConfig.Url;
  29. }
  30. }
  31. LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => {
  32. Debug.Log("rfd==>" + rfd.url);
  33. Debug.Log("rfd==>" + rfd.bytes.Length);
  34. string url = rfd.url;
  35. NetWorkHeaders.Instance.getNetTexture(url, null, (Texture tex) =>
  36. {
  37. if (tex)
  38. {
  39. Adaption(tex);
  40. mainImage.texture = tex;
  41. }
  42. });
  43. });
  44. }
  45. public void Adaption(Texture tex)
  46. {
  47. float standard_width = 150f;
  48. float standard_height = 84f;
  49. float video_width = tex.width;
  50. float video_height = tex.height;
  51. //Debug.Log(tex.width + "***" + tex.height);
  52. if (standard_width < video_width && standard_height > video_height)
  53. {
  54. float video_aspect = standard_width / video_width;
  55. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  56. }
  57. else if (standard_width > video_width && standard_height < video_height)
  58. {
  59. float video_aspect = standard_height / video_height;
  60. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  61. }
  62. else if (standard_width > video_width && standard_height > video_height)
  63. {
  64. if (standard_width / video_width > standard_height / video_height)
  65. {
  66. float video_aspect = standard_height / video_height;
  67. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  68. }
  69. else
  70. {
  71. float video_aspect = standard_width / video_width;
  72. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  73. }
  74. }
  75. else
  76. {
  77. if (standard_width / video_width > standard_height / video_height)
  78. {
  79. float video_aspect = standard_height / video_height;
  80. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  81. }
  82. else
  83. {
  84. float video_aspect = standard_width / video_width;
  85. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  86. }
  87. }
  88. }
  89. }