Mp4Item.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Video;
  6. public class Mp4Item : BaseFilePrefabItem
  7. {
  8. public RawImage showVideo;
  9. public RectTransform rectTransform;
  10. public VideoPlayer videoPlayer;
  11. public override void Init(FileConfig fileConfig)
  12. {
  13. base.Init(fileConfig);
  14. if (!string.IsNullOrEmpty(fileConfig.Url))
  15. {
  16. string url = fileConfig.Url;
  17. if (!fileConfig.Url.Contains("http"))
  18. {
  19. url = "https://" + fileConfig.Url;
  20. }
  21. videoPlayer.url = url;
  22. }
  23. }
  24. protected override void Start()
  25. {
  26. base.Start();
  27. StartCoroutine(enumerator());
  28. }
  29. private IEnumerator enumerator()
  30. {
  31. while (!videoPlayer.isPrepared)
  32. {
  33. yield return null;
  34. }
  35. float standard_width = 150f;
  36. float standard_height = 84f;
  37. float video_width = videoPlayer.texture.width;
  38. float video_height = videoPlayer.texture.height;
  39. if (standard_width < video_width && standard_height > video_height)
  40. {
  41. float video_aspect = standard_width / video_width;
  42. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  43. }
  44. else if (standard_width > video_width && standard_height < video_height)
  45. {
  46. float video_aspect = standard_height / video_height;
  47. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  48. }
  49. else if (standard_width > video_width && standard_height > video_height)
  50. {
  51. if (standard_width / video_width > standard_height / video_height)
  52. {
  53. float video_aspect = standard_height / video_height;
  54. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  55. }
  56. else
  57. {
  58. float video_aspect = standard_width / video_width;
  59. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  60. }
  61. }
  62. else
  63. {
  64. if (standard_width / video_width > standard_height / video_height)
  65. {
  66. float video_aspect = standard_height / video_height;
  67. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  68. }
  69. else
  70. {
  71. float video_aspect = standard_width / video_width;
  72. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  73. }
  74. }
  75. }
  76. }