LuckyBoxVideoView.cs 5.0 KB

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