UICGView.cs 5.9 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. using System;
  8. namespace GFGGame
  9. {
  10. public class UICGView : SingletonMonoBase<UICGView>
  11. {
  12. private const float VideoWidth = 1920f;
  13. private const float VideoHeight = 1080f;
  14. private GameObject _ui;
  15. private AssetOperationHandle handle;
  16. private RawFileOperationHandle videoHandle;
  17. private RawImage imgVideo;
  18. private Button BtnFullScreen;
  19. private Button BtnSkipVertical;
  20. private Button BtnSkipHorizontal;
  21. private VideoPlayer videoPlayer;
  22. private Coroutine fadeCoroutine;
  23. public void Show()
  24. {
  25. MusicManager.Instance.Stop();
  26. string path = ResPathUtil.GetUUIPrefabPath("UICG");
  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.x = Screen.width;
  32. imgVideoSizeDelta.y = (VideoHeight / VideoWidth) * imgVideoSizeDelta.x;
  33. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  34. BtnFullScreen = _ui.transform.Find("BtnFullScreen").GetComponent<Button>();
  35. RectTransform rectbtnFullScreen = BtnFullScreen.GetComponent<RectTransform>();
  36. Vector2 buttonSizeDelta = rectbtnFullScreen.sizeDelta;
  37. Vector2 anchoredPosition = rectbtnFullScreen.anchoredPosition;
  38. anchoredPosition.y = -imgVideoSizeDelta.y / 2 - buttonSizeDelta.y;
  39. rectbtnFullScreen.anchoredPosition = anchoredPosition;
  40. BtnFullScreen.onClick.AddListener(OnClickBtnFullScreen);
  41. BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
  42. RectTransform rectBtnSkipVertical = BtnSkipVertical.GetComponent<RectTransform>();
  43. anchoredPosition = rectBtnSkipVertical.anchoredPosition;
  44. anchoredPosition.y = rectbtnFullScreen.anchoredPosition.y;
  45. rectBtnSkipVertical.anchoredPosition = anchoredPosition;
  46. BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
  47. BtnSkipHorizontal = _ui.transform.Find("BtnSkipHorizontal").GetComponent<Button>();
  48. BtnSkipHorizontal.gameObject.SetActive(false);
  49. BtnSkipHorizontal.onClick.AddListener(OnClickrectBtnSkip);
  50. videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
  51. videoPlayer.loopPointReached += OnVideoEnded;
  52. videoPlayer.prepareCompleted += OnVideoPrepared;
  53. string assetPath = ResPathUtil.GetVideoPath("cg");
  54. videoHandle = YooAssets.LoadRawFileSync(assetPath);
  55. videoPlayer.url = videoHandle.GetRawFilePath();
  56. videoPlayer.Play();
  57. }
  58. private void OnVideoPrepared(VideoPlayer source)
  59. {
  60. videoPlayer.prepareCompleted -= OnVideoPrepared;
  61. fadeCoroutine = StartCoroutine(FadeButtonOverTime());
  62. }
  63. IEnumerator FadeButtonOverTime()
  64. {
  65. Image buttonImage = BtnFullScreen.GetComponent<Image>();
  66. Text buttonText = BtnFullScreen.GetComponentInChildren<Text>();
  67. yield return new WaitForSeconds((float)videoPlayer.length - 3f);
  68. if (buttonImage != null)
  69. {
  70. // 获取初始颜色
  71. Color startColor = buttonImage.color;
  72. Color startColorText = buttonText.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. buttonText.color = Color.Lerp(startColorText, targetColorText, t);
  85. yield return null; // 等待下一帧
  86. }
  87. // 设置最终颜色,确保透明度为 0
  88. buttonImage.color = targetColor;
  89. buttonText.color = targetColorText;
  90. }
  91. fadeCoroutine = null;
  92. }
  93. private void OnVideoEnded(VideoPlayer source)
  94. {
  95. this.Hide();
  96. }
  97. private void OnClickrectBtnSkip()
  98. {
  99. Hide();
  100. }
  101. private void OnClickBtnFullScreen()
  102. {
  103. BtnSkipVertical.gameObject.SetActive(false);
  104. BtnSkipHorizontal.gameObject.SetActive(true);
  105. BtnFullScreen.gameObject.SetActive(false);
  106. imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
  107. Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
  108. imgVideoSizeDelta.y = Screen.width;
  109. imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
  110. imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
  111. if(videoPlayer.time <= 3)
  112. {
  113. videoPlayer.Stop();
  114. videoPlayer.Play();
  115. }
  116. }
  117. public void Hide()
  118. {
  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. }