LuckyBoxVideoView.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 AudioSource audioSource;
  21. private List<ItemData> _rewardList;
  22. public void Show(List<ItemData> _reward = null)
  23. {
  24. FairyGUI.GRoot.inst.touchable = false;
  25. string path = ResPathUtil.GetUUIPrefabPath("UILuckyBox");
  26. // 异步加载UI预制体
  27. var uiLoadOperation = YooAssets.LoadAssetAsync<GameObject>(path);
  28. uiLoadOperation.Completed += (op) =>
  29. {
  30. if (op.Status == EOperationStatus.Succeed)
  31. {
  32. handle = op;
  33. _ui = handle.InstantiateSync(UGUIManager.Instance.desktop.transform);
  34. InitializeUIComponents();
  35. LoadAndPlayVideo();
  36. }
  37. else
  38. {
  39. Debug.LogError($"Failed to load UI prefab: {path}");
  40. FairyGUI.GRoot.inst.touchable = true; // 恢复触摸
  41. }
  42. };
  43. _rewardList = _reward;
  44. }
  45. private void InitializeUIComponents()
  46. {
  47. imgVideo = _ui.transform.Find("ImgVideo").GetComponent<RawImage>();
  48. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  49. imgVideoSizeDelta.y = Screen.height;
  50. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  51. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  52. BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
  53. BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
  54. BtnSkipVertical.gameObject.SetActive(true);
  55. videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
  56. videoPlayer.loopPointReached += OnVideoEnded;
  57. videoPlayer.prepareCompleted += OnVideoPrepared;
  58. audioSource = _ui.transform.GetComponent<AudioSource>();
  59. }
  60. private void LoadAndPlayVideo()
  61. {
  62. string assetPath = ResPathUtil.GetVideoPath("normalLucky");
  63. var videoLoadOperation = YooAssets.LoadRawFileAsync(assetPath);
  64. videoLoadOperation.Completed += (op) =>
  65. {
  66. if (op.Status == EOperationStatus.Succeed)
  67. {
  68. videoHandle = op;
  69. videoPlayer.url = videoHandle.GetRawFilePath();
  70. videoPlayer.Play();
  71. audioSource.Play();
  72. }
  73. else
  74. {
  75. Debug.LogError($"Failed to load video: {assetPath}");
  76. // 可以在这里处理视频加载失败的情况
  77. }
  78. };
  79. }
  80. private void OnVideoPrepared(VideoPlayer source)
  81. {
  82. videoPlayer.prepareCompleted -= OnVideoPrepared;
  83. StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
  84. }
  85. IEnumerator FadeButtonOverTime(Button btn)
  86. {
  87. Image buttonImage = btn.GetComponent<Image>();
  88. //Image img = btn.GetComponentInChildren<Image>();
  89. yield return new WaitForSeconds((float)videoPlayer.length - 3f);
  90. if (buttonImage != null)
  91. {
  92. // 获取初始颜色
  93. Color startColor = buttonImage.color;
  94. //Color startColorText = img.color;
  95. // 目标颜色,透明度设为 0
  96. Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);
  97. //Color targetColorText = new Color(startColorText.r, startColorText.g, startColorText.b, 0f);
  98. float fadeDuration = 1f;
  99. // 记录开始时间
  100. float startTime = Time.time;
  101. while (Time.time - startTime < fadeDuration)
  102. {
  103. // 在一定时间内逐渐插值颜色
  104. float t = (Time.time - startTime) / fadeDuration;
  105. buttonImage.color = Color.Lerp(startColor, targetColor, t);
  106. //img.color = Color.Lerp(startColorText, targetColorText, t);
  107. yield return null; // 等待下一帧
  108. }
  109. // 设置最终颜色,确保透明度为 0
  110. buttonImage.color = targetColor;
  111. //img.color = targetColorText;
  112. }
  113. }
  114. private void OnVideoEnded(VideoPlayer source)
  115. {
  116. this.Hide();
  117. }
  118. private void OnClickrectBtnSkip()
  119. {
  120. GetSuitItemController.isAuto = true;
  121. Hide();
  122. }
  123. private void OnClickBtnFullScreen()
  124. {
  125. BtnSkipVertical.gameObject.SetActive(false);
  126. imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
  127. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  128. imgVideoSizeDelta.y = Screen.width;
  129. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  130. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  131. if (videoPlayer.time <= 3)
  132. {
  133. videoPlayer.Stop();
  134. videoPlayer.Play();
  135. }
  136. }
  137. public void Hide()
  138. {
  139. videoPlayer.Pause();
  140. StopAllCoroutines();
  141. videoPlayer.Stop();
  142. videoPlayer.loopPointReached -= OnVideoEnded;
  143. videoPlayer.targetTexture.Release();
  144. handle.Release();
  145. handle = null;
  146. videoHandle.Release();
  147. videoHandle = null;
  148. GameObject.Destroy(_ui);
  149. _ui = null;
  150. FairyGUI.GRoot.inst.touchable = true;
  151. ViewManager.Show<LuckyBoxBonusShowView>(_rewardList);
  152. }
  153. }
  154. }