PngJpgItem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if (!string.IsNullOrEmpty(fileConfig.Url))
  14. {
  15. string url = fileConfig.Url;
  16. if (!fileConfig.Url.Contains("http"))
  17. {
  18. url = "https://" + fileConfig.Url;
  19. }
  20. NetWorkHeaders.Instance.getNetTexture(url, null, (Texture tex) => {
  21. if (tex)
  22. {
  23. Adaption(tex);
  24. mainImage.texture = tex;
  25. }
  26. });
  27. }
  28. }
  29. public void Adaption(Texture tex)
  30. {
  31. float standard_width = 150f;
  32. float standard_height = 84f;
  33. float video_width = tex.width;
  34. float video_height = tex.height;
  35. //Debug.Log(tex.width + "***" + tex.height);
  36. if (standard_width < video_width && standard_height > video_height)
  37. {
  38. float video_aspect = standard_width / video_width;
  39. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  40. }
  41. else if (standard_width > video_width && standard_height < video_height)
  42. {
  43. float video_aspect = standard_height / video_height;
  44. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  45. }
  46. else if (standard_width > video_width && standard_height > video_height)
  47. {
  48. if (standard_width / video_width > standard_height / video_height)
  49. {
  50. float video_aspect = standard_height / video_height;
  51. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  52. }
  53. else
  54. {
  55. float video_aspect = standard_width / video_width;
  56. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  57. }
  58. }
  59. else
  60. {
  61. if (standard_width / video_width > standard_height / video_height)
  62. {
  63. float video_aspect = standard_height / video_height;
  64. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  65. }
  66. else
  67. {
  68. float video_aspect = standard_width / video_width;
  69. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  70. }
  71. }
  72. }
  73. }