UICGView.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Video;
  4. using YooAsset;
  5. using GFGGame.Launcher;
  6. using System.Collections;
  7. namespace GFGGame
  8. {
  9. public class UICGView : SingletonMonoBase<UICGView>
  10. {
  11. private const float VideoWidth = 1920f;
  12. private const float VideoHeight = 1080f;
  13. private GameObject _ui;
  14. private AssetOperationHandle handle;
  15. private RawFileOperationHandle videoHandle;
  16. private RawImage imgVideo;
  17. private Button BtnFullScreen;
  18. private Button BtnSkipVertical;
  19. private Button BtnSkipHorizontal;
  20. private VideoPlayer videoPlayer;
  21. public void Show()
  22. {
  23. MusicManager.Instance.Stop();
  24. string path = ResPathUtil.GetUUIPrefabPath("UICG");
  25. handle = YooAssets.LoadAssetSync<GameObject>(path);
  26. _ui = handle.InstantiateSync(UGUIManager.Instance.desktop.transform);
  27. imgVideo = _ui.transform.Find("ImgVideo").GetComponent<RawImage>();
  28. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  29. imgVideoSizeDelta.x = Screen.width;
  30. imgVideoSizeDelta.y = (VideoHeight / VideoWidth) * imgVideoSizeDelta.x;
  31. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  32. BtnFullScreen = _ui.transform.Find("BtnFullScreen").GetComponent<Button>();
  33. RectTransform rectbtnFullScreen = BtnFullScreen.GetComponent<RectTransform>();
  34. Vector2 buttonSizeDelta = rectbtnFullScreen.sizeDelta;
  35. Vector2 anchoredPosition = rectbtnFullScreen.anchoredPosition;
  36. anchoredPosition.y = -imgVideoSizeDelta.y / 2 - buttonSizeDelta.y;
  37. rectbtnFullScreen.anchoredPosition = anchoredPosition;
  38. BtnFullScreen.onClick.AddListener(OnClickBtnFullScreen);
  39. BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
  40. RectTransform rectBtnSkipVertical = BtnSkipVertical.GetComponent<RectTransform>();
  41. anchoredPosition = rectBtnSkipVertical.anchoredPosition;
  42. anchoredPosition.y = rectbtnFullScreen.anchoredPosition.y;
  43. rectBtnSkipVertical.anchoredPosition = anchoredPosition;
  44. BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
  45. BtnSkipHorizontal = _ui.transform.Find("BtnSkipHorizontal").GetComponent<Button>();
  46. BtnSkipHorizontal.gameObject.SetActive(false);
  47. BtnSkipHorizontal.onClick.AddListener(OnClickrectBtnSkip);
  48. videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
  49. videoPlayer.loopPointReached += OnVideoEnded;
  50. videoPlayer.prepareCompleted += OnVideoPrepared;
  51. string assetPath = ResPathUtil.GetVideoPath("cg");
  52. videoHandle = YooAssets.LoadRawFileSync(assetPath);
  53. videoPlayer.url = videoHandle.GetRawFilePath();
  54. videoPlayer.Play();
  55. }
  56. private void OnVideoPrepared(VideoPlayer source)
  57. {
  58. videoPlayer.prepareCompleted -= OnVideoPrepared;
  59. StartCoroutine(FadeButtonOverTime(BtnFullScreen));
  60. StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
  61. StartCoroutine(FadeButtonOverTime(BtnSkipHorizontal));
  62. }
  63. IEnumerator FadeButtonOverTime(Button btn)
  64. {
  65. Image buttonImage = btn.GetComponent<Image>();
  66. //Image img = btn.GetComponentInChildren<Image>();
  67. yield return new WaitForSeconds((float)videoPlayer.length - 3f);
  68. if (buttonImage != null)
  69. {
  70. // 获取初始颜色
  71. Color startColor = buttonImage.color;
  72. //Color startColorText = img.color;
  73. // 目标颜色,透明度设为 0
  74. Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);
  75. //Color targetColorText = new Color(startColorText.r, startColorText.g, startColorText.b, 0f);
  76. float fadeDuration = 1f;
  77. // 记录开始时间
  78. float startTime = Time.time;
  79. while (Time.time - startTime < fadeDuration)
  80. {
  81. // 在一定时间内逐渐插值颜色
  82. float t = (Time.time - startTime) / fadeDuration;
  83. buttonImage.color = Color.Lerp(startColor, targetColor, t);
  84. //img.color = Color.Lerp(startColorText, targetColorText, t);
  85. yield return null; // 等待下一帧
  86. }
  87. // 设置最终颜色,确保透明度为 0
  88. buttonImage.color = targetColor;
  89. //img.color = targetColorText;
  90. }
  91. }
  92. private void OnVideoEnded(VideoPlayer source)
  93. {
  94. this.Hide();
  95. }
  96. private void OnClickrectBtnSkip()
  97. {
  98. Hide();
  99. }
  100. private void OnClickBtnFullScreen()
  101. {
  102. BtnSkipVertical.gameObject.SetActive(false);
  103. BtnSkipHorizontal.gameObject.SetActive(true);
  104. BtnFullScreen.gameObject.SetActive(false);
  105. imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
  106. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  107. imgVideoSizeDelta.y = Screen.width;
  108. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  109. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  110. if(videoPlayer.time <= 3)
  111. {
  112. videoPlayer.Stop();
  113. videoPlayer.Play();
  114. }
  115. }
  116. public void Hide()
  117. {
  118. StopAllCoroutines();
  119. videoPlayer.Stop();
  120. videoPlayer.loopPointReached -= OnVideoEnded;
  121. handle.Release();
  122. handle = null;
  123. videoHandle.Release();
  124. videoHandle = null;
  125. GameObject.Destroy(_ui);
  126. _ui = null;
  127. StoryController.ShowLevelView(100001001);
  128. }
  129. }
  130. }