```csharp
VideoPlayer 播放视频
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.Video;
using UnityEngine.UI;
using TMPro;
using UIWidgets;
public class VideoPlayerControl : MonoBehaviour
{
public RawImage m_image;
public VideoPlayer m_vPlayer;
public Button m_btnPlay;
public Button m_btnPause;
public Button m_btnVolumnOn;
public Button m_btnVolumnOff;
public Slider m_sliderVideo;
public Slider m_sliderSource;
public Text m_textTime;
public TextMeshProUGUI m_videoName;
public Text m_textCount;
public AudioSource m_source;
public GameObject m_obj;
public bool m_isShow;
public float m_numBer = 20f;
private int hour, mint;
private float m_time;
private float m_timeCount;
private float m_timeCurrent;
private bool m_isVideo;
bool isFullScreen = false;
public GameObject m_videoControlArea;
protected bool m_isShowUI;
[SerializeField]
public TreeView m_Tree;
protected Vector2 m_videoAreaSize;
protected Vector3 m_videoAreaAnchor;
public GameObject m_fullScreenBtn;
public GameObject m_unFullScreenBtn;
void Awake()
{
GetVideoName();
}
void Start()
{
m_image = m_obj.GetComponent<RawImage>();
m_vPlayer = m_obj.AddComponent<VideoPlayer>();
m_source = m_obj.AddComponent<AudioSource>();
m_vPlayer.playOnAwake = false;
m_source.playOnAwake = false;
m_source.Pause();
m_videoControlArea.SetActive(false);
m_btnPlay.onClick.AddListener(delegate { OnClick(0); });
m_btnPause.onClick.AddListener(delegate { OnClick(1); });
m_btnVolumnOn.onClick.AddListener(delegate { OnClick(2); });
m_btnVolumnOff.onClick.AddListener(delegate { OnClick(3); });
m_sliderSource.value = m_source.volume;
m_sliderSource.onValueChanged.AddListener(delegate { ChangeSource(m_sliderSource.value); });
m_videoAreaSize = m_obj.GetComponent<RectTransform>().sizeDelta;
m_videoAreaAnchor = m_obj.GetComponent<RectTransform>().anchoredPosition3D;
}
void Update()
{
if (m_vPlayer.isPlaying && m_isShow)
{
m_image.texture = m_vPlayer.texture;
m_sliderVideo.maxValue = (m_vPlayer.frameCount / m_vPlayer.frameRate);
m_time = m_sliderVideo.maxValue;
hour = (int)m_time / 60;
mint = (int)m_time % 60;
m_textCount.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
m_isShow = !m_isShow;
}
if (Mathf.Abs((int)m_vPlayer.time - (int)m_sliderVideo.maxValue) == 0)
{
m_vPlayer.frame = (long)m_vPlayer.frameCount;
m_vPlayer.Pause();
m_isVideo = false;
return;
}
else if (m_isVideo && m_vPlayer.isPlaying)
{
m_time = (float)m_vPlayer.time;
hour = (int)m_time / 60;
mint = (int)m_time % 60;
m_textTime.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
m_sliderVideo.value = m_time;
}
}
private void GetVideoName()
{
string path;
Configuration cfg = ConfigManager.GetConfig("AppConfig.xml");
cfg.Get("VideoPath", out path);
if (!Directory.Exists(path))
{
Debug.LogError("教学视频路径不存在!");
return;
}
DirectoryInfo dir = new DirectoryInfo(path);
m_Tree.Nodes = InitTreeList(dir);
m_Tree.gameObject.transform.Find("ScrollRect").GetComponent<ScrollRect>().horizontal = true;
}
private ObservableList<TreeNode<TreeViewItem>> InitTreeList(DirectoryInfo dir)
{
ObservableList<TreeNode<TreeViewItem>> nodes = new ObservableList<TreeNode<TreeViewItem>>();
FileInfo[] files = dir.GetFiles();
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i];
TreeViewItem item = new CustomTreeViewItem(file.Name, null,file.FullName);
TreeNode<TreeViewItem> node = new TreeNode<TreeViewItem>(item, null);
nodes.Add(node);
}
DirectoryInfo[] childDirList = dir.GetDirectories();
for (int i = 0; i < childDirList.Length; i++)
{
DirectoryInfo childDir = childDirList[i];
ObservableList<TreeNode<TreeViewItem>> childDirNodes = InitTreeList(childDir);
TreeViewItem item = new CustomTreeViewItem(childDir.Name, null, null);
TreeNode<TreeViewItem> childNode = new TreeNode<TreeViewItem>(item, childDirNodes);
nodes.Add(childNode);
}
return nodes;
}
public void PlayVideo(string fullName)
{
Init(fullName);
}
public void OnSelect(int index, ListViewItem item)
{
TreeViewComponent componentItem = item as TreeViewComponent;
CustomTreeViewItem customItem = componentItem.Item as CustomTreeViewItem;
if (customItem.Data != null)
{
Init(customItem.Data);
m_videoName.text = customItem.Name;
}
}
private void Init(string url)
{
m_isVideo = true;
m_isShow = true;
m_timeCount = 0;
m_timeCurrent = 0;
m_sliderVideo.value = 0;
m_vPlayer.source = VideoSource.Url;
m_vPlayer.url = url;
m_vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
m_vPlayer.SetTargetAudioSource(0, m_source);
m_vPlayer.controlledAudioTrackCount = 1;
m_vPlayer.IsAudioTrackEnabled(0);
m_vPlayer.prepareCompleted += Prepared;
m_vPlayer.Prepare();
}
public void ChangeSource(float value)
{
m_source.volume = value;
}
public void ChangeVideo()
{
if (m_vPlayer.isPrepared)
{
m_vPlayer.time = (long)m_sliderVideo.value;
m_time = (float)m_vPlayer.time;
hour = (int)m_time / 60;
mint = (int)m_time % 60;
m_textTime.text = string.Format("{0:D2}:{1:D2}", hour.ToString(), mint.ToString());
}
if (m_isVideo == false)
{
m_isVideo = true;
}
m_vPlayer.Play();
}
private void OnClick(int num)
{
switch (num)
{
case 0:
m_vPlayer.Play();
m_btnPlay.gameObject.SetActive(false);
m_btnPause.gameObject.SetActive(true);
Time.timeScale = 1;
break;
case 1:
m_vPlayer.Pause();
m_btnPlay.gameObject.SetActive(true);
m_btnPause.gameObject.SetActive(false);
Time.timeScale = 0;
break;
case 2:
m_source.volume = 0;
m_btnVolumnOn.gameObject.SetActive(false);
m_btnVolumnOff.gameObject.SetActive(true);
break;
case 3:
m_source.volume = m_sliderSource.value;
m_btnVolumnOn.gameObject.SetActive(true);
m_btnVolumnOff.gameObject.SetActive(false);
break;
default:
break;
}
}
void Prepared(VideoPlayer player)
{
player.Play();
}
public void OnControllAreaPointEnter()
{
if (!m_isShowUI)
{
m_videoControlArea.SetActive(true);
m_isShowUI = true;
}
}
public void OnControllAreaPointExit()
{
if (m_isShowUI)
{
m_videoControlArea.SetActive(false);
m_isShowUI = false;
}
}
public void OnFullScreenBtnClick()
{
if (!isFullScreen && m_vPlayer.isPrepared)
{
var rt = m_image.GetComponent<RectTransform>();
RectTransform fullScreen = GameObject.Find("MainUI").GetComponent<RectTransform>();
rt.localScale = new Vector3(fullScreen.rect.width / rt.rect.width, fullScreen.rect.height / rt.rect.height, 0);
rt.position = fullScreen.position;
isFullScreen = true;
m_fullScreenBtn.SetActive(false);
m_unFullScreenBtn.SetActive(true);
}
else
{
m_image.GetComponent<RectTransform>().localScale = Vector3.one;
m_image.GetComponent<RectTransform>().anchoredPosition3D = m_videoAreaAnchor;
isFullScreen = false;
m_fullScreenBtn.SetActive(true);
m_unFullScreenBtn.SetActive(false);
}
}
public void OnSelectTab(int id)
{
if (id == 4)
{
if (m_Tree.SelectedNode != null)
{
m_Tree.DeselectNode(m_Tree.SelectedNode);
}
}
}
}