如何在运行时使用 .bmp 文件并在 Unity 中创建纹理?
How can I use a .bmp file and create a Texture in Unity at runtime?
我在一个 Unity 项目中工作,用户选择用于制作 Texture2D
并粘贴到模型的图像文件(.bmp
格式),我创建下一个代码,我可以很好地处理 .png
和 .jpg
文件,但是当我尝试加载 .bmp
时,我只有一个(我假设)带有红色“?”的默认纹理符号,所以我认为是图像格式,如何在 运行 时使用 .bmp
文件创建纹理?
这是我的代码:
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
,因此通常表示图像损坏或未知的红色符号是预期的。
要在 Unity 中加载 .bmp
图像格式,您必须阅读并理解 .bmp
格式规范,然后实现一个将其字节数组转换为 Unity 的纹理的方法。幸运的是,这已经由另一个人完成了。获取 BMPLoader
插件 here.
要使用它,请包含 using B83.Image.BMP
命名空间:
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
函数:
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;
}
我在一个 Unity 项目中工作,用户选择用于制作 Texture2D
并粘贴到模型的图像文件(.bmp
格式),我创建下一个代码,我可以很好地处理 .png
和 .jpg
文件,但是当我尝试加载 .bmp
时,我只有一个(我假设)带有红色“?”的默认纹理符号,所以我认为是图像格式,如何在 运行 时使用 .bmp
文件创建纹理?
这是我的代码:
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
,因此通常表示图像损坏或未知的红色符号是预期的。
要在 Unity 中加载 .bmp
图像格式,您必须阅读并理解 .bmp
格式规范,然后实现一个将其字节数组转换为 Unity 的纹理的方法。幸运的是,这已经由另一个人完成了。获取 BMPLoader
插件 here.
要使用它,请包含 using B83.Image.BMP
命名空间:
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
函数:
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;
}