zifa2003293 发表于 2021-7-16 14:10

Unity 加载文件夹下所有图片

// 储存获取到的图片       List<Texture2D> allTex2d = new List<Texture2D>();   /// <summary>   /// 读取StremingAssets文件夹指定的文件夹目录下的所有图片进行加载   /// </summary>   /// <param name="path"> StremingAssets文件夹下的文件夹名字 </param>   void Load(string path)   {         List<string> filePaths = new List<string>();         string[] dirs = null;         string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";         string[] ImageType = imgtype.Split('|');         for (int i = 0; i < ImageType.Length; i++)         {             //获取Application.dataPath文件夹下所有的图片路径               dirs = Directory.GetFiles((Application.streamingAssetsPath+"/" + path), ImageType);             for (int j = 0; j < dirs.Length; j++)             {               filePaths.Add(dirs);             }                   }          Debug.Log("图片数量:" + dirs.Length);          for (int i = 0; i < filePaths.Count; i++)         {             Texture2D tx = new Texture2D(100, 100);             tx.LoadImage(Tools.GetImageByte(filePaths));             ///获取图片名字,并去除.png 后缀             tx.name = filePaths.Substring(filePaths.LastIndexOf(@"\") + 1, filePaths.LastIndexOf('.')-filePaths.LastIndexOf('\\')-1);             allTex2d.Add(tx);             Debug.Log("Texture2D Name:" + tx.name);         }   }/// <summary>       /// 根据图片路径返回图片的字节流byte[]       /// </summary>       /// <param name="imagePath">图片路径</param>       /// <returns>返回的字节流</returns>       public static byte[] GetImageByte(string imagePath)   {         FileStream files = new FileStream(imagePath, FileMode.Open);         byte[] imgByte = new byte;         files.Read(imgByte, 0, imgByte.Length);         files.Close();         return imgByte;   }
页: [1]
查看完整版本: Unity 加载文件夹下所有图片