46 lines
1.7 KiB
C#
46 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
using RenderHeads.Media.AVProVideo.Demos;
|
|
|
|
public class VideoListManager : MonoBehaviour
|
|
{
|
|
public string folderPath; // 视频文件夹路径
|
|
public GameObject buttonPrefab; // 用于显示视频文件的按钮预制体
|
|
public Transform buttonContainer; // 存放按钮的容器
|
|
public VCR vCR;
|
|
void Start()
|
|
{
|
|
|
|
folderPath = Application.streamingAssetsPath + "/Captures";
|
|
string[] videoFiles = Directory.GetFiles(folderPath, "*.mp4"); // 获取指定文件夹内的所有 mp4 文件
|
|
string[] _videoFiles = new string[videoFiles.Length];
|
|
int _num = 0;
|
|
foreach (string filePath in videoFiles)
|
|
{
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath); // 获取文件名(不带扩展名)
|
|
|
|
GameObject button = Instantiate(buttonPrefab, buttonContainer); // 实例化按钮
|
|
button.GetComponentInChildren<Text>().text = fileName; // 设置按钮文本为文件名
|
|
_videoFiles[_num] = fileName + ".mp4";
|
|
_num++;
|
|
Button videoButton = button.GetComponent<Button>();
|
|
videoButton.onClick.AddListener(() => PlayVideo(filePath)); // 添加按钮点击事件,调用 PlayVideo 方法
|
|
}
|
|
|
|
vCR._videoFiles = _videoFiles;
|
|
|
|
}
|
|
|
|
void PlayVideo(string filePath)
|
|
{
|
|
VideoPlayer videoPlayer = FindObjectOfType<VideoPlayer>(); // 找到场景中的 VideoPlayer 组件
|
|
videoPlayer.Stop(); // 停止当前正在播放的视频
|
|
videoPlayer.url = filePath; // 设置要播放的视频路径
|
|
videoPlayer.Play(); // 播放视频
|
|
}
|
|
}
|