Unity加载 jpg 、png 、 bmp等格式的图片

这里主要记录一下在Unity中加载本地图片的方法

1、通过unity再带的texture.LoadImage来加载一些jpg、png等(无法加载bmp)

public static Texture2D LoadTextureFromLoadImage(string loadPath)
{
    var imageData = File.ReadAllBytes(loadPath);
    var texture = new Texture2D(8, 8);
    var rect = new Rect(0, 0, texture.width, texture.height);
    texture.LoadImage(imageData);
    return texture;
}

2、通过System.Drawing.Bitmap去加载(加载bmp)

public static Texture2D LoadTextureFromBitMap(string loadPath)
{
    Texture2D texture = null;
    Bitmap bmpImage = new Bitmap(loadPath);
    texture = new Texture2D(bmpImage.Width, bmpImage.Height);
    for (int y = 0; y < bmpImage.Height; y++)
    {
        for (int x = 0; x < bmpImage.Width; x++)
        {
            System.Drawing.Color color = bmpImage.GetPixel(x, y);
            texture.SetPixel(x, bmpImage.Height - y - 1, new Color32(color.R, color.G, color.B, color.A));
        }
    }
    texture.Apply();
    bmpImage.Dispose();
    return texture;
}

这个就是通过插件System.Drawing插件里的方法去加载,这种方式只需要将对应插件导入到Unity中,就引用命名空间就可以使用了,需要注意的是 .NET Standard 2.1并不支持他,所以对于使用(.NET Standard 2.1)的已有项目中加载bmp通过这种方式就不合适,Api Compatibility Level这个选项对于已有项目是不建议更改的(具体可以查看Unity官方文档)

3、不使用插件加载bmp图片

public static Texture2D LoadBMP(string filePath)
{
    byte[] fileData = File.ReadAllBytes(filePath);

    int dataOffset = fileData[10];
    int width = fileData[18] + (fileData[19] << 8);
    int height = fileData[22] + (fileData[23] << 8);
    int bpp = fileData[28];

    Texture2D texture = new Texture2D(width, height);

    int stride = width * bpp / 8;
    int padding = stride % 4;
    if (padding != 0)
        stride += 4 - padding;

    Color32[] colors = new Color32[width * height];

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            int index = dataOffset + y * stride + x * bpp / 8; // 垂直翻转修改这里
            byte b = fileData[index];
            byte g = fileData[index + 1];
            byte r = fileData[index + 2];
            colors[(height - y - 1) * width + x] = new Color32(r, g, b, 255); // 垂直翻转修改这里
        }
    }
    // 垂直翻转 Texture2D
    Color32[] flippedColors = new Color32[width * height];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            flippedColors[(height - y - 1) * width + x] = colors[y * width + x];
        }
    }
    texture.SetPixels32(flippedColors);
    texture.Apply();

    return texture;
}

这种方式的好处就是不需要修改unity的设置,可以直接使用。

这个根据大佬们的记录在写的时候,也遇到一些问题

问题一:存在一个字节对齐的问题,这个问题会导致图片倾斜(bmp图片:(需要)每行对齐到4字节的原因_c++实现bmp的对齐-CSDN博客

问题二:图片是倒立起来,这个还比较好解决,直接给他重新存一下就好了

【还有一个图片文件格式的判断,因为有些时候,后缀并不能表示图片的真是格式,通过txt文本打开可以通过开头的几个字符看出图片的格式】

判断方式如下:

public static string GetImageType(string filePath)
{
    if (!File.Exists(filePath))
    {
        return "File not found";
    }

    byte[] buffer = new byte[8];

    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        fs.Read(buffer, 0, 8);
    }

    // JPEG
    if (buffer[0] == 0xFF && buffer[1] == 0xD8 && buffer[2] == 0xFF)
    {
        return "JPEG";
    }

    // PNG
    if (buffer[0] == 0x89 && buffer[1] == 0x50 && buffer[2] == 0x4E && buffer[3] == 0x47 && buffer[4] == 0x0D && buffer[5] == 0x0A && buffer[6] == 0x1A && buffer[7] == 0x0A)
    {
        return "PNG";
    }

    // GIF
    if (buffer[0] == 0x47 && buffer[1] == 0x49 && buffer[2] == 0x46 && buffer[3] == 0x38 && (buffer[4] == 0x37 || buffer[4] == 0x39) && buffer[5] == 0x61)
    {
        return "GIF";
    }

    // BMP
    if (buffer[0] == 0x42 && buffer[1] == 0x4D)
    {
        return "BMP";
    }

    return "Unknown";
}

这个是从大佬那里摘抄的,经过试验确实可以判断出正确的类型,

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值