PngJpgItem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. mainImage.texture = GetSprite(rfd.bytes).texture;
  36. //NetWorkHeaders.Instance.getNetTexture(url, null, (Texture tex) =>
  37. //{
  38. // if (tex)
  39. // {
  40. // Adaption(tex);
  41. // mainImage.texture = tex;
  42. // }
  43. //});
  44. });
  45. }
  46. public Sprite GetSprite(Byte[] bytes)
  47. {
  48. //先创建一个Texture2D对象,用于把流数据转成Texture2D
  49. Texture2D texture = new Texture2D(10, 10);
  50. texture.LoadImage(bytes);//流数据转换成Texture2D
  51. //创建一个Sprite,以Texture2D对象为基础
  52. Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
  53. return sp;
  54. }
  55. public void Adaption(Texture tex)
  56. {
  57. float standard_width = 150f;
  58. float standard_height = 84f;
  59. float video_width = tex.width;
  60. float video_height = tex.height;
  61. //Debug.Log(tex.width + "***" + tex.height);
  62. if (standard_width < video_width && standard_height > video_height)
  63. {
  64. float video_aspect = standard_width / video_width;
  65. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  66. }
  67. else if (standard_width > video_width && standard_height < video_height)
  68. {
  69. float video_aspect = standard_height / video_height;
  70. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  71. }
  72. else if (standard_width > video_width && standard_height > video_height)
  73. {
  74. if (standard_width / video_width > standard_height / video_height)
  75. {
  76. float video_aspect = standard_height / video_height;
  77. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  78. }
  79. else
  80. {
  81. float video_aspect = standard_width / video_width;
  82. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  83. }
  84. }
  85. else
  86. {
  87. if (standard_width / video_width > standard_height / video_height)
  88. {
  89. float video_aspect = standard_height / video_height;
  90. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  91. }
  92. else
  93. {
  94. float video_aspect = standard_width / video_width;
  95. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  96. }
  97. }
  98. }
  99. }