Mail_ImageItem.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 SetImageData(Texture imgSprite)
  25. {
  26. Adaption(imgSprite);
  27. mainImage.texture = imgSprite;
  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. }