LuckyBoxVideoView.cs 5.1 KB

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