如何在运行时使用.bmp文件并在Unity中创建纹理?

编程入门 行业动态 更新时间:2024-10-28 02:23:17
本文介绍了如何在运行时使用.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中创建纹理?

本文发布于:2023-11-12 07:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   纹理   文件   如何在   bmp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!