LuckyBoxVideoView.cs 4.6 KB

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