获取StreamingAssets中的文件的名称

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 () {

}

    /// <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));
                    }
                }
            }
        }
    }

}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值