| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Video;
- using YooAsset;
- using GFGGame.Launcher;
- using System.Collections;
- namespace GFGGame
- {
- public class UICGView : SingletonMonoBase<UICGView>
- {
- private const float VideoWidth = 1920f;
- private const float VideoHeight = 1080f;
- private GameObject _ui;
- private AssetHandle handle;
- private RawFileHandle videoHandle;
- private RawImage imgVideo;
- private Button BtnFullScreen;
- private Button BtnSkipVertical;
- private Button BtnSkipHorizontal;
- private VideoPlayer videoPlayer;
- public void Show()
- {
- MusicManager.Instance.Stop();
- string path = ResPathUtil.GetUUIPrefabPath("UICG");
- // 异步加载UI预制体
- var uiLoadOperation = YooAssets.LoadAssetAsync<GameObject>(path);
- uiLoadOperation.Completed += (op) =>
- {
- if (op.Status == EOperationStatus.Succeed)
- {
- _ui = op.InstantiateSync(UGUIManager.Instance.desktop.transform);
- InitializeUIComponents();
- LoadAndPlayVideo();
- }
- else
- {
- Debug.LogError($"Failed to load UI prefab: {path}");
- }
- };
- }
- private void InitializeUIComponents()
- {
- imgVideo = _ui.transform.Find("ImgVideo").GetComponent<RawImage>();
- Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
- imgVideoSizeDelta.x = Screen.width;
- imgVideoSizeDelta.y = (VideoHeight / VideoWidth) * imgVideoSizeDelta.x;
- imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
- BtnFullScreen = _ui.transform.Find("BtnFullScreen").GetComponent<Button>();
- RectTransform rectbtnFullScreen = BtnFullScreen.GetComponent<RectTransform>();
- Vector2 buttonSizeDelta = rectbtnFullScreen.sizeDelta;
- Vector2 anchoredPosition = rectbtnFullScreen.anchoredPosition;
- anchoredPosition.y = -imgVideoSizeDelta.y / 2 - buttonSizeDelta.y;
- rectbtnFullScreen.anchoredPosition = anchoredPosition;
- BtnFullScreen.onClick.AddListener(OnClickBtnFullScreen);
- BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
- RectTransform rectBtnSkipVertical = BtnSkipVertical.GetComponent<RectTransform>();
- anchoredPosition = rectBtnSkipVertical.anchoredPosition;
- anchoredPosition.y = rectbtnFullScreen.anchoredPosition.y;
- rectBtnSkipVertical.anchoredPosition = anchoredPosition;
- BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
- BtnSkipHorizontal = _ui.transform.Find("BtnSkipHorizontal").GetComponent<Button>();
- BtnSkipHorizontal.gameObject.SetActive(false);
- BtnSkipHorizontal.onClick.AddListener(OnClickrectBtnSkip);
- videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
- videoPlayer.loopPointReached += OnVideoEnded;
- videoPlayer.prepareCompleted += OnVideoPrepared;
- }
- private void LoadAndPlayVideo()
- {
- string assetPath = ResPathUtil.GetVideoPath("cg");
- var videoLoadOperation = YooAssets.LoadRawFileAsync(assetPath);
- videoLoadOperation.Completed += (op) =>
- {
- if (op.Status == EOperationStatus.Succeed)
- {
- videoHandle = op;
- videoPlayer.url = videoHandle.GetRawFilePath();
- videoPlayer.Play();
- }
- else
- {
- Debug.LogError($"Failed to load video: {assetPath}");
- }
- };
- }
- private void OnVideoPrepared(VideoPlayer source)
- {
- videoPlayer.prepareCompleted -= OnVideoPrepared;
- StartCoroutine(FadeButtonOverTime(BtnFullScreen));
- StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
- StartCoroutine(FadeButtonOverTime(BtnSkipHorizontal));
- }
- IEnumerator FadeButtonOverTime(Button btn)
- {
- Image buttonImage = btn.GetComponent<Image>();
- yield return new WaitForSeconds((float)videoPlayer.length - 3f);
- if (buttonImage != null)
- {
- Color startColor = buttonImage.color;
- Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);
- float fadeDuration = 1f;
- float startTime = Time.time;
- while (Time.time - startTime < fadeDuration)
- {
- float t = (Time.time - startTime) / fadeDuration;
- buttonImage.color = Color.Lerp(startColor, targetColor, t);
- yield return null;
- }
- buttonImage.color = targetColor;
- }
- }
- private void OnVideoEnded(VideoPlayer source)
- {
- this.Hide();
- }
- private void OnClickrectBtnSkip()
- {
- Hide();
- }
- private void OnClickBtnFullScreen()
- {
- BtnSkipVertical.gameObject.SetActive(false);
- BtnSkipHorizontal.gameObject.SetActive(true);
- BtnFullScreen.gameObject.SetActive(false);
- imgVideo.rectTransform.localRotation = Quaternion.Euler(0, 0, -90);
- Vector2 imgVideoSizeDelta = imgVideo.rectTransform.sizeDelta;
- imgVideoSizeDelta.y = Screen.width;
- imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
- imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
- if (videoPlayer.time <= 3)
- {
- videoPlayer.Stop();
- videoPlayer.Play();
- }
- RectTransform rectBtnSkipVertical = BtnSkipHorizontal.GetComponent<RectTransform>();
- Vector2 anchoredPosition = rectBtnSkipVertical.anchoredPosition;
- anchoredPosition.y = (Screen.height - imgVideoSizeDelta.x) / 2 + rectBtnSkipVertical.sizeDelta.x / 2;
- rectBtnSkipVertical.anchoredPosition = anchoredPosition;
- }
- public void Hide()
- {
- StopAllCoroutines();
- if (videoPlayer != null)
- {
- videoPlayer.Stop();
- videoPlayer.loopPointReached -= OnVideoEnded;
- }
- if (handle != null)
- {
- handle.Release();
- handle = null;
- }
- if (videoHandle != null)
- {
- videoHandle.Release();
- videoHandle = null;
- }
- if (_ui != null)
- {
- GameObject.Destroy(_ui);
- _ui = null;
- }
- StoryController.ShowLevelView(100001001);
- }
- }
- }
|