PngJpgItem.cs 3.5 KB

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