如何在运行时使用.bmp文件并在Unity中创建纹理? [英] How can I use a .bmp file and create a Texture in Unity at runtime?

查看:346
本文介绍了如何在运行时使用.bmp文件并在Unity中创建纹理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unity项目中工作,用户在其中选择用于制作 Texture2D 并粘贴到模型的图像文件( .bmp 格式),我创建了下一个代码,可以很好地处理 .png .jpg 文件,但是当我尝试加载 .bmp 时,我只有一个(我假设)默认纹理带有红色的?"符号,所以我认为是针对图像格式的,如何在运行时使用 .bmp 文件创建纹理?

I'm working in a Unity Project where the user choose image files (in .bmp format) that is used to make a Texture2D and pasted to a model, I create the next code, I work fine with .png and .jpg file, but when I try load .bmp I got only a (I assume) default texture with a red "?" symbol, so I think is for the image format, how can I create a Texture using .bmp files at run-time?

这是我的代码:

public static Texture2D LoadTexture(string filePath)
{
    Texture2D tex = null;
    byte[] fileData;

    if (File.Exists(filePath))
    {
        fileData = File.ReadAllBytes(filePath);
        tex = new Texture2D(2, 2);
        tex.LoadImage(fileData);
    }

    return tex;
}

推荐答案

Texture2D.LoadImage 函数仅用于将PNG/JPG图像字节数组加载到 Texture 中.它不支持 .bmp ,因此红色符号通常意味着损坏的图像或未知的图像.

The Texture2D.LoadImage function is only used to load PNG/JPG image byte array into a Texture. It doesn't support .bmp so the red symbol which usually means corrupted or unknown image is expected.

要在Unity中加载 .bmp 图像格式,您必须阅读并理解 .bmp 格式规范,然后实现将其字节数组转换为Unity的Texture的方法.幸运的是,这已经由另一个人完成了.抓住 BMPLoader 插件 此处 .

To load .bmp image format in Unity, you have to read and understand the .bmp format specification then implement a method that converts its byte array into Unity's Texture. Luckily, this has already been done by another person. Grab the BMPLoader plugin here.

要使用它,请使用B83.Image.BMP 命名空间包含:

To use it, include the using B83.Image.BMP namespace:

public static Texture2D LoadTexture(string filePath)
{
    Texture2D tex = null;
    byte[] fileData;

    if (File.Exists(filePath))
    {
        fileData = File.ReadAllBytes(filePath);

        BMPLoader bmpLoader = new BMPLoader();
        //bmpLoader.ForceAlphaReadWhenPossible = true; //Uncomment to read alpha too

        //Load the BMP data
        BMPImage bmpImg = bmpLoader.LoadBMP(fileData);

        //Convert the Color32 array into a Texture2D
        tex = bmpImg.ToTexture2D();
    }
    return tex;
}

您还可以跳过 File.ReadAllBytes(filePath); 部分,并将 .bmp 图像路径直接传递到 BMPLoader.LoadBMP 功能:

You can also skip the File.ReadAllBytes(filePath); part and pass the .bmp image path directly to the BMPLoader.LoadBMP function:

public static Texture2D LoadTexture(string filePath)
{
    Texture2D tex = null;

    if (File.Exists(filePath))
    {
        BMPLoader bmpLoader = new BMPLoader();
        //bmpLoader.ForceAlphaReadWhenPossible = true; //Uncomment to read alpha too

        //Load the BMP data
        BMPImage bmpImg = bmpLoader.LoadBMP(filePath);

        //Convert the Color32 array into a Texture2D
        tex = bmpImg.ToTexture2D();
    }
    return tex;
}

这篇关于如何在运行时使用.bmp文件并在Unity中创建纹理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆