Files
Main/Assets/Launcher/ExternalLibs/NGUI/Scripts/UI/UIVideoTexture.cs
2025-01-25 04:38:09 +08:00

278 lines
6.3 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.Video;
public class UIVideoTexture : MonoBehaviour
{
//视频状态
private enum VideoPlayState
{
None,
Playing,
Pause,
Stop
}
//当前视频状态
private VideoPlayState _videoPlayState = VideoPlayState.None;
//UITexture 组件
private UITexture _uiTexture = null;
//VideoPlayer 组件
private VideoPlayer _videoPlayer = null;
//声音源
private AudioSource _audioSource = null;
//RenderTexture
private RenderTexture _rTex = null;
//是否初始化
public bool _isInit = false;
//是否循环
private bool _isLooping = false;
//自动播放
private bool _playOnAwake = false;
//总帧数
private int _frameCount = 0;
//帧率
private float _frameRate = 0;
//开始播放时间
private float _beginTime = 0;
public VideoPlayer VideoPlayer
{
get
{
return _videoPlayer;
}
}
public UITexture UITexture
{
get
{
return _uiTexture;
}
}
//是否循环
public bool IsLooping
{
get
{
return _isLooping;
}
set
{
if (_videoPlayer != null)
{
_videoPlayer.isLooping = value;
}
_isLooping = value;
}
}
//自动播放
public bool PlayOnAwake
{
get
{
return _playOnAwake;
}
set
{
if (_videoPlayer != null)
{
_videoPlayer.playOnAwake = value;
}
_playOnAwake = value;
}
}
//当前帧
public int Frame
{
get
{
if (VideoPlayer != null)
{
return (int)VideoPlayer.frame;
}
return 0;
}
}
//总帧数
public int FrameCount
{
get
{
return _frameCount;
}
}
//帧率
public float FrameRate
{
get
{
return _frameRate;
}
}
public void Init()
{
if (_isInit)
return;
_uiTexture = GetComponent<UITexture>();
if (_uiTexture == null)
{
_uiTexture = gameObject.AddComponent<UITexture>();
}
_videoPlayer = GetComponent<VideoPlayer>();
if (_videoPlayer == null)
{
_videoPlayer = gameObject.AddComponent<VideoPlayer>();
}
_audioSource = GetComponent<AudioSource>();
if (_audioSource == null)
{
_audioSource = gameObject.AddComponent<AudioSource>();
}
//要置空,不置空可能会没声音
_videoPlayer.clip = null;
_videoPlayer.renderMode = VideoRenderMode.RenderTexture;
_videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
_videoPlayer.playOnAwake = _playOnAwake;
_videoPlayer.isLooping = _isLooping;
_videoPlayer.SetTargetAudioSource(0, _audioSource);
_videoPlayer.controlledAudioTrackCount = 1;
_isInit = true;
}
public void Play(string url, float time = 0)
{
Init();
if(url != _videoPlayer.url)
{
_videoPlayer.source = VideoSource.Url;
_videoPlayer.url = url;
}
Play(time);
}
//播放
public void Play(VideoClip videoClip, float time = 0)
{
Init();
if (videoClip != _videoPlayer.clip)
{
_videoPlayer.source = VideoSource.VideoClip;
_videoPlayer.clip = videoClip;
}
Play(time);
}
//播放
public void Play()
{
_videoPlayState = VideoPlayState.Playing;
if (_videoPlayer != null)
{
if (_videoPlayer.isPlaying)
{
_videoPlayer.time = _beginTime;
}
else if (_videoPlayer.isPrepared)
{
_videoPlayer.time = _beginTime;
_videoPlayer.Play();
}
else
{
_videoPlayer.Prepare();
StartCoroutine(PlayVideo());
}
}
}
//播放
public void Play(float time)
{
Goto(time);
Play();
}
private IEnumerator PlayVideo()
{
while (!_videoPlayer.isPrepared)
{
yield return new WaitForEndOfFrame();
}
_frameCount = (int)_videoPlayer.frameCount;
_frameRate = _videoPlayer.frameRate;
if (_rTex == null || _rTex.width != _uiTexture.width || _rTex.height != _uiTexture.height)
{
if (_rTex != null)
RenderTexture.ReleaseTemporary(_rTex);
_rTex = RenderTexture.GetTemporary((int)_videoPlayer.clip.width, (int)_videoPlayer.clip.height, 0, RenderTextureFormat.ARGB32);
_rTex.name = "RenderTexture7";
_rTex.antiAliasing = 1;
_uiTexture.mainTexture = _rTex;
_videoPlayer.targetTexture = _rTex;
}
//准备完成
if (_videoPlayState == VideoPlayState.Playing)
{
_videoPlayer.time = _beginTime;
_videoPlayer.Play();
}
}
//调到xx时间播放
public void Goto(float time)
{
int targetFram = (int)(_frameRate * time);
if (targetFram <= _frameCount)
{
_beginTime = time;
}
else
{
_beginTime = (float)_frameCount / _frameRate;
}
}
//停止
public bool Stop()
{
_videoPlayState = VideoPlayState.Stop;
if (_videoPlayer != null && _videoPlayer.isPlaying)
{
_videoPlayer.Stop();
return true;
}
return false;
}
//暂停
public bool Pause()
{
_videoPlayState = VideoPlayState.Pause;
if (_videoPlayer != null && _videoPlayer.isPlaying)
{
_videoPlayer.Pause();
return true;
}
return false;
}
//重播
public bool Replay()
{
_videoPlayState = VideoPlayState.Playing;
if (_videoPlayer != null)
{
_videoPlayer.Stop();
Play(0);
return true;
}
return false;
}
}