直接加载非Readable的Texture,是不能访问其像素数据的:spa
// 加载 var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); var data = tex.GetPixels();
上面的代码汇报以下错误:code
the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
也就是说须要将Texture标记为可读状态,可是有时候在写一些图片批处理解析的时候,大量修改readable,用完之后再改回来,是很是耗时的,因此须要使用别的方式来读取Texture的像素数据。blog
// 加载 var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath); FileStream fs = File.OpenRead(fileFullname); fs.Seek(0, SeekOrigin.Begin); byte[] image = new byte[(int)fs.Length]; fs.Read(image, 0, (int)fs.Length); var texCopy = new Texture2D(tex.width, tex.height); texCopy.LoadImage(image);
这样能够不修改readable,而且能够读取图像的信息。图片
PS:这样读出来的数据,texture的尺寸是图片的本地尺寸,和unity import setting后的大小可能会不同。ip