LuckyBoxVideoView.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Video;
  4. using YooAsset;
  5. using GFGGame.Launcher;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. namespace GFGGame
  9. {
  10. public class LuckyBoxVideoView : SingletonMonoBase<LuckyBoxVideoView>
  11. {
  12. private const float VideoWidth = 1080f;
  13. private const float VideoHeight = 1920f;
  14. private GameObject _ui;
  15. private AssetOperationHandle handle;
  16. private RawFileOperationHandle videoHandle;
  17. private RawImage imgVideo;
  18. private Button BtnSkipVertical;
  19. private VideoPlayer videoPlayer;
  20. private List<ItemData> _rewardList;
  21. public void Show(List<ItemData> _reward = null)
  22. {
  23. MusicManager.Instance.Stop();
  24. string path = ResPathUtil.GetUUIPrefabPath("UILuckyBox");
  25. handle = YooAssets.LoadAssetSync<GameObject>(path);
  26. _ui = handle.InstantiateSync(UGUIManager.Instance.desktop.transform);
  27. imgVideo = _ui.transform.Find("ImgVideo").GetComponent<RawImage>();
  28. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  29. imgVideoSizeDelta.y = Screen.height;
  30. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  31. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  32. BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
  33. BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
  34. BtnSkipVertical.gameObject.SetActive(false);
  35. videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
  36. videoPlayer.loopPointReached += OnVideoEnded;
  37. videoPlayer.prepareCompleted += OnVideoPrepared;
  38. string assetPath = ResPathUtil.GetVideoPath("normalLucky");
  39. videoHandle = YooAssets.LoadRawFileSync(assetPath);
  40. videoPlayer.url = videoHandle.GetRawFilePath();
  41. videoPlayer.Play();
  42. _rewardList = _reward;
  43. }
  44. private void OnVideoPrepared(VideoPlayer source)
  45. {
  46. videoPlayer.prepareCompleted -= OnVideoPrepared;
  47. StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
  48. }
  49. IEnumerator FadeButtonOverTime(Button btn)
  50. {
  51. Image buttonImage = btn.GetComponent<Image>();
  52. //Image img = btn.GetComponentInChildren<Image>();
  53. yield return new WaitForSeconds((float)videoPlayer.length - 3f);
  54. if (buttonImage != null)
  55. {
  56. // 获取初始颜色
  57. Color startColor = buttonImage.color;
  58. //Color startColorText = img.color;
  59. // 目标颜色,透明度设为 0
  60. Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);
  61. //Color targetColorText = new Color(startColorText.r, startColorText.g, startColorText.b, 0f);
  62. float fadeDuration = 1f;
  63. // 记录开始时间
  64. float startTime = Time.time;
  65. while (Time.time - startTime < fadeDuration)
  66. {
  67. // 在一定时间内逐渐插值颜色
  68. float t = (Time.time - startTime) / fadeDuration;
  69. buttonImage.color = Color.Lerp(startColor, targetColor, t);
  70. //img.color = Color.Lerp(startColorText, targetColorText, t);
  71. yield return null; // 等待下一帧
  72. }
  73. // 设置最终颜色,确保透明度为 0
  74. buttonImage.color = targetColor;
  75. //img.color = targetColorText;
  76. }
  77. }
  78. private void OnVideoEnded(VideoPlayer source)
  79. {
  80. this.Hide();
  81. }
  82. private void OnClickrectBtnSkip()
  83. {
  84. Hide();
  85. }
  86. private void OnClickBtnFullScreen()
  87. {
  88. BtnSkipVertical.gameObject.SetActive(false);
  89. imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
  90. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  91. imgVideoSizeDelta.y = Screen.width;
  92. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  93. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  94. if (videoPlayer.time <= 3)
  95. {
  96. videoPlayer.Stop();
  97. videoPlayer.Play();
  98. }
  99. }
  100. public void Hide()
  101. {
  102. StopAllCoroutines();
  103. videoPlayer.Stop();
  104. videoPlayer.loopPointReached -= OnVideoEnded;
  105. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
  106. handle.Release();
  107. handle = null;
  108. videoHandle.Release();
  109. videoHandle = null;
  110. GameObject.Destroy(_ui);
  111. _ui = null;
  112. ViewManager.Show<LuckyBoxBonusShowView>(_rewardList);
  113. }
  114. }
  115. }