| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 | using UnityEngine;using UnityEngine.UI;using UnityEngine.Video;using YooAsset;using GFGGame.Launcher;using System.Collections;using System;namespace GFGGame{    public class UICGView : SingletonMonoBase<UICGView>    {        private const float VideoWidth = 1920f;        private const float VideoHeight = 1080f;        private GameObject _ui;        private AssetOperationHandle handle;        private RawFileOperationHandle videoHandle;        private RawImage imgVideo;        private Button btnFullScreen;        private VideoPlayer videoPlayer;        public void Show()        {            MusicManager.Instance.Stop();            string path = ResPathUtil.GetUUIPrefabPath("UICG");            handle = YooAssets.LoadAssetSync<GameObject>(path);            _ui = handle.InstantiateSync(UGUIManager.Instance.desktop.transform);            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 rectTransform = btnFullScreen.GetComponent<RectTransform>();            Vector2 buttonSizeDelta = rectTransform.sizeDelta;            Vector2 anchoredPosition = rectTransform.anchoredPosition;            anchoredPosition.y = -imgVideoSizeDelta.y / 2 - buttonSizeDelta.y;            rectTransform.anchoredPosition = anchoredPosition;            btnFullScreen.onClick.AddListener(OnClickBtnFullScreen);            videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();            videoPlayer.loopPointReached += OnVideoEnded;            videoPlayer.prepareCompleted += OnVideoPrepared;            string assetPath = ResPathUtil.GetVideoPath("cg");            videoHandle = YooAssets.LoadRawFileSync(assetPath);            videoPlayer.url = videoHandle.GetRawFilePath();            videoPlayer.Play();                    }        private void OnVideoPrepared(VideoPlayer source)        {            videoPlayer.prepareCompleted -= OnVideoPrepared;            StartCoroutine(FadeButtonOverTime());        }        IEnumerator FadeButtonOverTime()        {            Image buttonImage = btnFullScreen.GetComponent<Image>();            Text buttonText = btnFullScreen.GetComponentInChildren<Text>();            yield return new WaitForSeconds((float)videoPlayer.length - 3f);            if (buttonImage != null)            {                // 获取初始颜色                Color startColor = buttonImage.color;                Color startColorText = buttonText.color;                // 目标颜色,透明度设为 0                Color targetColor = new Color(startColor.r, startColor.g, startColor.b, 0f);                Color targetColorText = new Color(startColorText.r, startColorText.g, startColorText.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);                    buttonText.color = Color.Lerp(startColorText, targetColorText, t);                    yield return null; // 等待下一帧                }                // 设置最终颜色,确保透明度为 0                buttonImage.color = targetColor;                buttonText.color = targetColorText;            }        }        private void OnVideoEnded(VideoPlayer source)        {            videoPlayer.loopPointReached -= OnVideoEnded;            this.Hide();            StoryController.ShowLevelView(100001001);        }        private void OnClickBtnFullScreen()        {            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();            }        }        public void Hide()        {            handle.Release();            handle = null;            videoHandle.Release();            videoHandle = null;            GameObject.Destroy(_ui);            _ui = null;        }    }}
 |