UICGView.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 AssetHandle handle;
  15. private RawFileHandle 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. // 异步加载UI预制体
  26. var uiLoadOperation = YooAssets.LoadAssetAsync<GameObject>(path);
  27. uiLoadOperation.Completed += (op) =>
  28. {
  29. if (op.Status == EOperationStatus.Succeed)
  30. {
  31. _ui = op.InstantiateSync(UGUIManager.Instance.desktop.transform);
  32. InitializeUIComponents();
  33. LoadAndPlayVideo();
  34. }
  35. else
  36. {
  37. Debug.LogError($"Failed to load UI prefab: {path}");
  38. }
  39. };
  40. }
  41. private void InitializeUIComponents()
  42. {
  43. imgVideo = _ui.transform.Find("ImgVideo").GetComponent<RawImage>();
  44. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  45. imgVideoSizeDelta.x = Screen.width;
  46. imgVideoSizeDelta.y = (VideoHeight / VideoWidth) * imgVideoSizeDelta.x;
  47. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  48. BtnFullScreen = _ui.transform.Find("BtnFullScreen").GetComponent<Button>();
  49. RectTransform rectbtnFullScreen = BtnFullScreen.GetComponent<RectTransform>();
  50. Vector2 buttonSizeDelta = rectbtnFullScreen.sizeDelta;
  51. Vector2 anchoredPosition = rectbtnFullScreen.anchoredPosition;
  52. anchoredPosition.y = -imgVideoSizeDelta.y / 2 - buttonSizeDelta.y;
  53. rectbtnFullScreen.anchoredPosition = anchoredPosition;
  54. BtnFullScreen.onClick.AddListener(OnClickBtnFullScreen);
  55. BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
  56. RectTransform rectBtnSkipVertical = BtnSkipVertical.GetComponent<RectTransform>();
  57. anchoredPosition = rectBtnSkipVertical.anchoredPosition;
  58. anchoredPosition.y = rectbtnFullScreen.anchoredPosition.y;
  59. rectBtnSkipVertical.anchoredPosition = anchoredPosition;
  60. BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
  61. BtnSkipHorizontal = _ui.transform.Find("BtnSkipHorizontal").GetComponent<Button>();
  62. BtnSkipHorizontal.gameObject.SetActive(false);
  63. BtnSkipHorizontal.onClick.AddListener(OnClickrectBtnSkip);
  64. videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
  65. videoPlayer.loopPointReached += OnVideoEnded;
  66. videoPlayer.prepareCompleted += OnVideoPrepared;
  67. }
  68. private void LoadAndPlayVideo()
  69. {
  70. string assetPath = ResPathUtil.GetVideoPath("cg");
  71. var videoLoadOperation = YooAssets.LoadRawFileAsync(assetPath);
  72. videoLoadOperation.Completed += (op) =>
  73. {
  74. if (op.Status == EOperationStatus.Succeed)
  75. {
  76. videoHandle = op;
  77. videoPlayer.url = videoHandle.GetRawFilePath();
  78. videoPlayer.Play();
  79. }
  80. else
  81. {
  82. Debug.LogError($"Failed to load video: {assetPath}");
  83. }
  84. };
  85. }
  86. private void OnVideoPrepared(VideoPlayer source)
  87. {
  88. videoPlayer.prepareCompleted -= OnVideoPrepared;
  89. StartCoroutine(FadeButtonOverTime(BtnFullScreen));
  90. StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
  91. StartCoroutine(FadeButtonOverTime(BtnSkipHorizontal));
  92. }
  93. IEnumerator FadeButtonOverTime(Button btn)
  94. {
  95. Image buttonImage = btn.GetComponent<Image>();
  96. yield return new WaitForSeconds((float)videoPlayer.length - 3f);
  97. if (buttonImage != null)
  98. {
  99. Color startColor = buttonImage.color;
  100. Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);
  101. float fadeDuration = 1f;
  102. float startTime = Time.time;
  103. while (Time.time - startTime < fadeDuration)
  104. {
  105. float t = (Time.time - startTime) / fadeDuration;
  106. buttonImage.color = Color.Lerp(startColor, targetColor, t);
  107. yield return null;
  108. }
  109. buttonImage.color = targetColor;
  110. }
  111. }
  112. private void OnVideoEnded(VideoPlayer source)
  113. {
  114. this.Hide();
  115. }
  116. private void OnClickrectBtnSkip()
  117. {
  118. Hide();
  119. }
  120. private void OnClickBtnFullScreen()
  121. {
  122. BtnSkipVertical.gameObject.SetActive(false);
  123. BtnSkipHorizontal.gameObject.SetActive(true);
  124. BtnFullScreen.gameObject.SetActive(false);
  125. imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
  126. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  127. imgVideoSizeDelta.y = Screen.width;
  128. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  129. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  130. if (videoPlayer.time <= 3)
  131. {
  132. videoPlayer.Stop();
  133. videoPlayer.Play();
  134. }
  135. RectTransform rectBtnSkipVertical = BtnSkipHorizontal.GetComponent<RectTransform>();
  136. Vector2 anchoredPosition = rectBtnSkipVertical.anchoredPosition;
  137. anchoredPosition.y = (Screen.height - imgVideoSizeDelta.x) / 2 + rectBtnSkipVertical.sizeDelta.x / 2;
  138. rectBtnSkipVertical.anchoredPosition = anchoredPosition;
  139. }
  140. public void Hide()
  141. {
  142. StopAllCoroutines();
  143. if (videoPlayer != null)
  144. {
  145. videoPlayer.Stop();
  146. videoPlayer.loopPointReached -= OnVideoEnded;
  147. }
  148. if (handle != null)
  149. {
  150. handle.Release();
  151. handle = null;
  152. }
  153. if (videoHandle != null)
  154. {
  155. videoHandle.Release();
  156. videoHandle = null;
  157. }
  158. if (_ui != null)
  159. {
  160. GameObject.Destroy(_ui);
  161. _ui = null;
  162. }
  163. StoryController.ShowLevelView(100001001);
  164. }
  165. }
  166. }