一个可以读取文件夹中所有图片的代码
本帖最后由 大西几 于 2020-8-31 17:38 编辑using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
//using DG.Tweening;
using UnityEngine.SceneManagement;
using System.Collections;
public class LoadAllPic : MonoBehaviour
{
//public RectTransform fa;
//public GameObject father;
//public GameObject StoreObj;
// 储存获取到的图片
publicList<Texture2D> allpic = new List<Texture2D>();
List<string> MingZi = new List<string>();
publicstatic List<Sprite> loadsprites;
// Use this for initialization
void Start()
{
load();
//for (int i = 0; i < allTex2d.Count; i++)
//{
// //GameObject temp = Instantiate(StoreObj, Vector3.zero, Quaternion.identity);
// //temp.GetComponent<Transform>().SetParent(StoreObj.transform.parent);
// Sprite sprite = Sprite.Create(allTex2d, new Rect(0, 0, allTex2d.width, allTex2d.height), new Vector2(0.5f, 0.5f));
// //temp.transform.Find("pic").GetComponent<Image>().sprite = sprite;
// //temp.AddComponent<Button>.();
// //temp.transform.name = i.ToString();
// //temp.transform.Find("Text").GetComponent<Text>().text = MingZi;
// //fa.GetComponent<RectTransform>().sizeDelta = new Vector2(allTex2d.Count * 750, 150);
//}
////StoreObj.SetActive(false);
}
void load()
{
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.streamingAssetsPath + "/" + "资料库", ImageType);
DirectoryInfo dir = new DirectoryInfo(Application.streamingAssetsPath + "/" + "资料库");//初始化一个DirectoryInfo类的对象
GetAllFiles(dir);
for (int j = 0; j < dirs.Length; j++)
{
filePaths.Add(dirs);
}
}
for (int i = 0; i < filePaths.Count; i++)
{
Texture2D tx = new Texture2D(100, 100);
tx.LoadImage(getImageByte(filePaths));
allpic.Add(tx);
Sprite sprite = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), Vector2.zero);
loadsprites.Add(sprite);
}
}
/// <summary>
/// 根据图片路径返回图片的字节流byte[]
/// /// </summary>
/// /// <param name="imagePath">图片路径</param>
/// /// <returns>返回的字节流</returns>
private static byte[] getImageByte(string imagePath)
{
FileStream files = new FileStream(imagePath, FileMode.Open);
byte[] imgByte = new byte;
files.Read(imgByte, 0, imgByte.Length);
files.Close();
return imgByte;
}
Hashtable ht = new Hashtable();
public void GetAllFiles(DirectoryInfo dir)
{
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //初始化一个FileSystemInfo类型的实例
foreach (FileSystemInfo i in fileinfo) //循环遍历fileinfo下的所有内容
{
if (i is DirectoryInfo) //当在DirectoryInfo中存在i时
{
GetAllFiles((DirectoryInfo)i);//获取i下的所有文件
}
else
{
//提取字符串中的数字
string name = i.Name;
if (name.Contains(".meta"))
name = i.Name.Replace(".meta", "");
if (name.Contains(".png"))
name = i.Name.Replace(".png", "");
else if (name.Contains(".jpg"))
name = i.Name.Replace(".jpg", "");
if (name != "Thumbs.db")
MingZi.Add(name);
string str = i.FullName; //记录i的绝对路径
string path = Application.streamingAssetsPath + "/" + "资料库";
string strType = str.Substring(path.Length);
if (strType.Substring(strType.Length - 3).ToLower() == "jpg")
{
if (ht.Contains(strType))
{
ht = strType;
}
else
{
ht.Add(strType, strType);
}
}
else if (strType.Substring(strType.Length - 3).ToLower() == "png")
{
if (ht.Contains(strType))
{
ht = strType;
}
else
{
ht.Add(strType, strType);
}
}
}
}
}
}
感谢楼主分享~ 感谢楼主分享~
页:
[1]