这可能是两件事,也许是结合起来的:choice of coordinate system 和 Endianness
有一个约定(我相信是通用的)从左到右列出像素,但没有关于 vertical 方向的约定。虽然一些程序和 API 的 Y 坐标在底部为零并向上增加,但其他程序和 API 则完全相反。我不知道你从哪里得到byte[],但有些API 允许你在写入、读取或使用纹理时配置像素方向。否则,您将不得不手动重新排列行。
这同样适用于字节顺序; ARGB 有时意味着蓝色是最后一个字节,有时是第一个字节。一些类,如 BitConverter 也有内置解决方案。
Unity 使用 big-endian、bottom-up 纹理。事实上,Unity handles lots of this stuff under the hood,并且在导入位图文件时必须重新排序行和翻转字节。 Unity 还提供了LoadImage 和EncodeToPNG 等方法来解决这两个问题。
为了说明byte[] 会发生什么,此示例代码以三种不同的方式保存相同的图像(但您需要将它们导入为 Truecolor 以在 Unity 中正确查看它们):
using UnityEngine;
using UnityEditor;
using System.Drawing;
using System.Drawing.Imaging;
public class CreateTexture2D : MonoBehaviour {
public void Start () {
int texWidth = 4, texHeight = 4;
// Raw 4x4 bitmap data, in bottom-up big-endian ARGB byte order. It's transparent black for the most part.
byte[] rawBitmap = new byte[] {
// Red corner (bottom-left) is written first
255,255,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
255,0,0,255, 255,0,0,255, 255,0,0,255, 255,0,0,255
//Blue border (top) is the last "row" of the array
};
// We create a Texture2D from the rawBitmap
Texture2D texture = new Texture2D(texWidth, texHeight, TextureFormat.ARGB32, false);
texture.LoadRawTextureData(rawBitmap);
texture.Apply();
// 1.- We save it directly as a Unity asset (btw, this is useful if you only use it inside Unity)
UnityEditor.AssetDatabase.CreateAsset(texture, "Assets/TextureAsset.asset");
// 2.- We save the texture to a file, but letting Unity handle formatting
byte[] textureAsPNG = texture.EncodeToPNG();
System.IO.File.WriteAllBytes(Application.dataPath + "/EncodedByUnity.png", textureAsPNG);
// 3.- Rearrange the rawBitmap manually into a top-down small-endian ARGB byte order. Then write to a Bitmap, and save to disk.
// Bonus: This permutation is it's own inverse, so it works both ways.
byte[] rearrangedBM = new byte[rawBitmap.Length];
for (int row = 0; row < texHeight; row++)
for (int col = 0; col < texWidth; col++)
for (int i = 0; i < 4; i++)
rearrangedBM[row * 4 * texWidth + 4 * col + i] = rawBitmap[(texHeight - 1 - row) * 4 * texWidth + 4 * col + (3 - i)];
Bitmap bitmap = new Bitmap(texWidth, texHeight, PixelFormat.Format32bppArgb);
var data = bitmap.LockBits(new Rectangle(0, 0, texWidth, texHeight), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
System.Runtime.InteropServices.Marshal.Copy(rearrangedBM, 0, data.Scan0, rearrangedBM.Length);
bitmap.UnlockBits(data);
bitmap.Save(Application.dataPath + "/SavedBitmap.png", ImageFormat.Png);
}
}