使用WWW方式读取StreamingAsset目录的图片

来源:互联网 发布:荆门网络作文大赛 编辑:程序博客网 时间:2024/06/05 06:59
using System.Collections;using System.Collections.Generic;using System.IO;using UnityEngine;using UnityEngine.UI;public class UI_Main : MonoBehaviour {    private Hashtable ht = new Hashtable();    private Dictionary<string, Texture2D> dic = new Dictionary<string, Texture2D>();public GameObject Finish = null;void Start ()     {LoadPictures();Finish.GetComponent<Image>().sprite = Sprite.Create(dic[""], new Rect(0, 0, dic[""].width, dic[""].height), new Vector2(0.5f, 0.5f));}    void LoadPictures()    {        StartCoroutine(LoadImages());    }    IEnumerator LoadImages()    {        string streamingPath = Application.streamingAssetsPath;        DirectoryInfo dir = new DirectoryInfo(streamingPath);        GetAllFiles(dir);        foreach (DictionaryEntry dic in ht)        {            WWW www = new WWW("file://" + streamingPath + dic.Key);            yield return www;            if (www != null && string.IsNullOrEmpty(www.error))            {                ww.Add(dic.Key.ToString().Substring(1, dic.Key.ToString().Length - 5), www.texture);            }            if (www.isDone)            {                www.Dispose();            }        }    }    public void GetAllFiles(DirectoryInfo dir)    {        FileSystemInfo[] fil = dir.GetFileSystemInfos();        foreach (FileSystemInfo i in fil)        {            if (i is DirectoryInfo)            {                GetAllFiles((DirectoryInfo)i);            }            else            {                string str = i.FullName;                string path = Application.streamingAssetsPath;                string strType = str.Substring(path.Length);                if (strType.Substring(strType.Length - 3).ToLower() == "jpg")                {                    if (ht.Contains(strType))                    {                        ht[strType] = strType;                    }                    else                    {                        strType = strType.Replace("\\", "/");                        ht.Add(strType, strType);                    }                }            }        }    }}

原创粉丝点击