[Unity] 一个可以读取文件夹中所有图片的代码

查看:771 |回复:2 | 2020-6-16 13:59:52

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 大西几 于 2020-8-31 17:38 编辑

using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
//using DG.Tweening;
using UnityEngine.SceneManagement;
using System.Collections;


public class LoadAllPic : MonoBehaviour
{


    //public RectTransform fa;
    //public GameObject father;
    //public GameObject StoreObj;
    // 储存获取到的图片  
    public  List<Texture2D> allpic = new List<Texture2D>();
     List<string> MingZi = new List<string>();
    public  static List<Sprite> loadsprites;
    // Use this for initialization     
    void Start()
    {
        load();


      


        //for (int i = 0; i < allTex2d.Count; i++)
        //{
        //    //GameObject temp = Instantiate(StoreObj, Vector3.zero, Quaternion.identity);
        //    //temp.GetComponent<Transform>().SetParent(StoreObj.transform.parent);
        //    Sprite sprite = Sprite.Create(allTex2d, new Rect(0, 0, allTex2d.width, allTex2d.height), new Vector2(0.5f, 0.5f));
        //    //temp.transform.Find("pic").GetComponent<Image>().sprite = sprite;
        //    //temp.AddComponent<Button>.();
        //    //temp.transform.name = i.ToString();
        //    //temp.transform.Find("Text").GetComponent<Text>().text = MingZi[i * 2];
        //    //fa.GetComponent<RectTransform>().sizeDelta = new Vector2(allTex2d.Count * 750, 150);
        //}
        ////StoreObj.SetActive(false);
    }
    void load()
    {   
        
        List<string> filePaths = new List<string>();
        string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
        string[] ImageType = imgtype.Split('|');
        for (int i = 0; i < ImageType.Length; i++)
        {
            //获取Application.dataPath文件夹下所有的图片路径   
            string[] dirs = Directory.GetFiles(Application.streamingAssetsPath + "/" + "资料库", ImageType);
            DirectoryInfo dir = new DirectoryInfo(Application.streamingAssetsPath + "/" + "资料库");//初始化一个DirectoryInfo类的对象
            GetAllFiles(dir);
           


            for (int j = 0; j < dirs.Length; j++)
            {
                filePaths.Add(dirs[j]);
            }
        }
        for (int i = 0; i < filePaths.Count; i++)
        {
            Texture2D tx = new Texture2D(100, 100);
            tx.LoadImage(getImageByte(filePaths));
            allpic.Add(tx);
            Sprite sprite = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), Vector2.zero);
            loadsprites.Add(sprite);


        }
    }
    /// <summary>      
    /// 根据图片路径返回图片的字节流byte[]     
    /// /// </summary>      
    /// /// <param name="imagePath">图片路径</param>      
    /// /// <returns>返回的字节流</returns>     
    private static byte[] getImageByte(string imagePath)
    {
        FileStream files = new FileStream(imagePath, FileMode.Open);
        byte[] imgByte = new byte[files.Length];
        files.Read(imgByte, 0, imgByte.Length);
        files.Close();
        return imgByte;
    }
    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 name = i.Name;
                if (name.Contains(".meta"))
                    name = i.Name.Replace(".meta", "");


                if (name.Contains(".png"))
                    name = i.Name.Replace(".png", "");
                else if (name.Contains(".jpg"))
                    name = i.Name.Replace(".jpg", "");


                if (name != "Thumbs.db")
                    MingZi.Add(name);


                string str = i.FullName;        //记录i的绝对路径
                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
                    {
                        ht.Add(strType, strType);
                    }
                }
                else if (strType.Substring(strType.Length - 3).ToLower() == "png")
                {
                    if (ht.Contains(strType))
                    {
                        ht[strType] = strType;
                    }
                    else
                    {
                        ht.Add(strType, strType);
                    }
                }
            }
        }
    }
}


image.png
2020-6-16 13:59:52  
 赞 赞 0

使用道具 登录

2个回答,把该问题分享到群,邀请大神一起回答。
2#
感谢楼主分享~
回复 收起回复
2021-2-5 08:26:26   回复
 赞 赞 0

使用道具 登录

3#
感谢楼主分享~
回复 收起回复
2021-2-5 08:26:37   回复
 赞 赞 0

使用道具 登录

CG 游戏行业专业问题

手机游戏引擎手游引擎Unity3D技术
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表