读取StreamingAssets文件夹里面所有的图片

来源:互联网 发布:java socket epoll 编辑:程序博客网 时间:2024/05/17 21:57

读取StreamingAssets文件夹里面所有的图片(包括子目录中的)

图片应放在unity工程下的 StreamingAssets文件夹下
void LoadPicturesOfStreamingAsset()
    {
        StartCoroutine(LoadWWWAllPicture());
    }


    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();
0 0
原创粉丝点击