- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
- 纳金币
- 53488
- 精华
- 316
|
图片应放在unity工程下的 StreamingAssets文件夹下
public UITexture uit;
void LoadPicturesOfStreamingAsset()
{
StartCoroutine(LoadWWWAllPicture());
}
List<Texture> ww = new List<Texture>();
IEnumerator LoadWWWAllPicture()
{
string streamingPath = Application.streamingAssetsPath;
DirectoryInfo dir = new DirectoryInfo(streamingPath);//初始化一个DirectoryInfo类的对象
GetAllFiles(dir);
double startTime = (double)Time.time;
foreach (DictionaryEntry de in ht)
{
WWW www = new WWW("file://" + streamingPath + "/" + de.Key);
yield return www;
if (www != null)
{
ww.Add(www.texture);
startTime = (double)Time.time - startTime;
}
if (www.isDone)
{
www.Dispose();
}
}
uit.mainTexture = ww[5];
uit.MakePixelPerfect();
Debug.Log("WWW use time: " + startTime + " pictures count: " + ww.Count);
}
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 str = i.FullName; //记录i的绝对路径
string path = Application.streamingAssetsPath;
string strType= str.Substring(path.Length);
if (strType.Substring(strType.Length - 3).ToLower() == "png")
{
if (ht.Contains(strType))
{
ht[strType] = strType;
}
else
{
ht.Add(strType, strType);
}
}
}
}
}
使用方法:调用LoadPicturesOfStreamingAsset();
|
|