123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Video;
- using YooAsset;
- using GFGGame.Launcher;
- using System.Collections;
- using System.Collections.Generic;
- namespace GFGGame
- {
- public class LuckyBoxVideoView : SingletonMonoBase<LuckyBoxVideoView>
- {
- private const float VideoWidth = 1080f;
- private const float VideoHeight = 1920f;
- private GameObject _ui;
- private AssetOperationHandle handle;
- private RawFileOperationHandle videoHandle;
- private RawImage imgVideo;
- private Button BtnSkipVertical;
- private VideoPlayer videoPlayer;
- private List<ItemData> _rewardList;
- public void Show(List<ItemData> _reward = null)
- {
- MusicManager.Instance.Stop();
- FairyGUI.GRoot.inst.touchable = false;
- string path = ResPathUtil.GetUUIPrefabPath("UILuckyBox");
- 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.y = Screen.height;
- imgVideoSizeDelta.x = (VideoWidth / VideoHeight) * imgVideoSizeDelta.y;
- imgVideo.rectTransform.sizeDelta = imgVideoSizeDelta;
- BtnSkipVertical = _ui.transform.Find("BtnSkipVertical").GetComponent<Button>();
- BtnSkipVertical.onClick.AddListener(OnClickrectBtnSkip);
- BtnSkipVertical.gameObject.SetActive(false);
- videoPlayer = _ui.transform.Find("VideoPlayer").GetComponent<VideoPlayer>();
- videoPlayer.loopPointReached += OnVideoEnded;
- videoPlayer.prepareCompleted += OnVideoPrepared;
- string assetPath = ResPathUtil.GetVideoPath("normalLucky");
- videoHandle = YooAssets.LoadRawFileSync(assetPath);
- videoPlayer.url = videoHandle.GetRawFilePath();
- videoPlayer.Play();
- _rewardList = _reward;
- }
- private void OnVideoPrepared(VideoPlayer source)
- {
- videoPlayer.prepareCompleted -= OnVideoPrepared;
- StartCoroutine(FadeButtonOverTime(BtnSkipVertical));
- }
- IEnumerator FadeButtonOverTime(Button btn)
- {
- Image buttonImage = btn.GetComponent<Image>();
- //Image img = btn.GetComponentInChildren<Image>();
- yield return new WaitForSeconds((float)videoPlayer.length - 3f);
- if (buttonImage != null)
- {
- // 获取初始颜色
- Color startColor = buttonImage.color;
- //Color startColorText = img.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);
- //img.color = Color.Lerp(startColorText, targetColorText, t);
- yield return null; // 等待下一帧
- }
- // 设置最终颜色,确保透明度为 0
- buttonImage.color = targetColor;
- //img.color = targetColorText;
- }
- }
- private void OnVideoEnded(VideoPlayer source)
- {
- this.Hide();
- }
- private void OnClickrectBtnSkip()
- {
- Hide();
- }
- private void OnClickBtnFullScreen()
- {
- BtnSkipVertical.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()
- {
- StopAllCoroutines();
- videoPlayer.Stop();
- videoPlayer.loopPointReached -= OnVideoEnded;
- MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
- handle.Release();
- handle = null;
- videoHandle.Release();
- videoHandle = null;
- GameObject.Destroy(_ui);
- _ui = null;
- FairyGUI.GRoot.inst.touchable = true;
- ViewManager.Show<LuckyBoxBonusShowView>(_rewardList);
- }
- }
- }
|