Mail_ImageItem.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Mail_ImageItem : MonoBehaviour
  6. {
  7. public RawImage mainImage;
  8. public RectTransform rectTransform;
  9. public void SetImageData( string imageData)
  10. {
  11. Debug.Log(imageData);
  12. UILogManager.Instance.text1.text = imageData;
  13. imageData = imageData.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
  14. if (imageData.Length % 4 > 0)
  15. {
  16. imageData = imageData.PadRight(imageData.Length + 4 - imageData.Length % 4, '=');
  17. }
  18. Texture2D pic = new Texture2D(190, 190, TextureFormat.RGBA32, false);
  19. byte[] data = System.Convert.FromBase64String(imageData.Trim('\0'));
  20. pic.LoadImage(data);
  21. Adaption(pic);
  22. mainImage.texture = pic;
  23. }
  24. public void Adaption(Texture tex)
  25. {
  26. float standard_width = 150f;
  27. float standard_height = 84f;
  28. float video_width = tex.width;
  29. float video_height = tex.height;
  30. //Debug.Log(tex.width + "***" + tex.height);
  31. if (standard_width < video_width && standard_height > video_height)
  32. {
  33. float video_aspect = standard_width / video_width;
  34. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  35. }
  36. else if (standard_width > video_width && standard_height < video_height)
  37. {
  38. float video_aspect = standard_height / video_height;
  39. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  40. }
  41. else if (standard_width > video_width && standard_height > video_height)
  42. {
  43. if (standard_width / video_width > standard_height / video_height)
  44. {
  45. float video_aspect = standard_height / video_height;
  46. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  47. }
  48. else
  49. {
  50. float video_aspect = standard_width / video_width;
  51. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  52. }
  53. }
  54. else
  55. {
  56. 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, video_height * video_aspect);
  60. }
  61. else
  62. {
  63. float video_aspect = standard_width / video_width;
  64. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  65. }
  66. }
  67. }
  68. }