Files
plugin-library/Assets/00.StaryEvoTools/Runtime/VideoSystemPanel/VideoPanel.cs
stary d6710e491a
All checks were successful
Plugin Library CI / publish (00.BuildOriginality) (push) Successful in 6s
Plugin Library CI / publish (00.StaryEvo) (push) Successful in 10s
Plugin Library CI / publish (00.StaryEvoTools) (push) Successful in 24s
Plugin Library CI / publish (01.HybridCLR) (push) Successful in 7s
Plugin Library CI / publish (02.InformationSave) (push) Successful in 4s
Plugin Library CI / publish (03.YooAsset) (push) Successful in 40s
Plugin Library CI / publish (04.AudioCore) (push) Successful in 4s
Plugin Library CI / publish (05.TableTextConversion) (push) Successful in 5s
Plugin Library CI / publish (06.UIFarme) (push) Successful in 17s
Plugin Library CI / publish (07.RKTools) (push) Successful in 3s
Plugin Library CI / publish (08.UniTask) (push) Successful in 3s
Plugin Library CI / publish (09.CodeChecker) (push) Successful in 20s
Plugin Library CI / publish (10.StoryEditor) (push) Successful in 4s
Plugin Library CI / publish (10.XNode) (push) Successful in 4s
Plugin Library CI / publish (11.PointCloudTools) (push) Successful in 3s
Plugin Library CI / publish (12.WeixinMinigame) (push) Successful in 1m6s
111
2026-05-15 17:42:02 +08:00

195 lines
5.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Stary.Evo;
using Stary.Evo.AudioCore;
using Stary.Evo.UIFarme;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using YooAsset;
public class VideoPanel : BasePanel
{
private Transform area;
private RawImage intro;
private GameObject prospect;
private GameObject bg;
private Animator _animator;
private VideoPlayer videoPlayer;
private AudioSource audioSource;
private RenderTexture renderTexture;
public VideoPanel()
{
}
public override string UIPath => "";
public override UITweenType TweenType => UITweenType.Fade;
public override void Initialize(GameObject panelGo)
{
base.Initialize(panelGo);
area = activePanel.transform.Find("Area");
intro = area.Find("Intro").GetComponent<RawImage>();
prospect = area.Find("prospect").gameObject;
bg = area.Find("bg").gameObject;
videoPlayer = area.GetComponentInChildren<VideoPlayer>();
audioSource = area.GetComponentInChildren<AudioSource>();
_animator = area.GetComponentInChildren<Animator>();
}
public override void OnEnter(Action complete = null)
{
base.OnEnter(complete);
this.RegisterEvent<ModeType, VideoInfo>(ModeType.VideoStart, OnStartMove);
this.RegisterEvent<ModeType>(ModeType.VideoEnd, OnStopMove);
}
public override void OnExit(float delay = 0)
{
base.OnExit(delay);
this.UnRegisterEvent<ModeType, VideoInfo>(ModeType.VideoStart, OnStartMove);
this.UnRegisterEvent<ModeType>(ModeType.VideoEnd, OnStopMove);
}
private void OnStartMove(VideoInfo info)
{
if (info.filename.Equals(""))
{
Debug.LogError("UnityEvo视频文件名为空,必须传文件名才能执行");
return;
}
if (info.position != null)
{
activePanel.transform.position = info.position.SetVector3Ctor();
}
if (info.rotation != null)
{
activePanel.transform.rotation = Quaternion.Euler(info.rotation.SetVector3Ctor());
}
if (info.scale != null)
{
activePanel.transform.DOScale(info.scale.SetVector3Ctor(), 0.5f);
}
OnStartMove(info.filename, info.videoframeName, info.callback, info.isFixedSize);
}
private async void OnStartMove(string fileName, string videoframeName, Action callback, bool isFixedSize)
{
if (isFixedSize)
{
_animator.gameObject.SetActive(false);
bg.SetActive(false);
prospect.SetActive(false);
}
else
{
_animator.gameObject.SetActive(true);
bg.SetActive(true);
prospect.SetActive(true);
}
var mainpackage = YooAssets.TryGetPackage("Main");
var videoframeHandle = mainpackage.LoadAssetAsync<Sprite>(videoframeName);
await videoframeHandle.Task;
bg.GetComponent<Image>().sprite = videoframeHandle.GetAssetObject<Sprite>();
//新增renderTexture创建
if (videoPlayer.isPlaying)
{
videoPlayer.Stop();
//释放renderTexture
if (renderTexture != null)
{
renderTexture.Release();
renderTexture = null;
}
}
var videHandle = YooAssets.LoadAssetAsync<VideoClip>(fileName);
await videHandle.Task;
Debug.Log("UnityEvo开始播放视频:" + fileName);
VideoClip videoClip = videHandle.GetAssetObject<VideoClip>();
renderTexture = new RenderTexture((int)videoClip.width, (int)videoClip.height, 24);
videoPlayer.targetTexture = renderTexture;
intro.texture = renderTexture;
intro.SetNativeSize();
var musicHandle = YooAssets.LoadAssetAsync<AudioClip>(fileName + "_mp3");
await musicHandle.Task;
videoPlayer.source = VideoSource.VideoClip;
videoPlayer.clip = videoClip;
Debug.Log("UnityEvo开始播放视频的音频:" + fileName + "_mp3");
AudioClip audioClip = musicHandle.GetAssetObject<AudioClip>();
audioSource.clip = audioClip;
//等待视频加载完成
videoPlayer.Prepare();
videoPlayer.prepareCompleted += (source) =>
{
_animator.Play("vid_maskAni", 0, 0);
//预加载
if (videoPlayer.isPrepared)
{
videoPlayer.Play();
audioSource.Play();
}
};
videoPlayer.loopPointReached += (source) =>
{
OnStopMove();
callback?.Invoke();
};
videoPlayer.errorReceived += (source, error) => { Debug.LogError($"视频播放失败:{error}"); };
}
private void OnStopMove()
{
Debug.Log("UnityEvo视频播放完成");
AudioCoreManager.SetMusicVolume(2f, 1f);
_animator.Play("vid_maskDefault", 0, 0);
if (renderTexture != null)
{
renderTexture.Release();
renderTexture = null;
}
if (audioSource.isPlaying)
{
audioSource.Stop();
}
PanelSystem.PopQueue<VideoPanel>();
}
public struct VideoInfo
{
public string filename;
public string videoframeName;
public Vector3Ctor position;
public Vector3Ctor rotation;
public Vector3Ctor scale;
public Action callback;
public bool isFixedSize;
}
}