using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
/// <summary>
/// 获取StreamingAssets中的文件的名称
/// </summary>
public class GetStreamingAssetsName : MonoBehaviour {
private List<Texture> tex = new List<Texture>();
private List<string> strList = new List<string>();
public Texture[] texArray;
public string[] strArray;
// Use this for initialization
void Start () {
LoadPicturesOfStreamingAssets();
Debug.Log(Application.streamingAssetsPath);
}
// Update is called once per frame
void Update () {
/// 从StreamingAssets中读取图片
/// </summary>
void LoadPicturesOfStreamingAssets()
{
StartCoroutine(Load_WWW_AllPicture());
}
IEnumerator Load_WWW_AllPicture()
{
string streamingAssetsPath = Application.streamingAssetsPath; //所有的文件的路径
//string streamingAssetsPath = Application.streamingAssetsPath + "/" + "Textures"; //单个文件夹的文件的路径
DirectoryInfo dir = new DirectoryInfo(streamingAssetsPath); //初始化一个目录信息类的对象 //Directory:目录 info:信息
GetAllFiles(dir);
foreach(DictionaryEntry de in hashtable)
{
WWW www = new WWW("file://" + streamingAssetsPath + "/" + de.Key); //获取文件名称对应的文件
yield return www;
if (www != null)
{
tex.Add(www.texture);
}
if (www.isDone)
{
www.Dispose();
Debug.Log("找到一张图片");
}
}
Debug.Log("图片寻找完毕");
if (tex.Count != 0)
{
texArray = tex.ToArray();
}
if (strList.Count != 0)
{
strArray = strList.ToArray();
for (int i = 0; i < strArray.Length; i++)
{
Debug.Log("图片名称是:"+ strArray[i]);
}
}
}
Hashtable hashtable = new Hashtable();
void GetAllFiles(DirectoryInfo dir)
{
FileSystemInfo[] fileSystemInfo = dir.GetFileSystemInfos(); //初始化一个FileSystemInfo类型的实例
foreach(FileSystemInfo info in fileSystemInfo) //遍历 DirectoryInfo 下的所有内容
{
if(info is DirectoryInfo) //当在 DirectoryInfo 中存在 info 时
{
GetAllFiles((DirectoryInfo)info); //获取 info 下的所有文件
}
else
{
string str = info.FullName; //记录 info 的绝对路径
string path = Application.streamingAssetsPath;
//string path = Application.streamingAssetsPath + "/" + "Textures";
string strType = str.Substring(path.Length); //将文件的名称记录
//通过文件的名称的最后3位,辨别文件的类型
if(strType.Substring(strType.Length-3).ToLower()=="png"|| strType.Substring(strType.Length - 3).ToLower() == "jpg")
{
if (hashtable.Contains(strType)) //判断当前哈希表中是否存在这个值
{
hashtable[strType] = strType; //将该文件的名称作为键,存储在哈希表中
}
else
{
hashtable.Add(strType, strType); //如果不存在,添加这个键和值
//strList.Add((strType.Remove(0,1)).Remove((strType.Remove(0, 1)).Length-4,4)); //将图片名称添加到数组中
strList.Add(strType.Remove(strType.Length - 4, 4).Remove(0,1));
}
}
}
}
}
using System.Collections.Generic;
using UnityEngine;
using System.IO;
/// <summary>
/// 获取StreamingAssets中的文件的名称
/// </summary>
public class GetStreamingAssetsName : MonoBehaviour {
private List<Texture> tex = new List<Texture>();
private List<string> strList = new List<string>();
public Texture[] texArray;
public string[] strArray;
// Use this for initialization
void Start () {
LoadPicturesOfStreamingAssets();
Debug.Log(Application.streamingAssetsPath);
}
// Update is called once per frame
void Update () {
}
/// <summary>/// 从StreamingAssets中读取图片
/// </summary>
void LoadPicturesOfStreamingAssets()
{
StartCoroutine(Load_WWW_AllPicture());
}
IEnumerator Load_WWW_AllPicture()
{
string streamingAssetsPath = Application.streamingAssetsPath; //所有的文件的路径
//string streamingAssetsPath = Application.streamingAssetsPath + "/" + "Textures"; //单个文件夹的文件的路径
DirectoryInfo dir = new DirectoryInfo(streamingAssetsPath); //初始化一个目录信息类的对象 //Directory:目录 info:信息
GetAllFiles(dir);
foreach(DictionaryEntry de in hashtable)
{
WWW www = new WWW("file://" + streamingAssetsPath + "/" + de.Key); //获取文件名称对应的文件
yield return www;
if (www != null)
{
tex.Add(www.texture);
}
if (www.isDone)
{
www.Dispose();
Debug.Log("找到一张图片");
}
}
Debug.Log("图片寻找完毕");
if (tex.Count != 0)
{
texArray = tex.ToArray();
}
if (strList.Count != 0)
{
strArray = strList.ToArray();
for (int i = 0; i < strArray.Length; i++)
{
Debug.Log("图片名称是:"+ strArray[i]);
}
}
}
Hashtable hashtable = new Hashtable();
void GetAllFiles(DirectoryInfo dir)
{
FileSystemInfo[] fileSystemInfo = dir.GetFileSystemInfos(); //初始化一个FileSystemInfo类型的实例
foreach(FileSystemInfo info in fileSystemInfo) //遍历 DirectoryInfo 下的所有内容
{
if(info is DirectoryInfo) //当在 DirectoryInfo 中存在 info 时
{
GetAllFiles((DirectoryInfo)info); //获取 info 下的所有文件
}
else
{
string str = info.FullName; //记录 info 的绝对路径
string path = Application.streamingAssetsPath;
//string path = Application.streamingAssetsPath + "/" + "Textures";
string strType = str.Substring(path.Length); //将文件的名称记录
//通过文件的名称的最后3位,辨别文件的类型
if(strType.Substring(strType.Length-3).ToLower()=="png"|| strType.Substring(strType.Length - 3).ToLower() == "jpg")
{
if (hashtable.Contains(strType)) //判断当前哈希表中是否存在这个值
{
hashtable[strType] = strType; //将该文件的名称作为键,存储在哈希表中
}
else
{
hashtable.Add(strType, strType); //如果不存在,添加这个键和值
//strList.Add((strType.Remove(0,1)).Remove((strType.Remove(0, 1)).Length-4,4)); //将图片名称添加到数组中
strList.Add(strType.Remove(strType.Length - 4, 4).Remove(0,1));
}
}
}
}
}
}