unity 获取外部某个文件夹下的所有图片

// 储存获取到的图片
	List<Texture2D> allTex2d = new List<Texture2D> ();
	// Use this for initialization
	void Start ()
	{
		load ();
	}
 
	void OnGUI ()
	{
		if (allTex2d.Count != 0) {
			// 把加载的图片显示出来
			for (int i = 0; i < allTex2d.Count; i++) {
				GUILayout.Button (allTex2d [i]);
			}
		}
	}
 
	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++) {
			//获取d盘中a文件夹下所有的图片路径
			string[] dirs = Directory.GetFiles (@"d:\\a", ImageType [i]);
			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 [i]));
			allTex2d.Add (tx);
		}
	}
	
	/// 
	/// 根据图片路径返回图片的字节流byte[]
	/// 
	/// 图片路径
	/// 返回的字节流
	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;
	}

你可能感兴趣的:(Unity3D,C#)