1. 程式人生 > 其它 >Unity 獲取本地資料夾下所有視訊和圖片

Unity 獲取本地資料夾下所有視訊和圖片

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class Party_Building : MonoBehaviour
{
    [Header ("儲存視訊檔案地址的列表")]
    public List<string> listVideoPath = new List<string>();

    //string path = "\\黨建視訊\\";
    
//exe打包檔案的路徑 string configPath = System.Environment.CurrentDirectory+ @"\DJmp4"; //視訊資料夾 string ceshimp4 = "E:\\unity.Demo\\Intelligence_Community(zihuixiaoqu)\\智慧小區Demo\\DJmp4"; public VideoPlayer VideoPlayer; // Start is called before the first frame update void Start() { GetFilesAllMp4(ceshimp4); VideoPlayer.url
= listVideoPath[0]; } //獲取本地資料夾裡所有視訊檔案 public void GetFilesAllMp4(string path_) { if (Directory.Exists (path_)) { DirectoryInfo direction = new DirectoryInfo(path_); FileInfo[] files = direction.GetFiles("*"); Debug.Log("視訊數量" + files.Length);
for (int i = 0; i < files.Length; i++) { //忽略關聯檔案 if (files[i].Name.EndsWith(".meta")) { continue; } //Debug.Log("檔名:" + files[i].Name); Debug.Log("檔案絕對路徑:" + files[i].FullName); listVideoPath.Add(files[i].FullName); //Debug.Log("檔案所在目錄:" + files[i].DirectoryName); } } else { return; } } private List<Texture2D> images = new List<Texture2D>(); private List<Sprite> imageSprite = new List<Sprite>(); //獲取資料夾下所有圖片 public void GetFilesAllSprite(string path_) { List<string> filePaths = new List<string>(); string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG"; string[] ImageType = imgtype.Split('|'); for (int i = 0; i < ImageType.Length; i++) { //獲取Application.dataPath資料夾下所有的圖片路徑 string[] dirs = Directory.GetFiles((Application.dataPath + path_), ImageType[i]); for (int j = 0; j < dirs.Length; j++) { filePaths.Add(dirs[j]); } } for (int i = 0; i < filePaths.Count; i++) { Texture2D tx = new Texture2D(100, 100); tx.LoadImage(GetImageByte(filePaths[i])); //轉化成sprite新增到列表使用 imageSprite.Add(ChangeToSprite(tx)); //轉化成Texture2D新增到列表使用 images.Add(tx); } } //返回圖片的位元組流 private static byte[] GetImageByte(string imagePath) { FileStream files = new FileStream(imagePath, FileMode.Open); byte[] imgByte = new byte[files.Length]; files.Read(imgByte, 0, imgByte.Length); files.Close(); return imgByte; } private Sprite ChangeToSprite(Texture2D tex) { Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); return sprite; } }