LuckyBoxVideoView.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
  29. BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
  30. videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
  31. videoPlayer.loopPointReached += OnVideoEnded;
  32. videoPlayer.prepareCompleted += OnVideoPrepared;
  33. string assetPath = ResPathUtil.GetVideoPath("normalLucky");
  34. videoHandle = YooAssets.LoadRawFileSync(assetPath);
  35. videoPlayer.url = videoHandle.GetRawFilePath();
  36. videoPlayer.Play();
  37. _rewardList = _reward;
  38. }
  39. private void OnVideoPrepared(VideoPlayer source)
  40. {
  41. videoPlayer.prepareCompleted -= OnVideoPrepared;
  42. StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
  43. }
  44. IEnumerator FadeButtonOverTime(Button btn)
  45. {
  46. Image buttonImage = btn.GetComponent<Image>();
  47. //Image img = btn.GetComponentInChildren<Image>();
  48. yield return new WaitForSeconds((float)videoPlayer.length - 3f);
  49. if (buttonImage != null)
  50. {
  51. // 获取初始颜色
  52. Color startColor = buttonImage.color;
  53. //Color startColorText = img.color;
  54. // 目标颜色,透明度设为 0
  55. Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);
  56. //Color targetColorText = new Color(startColorText.r, startColorText.g, startColorText.b, 0f);
  57. float fadeDuration = 1f;
  58. // 记录开始时间
  59. float startTime = Time.time;
  60. while (Time.time - startTime < fadeDuration)
  61. {
  62. // 在一定时间内逐渐插值颜色
  63. float t = (Time.time - startTime) / fadeDuration;
  64. buttonImage.color = Color.Lerp(startColor, targetColor, t);
  65. //img.color = Color.Lerp(startColorText, targetColorText, t);
  66. yield return null; // 等待下一帧
  67. }
  68. // 设置最终颜色,确保透明度为 0
  69. buttonImage.color = targetColor;
  70. //img.color = targetColorText;
  71. }
  72. }
  73. private void OnVideoEnded(VideoPlayer source)
  74. {
  75. this.Hide();
  76. }
  77. private void OnClickrectBtnSkip()
  78. {
  79. Hide();
  80. }
  81. private void OnClickBtnFullScreen()
  82. {
  83. BtnSkipVertical.gameObject.SetActive(false);
  84. imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
  85. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  86. imgVideoSizeDelta.y = Screen.width;
  87. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  88. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  89. if (videoPlayer.time <= 3)
  90. {
  91. videoPlayer.Stop();
  92. videoPlayer.Play();
  93. }
  94. }
  95. public void Hide()
  96. {
  97. StopAllCoroutines();
  98. videoPlayer.Stop();
  99. videoPlayer.loopPointReached -= OnVideoEnded;
  100. handle.Release();
  101. handle = null;
  102. videoHandle.Release();
  103. videoHandle = null;
  104. GameObject.Destroy(_ui);
  105. _ui = null;
  106. ViewManager.Show<LuckyBoxBonusShowView>(_rewardList);
  107. }
  108. }
  109. }