var image = transform.GetComponent<Image>();
var bits = Util.LoadBinary(BoardImage); // load off disk in binary
var tex = new Texture2D(2, 2); // size not used
tex.LoadImage(bits);
image.preserveAspect = true;
image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
But it only works for PNG and JPG, not for BMP. Unity Editor can load BMP, but it creates metadata. So it can be done, I just don’t know how.
So the question is how to load a BMP file directly in binary using the API, not via the editor.
Besides what @Seyren said in his answer you can always load any format you want as long as you know how the format is specified and as long as it’s not too complex / complicated.
Another option besides creating your own BMP loader is to search the net for a C# implementation of a BMP loader. If you don’t mind setting your project to .NET 2.0 and importing the System.Drawing assembly, you could use the Bitmap class inside System.Drawing. Though it might not work on certain platforms and is quite large.
A bit of “googling” revealed this. Not sure what language that actually is, but the code is in C# and looks like a naive straight forward implementation of the wikipedia information ^^. It just extracts the image data as a byte array as well as the image resolution. It certainly is a start to implement a proper BMP → Texture2D loader.
edit
Since the BMP format is rather simple i just went ahead and quickly wrote a BMPFile loader (github backup) for Unity. It’s hosted on my pastebin account as unlisted paste.
As i have written in the information header I have not tested it with all possible file versions. Also it doesn’t support any of the compressed formats (yet). Though it can read alpha values if required. I couldn’t test the 16 and 32 bit formats since MS Paint only supports up to 24 bit images. Paint .NET only supports 8 or 24bit. So i only tested the alpha read for indexed images. I actually changed the alpha value manually in the file with a hexeditor. I also tried to change the palette colors of a monochrome image. Funnily the windows image preview as well as my loader had no problem with a monochrome image (1bit) that has a custom palette (for example red and green). MS Paint itself doesn’t seem to actually interpret the palette and force the image to a B&W image when loaded.
Usage example:
// at the top
using B83.Image.BMP;
// [...]
BMPLoader loader = new BMPLoader();
//loader.ForceAlphaReadWhenPossible = true; // can be uncomment to read alpha
//load the image data
BMPImage img = loader.LoadBMP("C:\\Data\\B1.bmp");
// Convert the Color32 array into a Texture2D
Texture2D tex = img.ToTexture2D();
If someone uses it and / or finds any bugs or problems, feel free to leave a comment. If you try to load an unsupported format it should print some error messages in the console.
For convenience the LoadBMP method has overloads that takes either a filepath as string, a data stream, a byte array or a BinaryReader.
But Unity already knows how to load BMP files. I just want to know how.
@bunny83: I'm impressed you 'quickly wrote' such a large chunk of code. That looks useful. However, you haven't convinced me it's needed. Once the file XXX.bmp.meta has been created (by the editor), a Unity app can load BMP files just fine. There is code already in Unity somewhere to load BMP files, and I would still like to know where it is and how to get it to do its thing.
Thanks, that's the answer I was looking for. The BMP loader is not part of the editor as such, nor is it available in the runtime. It's in code that runs whenever the editor starts up, even if you never touch the file in the editor. I would call that the asset pipeline, and I would suspect it's possible to build assets that include BMP files (and probably many file types) by running a stand-alone build. But what you've told me is enough to solve my problem.
Wow, this is SO useful! Thank you - it worked like a charm! I can't believe how easy it was to implement! You even have an overload for the LoadBMP function to take in a byte array - unbelievable. I didn't even need to tweak or debug anything, it just ran. Props to you, good sir! I tried to make a BMP loader for something else in the past but had little luck, so I didn't know what I would do when I found myself in need of one now, but I'm so glad I found your comment! To the top with you! :D
If the API does not support it, you either have to wait until it does, or just go back to PNG or JPG, which in my opinion is the best option for a videogame.
BMP files are uncompressed, and will take a lot of space, and therefore, more time to load, which is not good.
I would choose in this case, between PNG and JPG, a PNG file, since it uses lossless compression, which means the image will not lose any data compared to the original, so you can freely switch to it, being the size the only difference between the two formats.
If your concern is, for example, that you have a lot of files in BMP and it’s a pain to convert them to PNG, there is a lot of batch conversors you can find on google.
I know that this is not the solution you would want, but in my opinion BMP is not a good idea.
Not to mention PNG supports transparency.
Thank you for your answer, but this is what is known on Stack Overflow as an X-Y answer. I want to do X, but you told me to do Y instead. I still want an answer for X. I know everything you said, but I still want to load BMP files (and I have a good reason, which is not relevant to the question.) Can you help or not?
But Unity already knows how to load BMP files. I just want to know how.
– dpoly@bunny83: I'm impressed you 'quickly wrote' such a large chunk of code. That looks useful. However, you haven't convinced me it's needed. Once the file XXX.bmp.meta has been created (by the editor), a Unity app can load BMP files just fine. There is code already in Unity somewhere to load BMP files, and I would still like to know where it is and how to get it to do its thing.
– dpolyThanks, that's the answer I was looking for. The BMP loader is not part of the editor as such, nor is it available in the runtime. It's in code that runs whenever the editor starts up, even if you never touch the file in the editor. I would call that the asset pipeline, and I would suspect it's possible to build assets that include BMP files (and probably many file types) by running a stand-alone build. But what you've told me is enough to solve my problem.
– dpolyWow, this is SO useful! Thank you - it worked like a charm! I can't believe how easy it was to implement! You even have an overload for the LoadBMP function to take in a byte array - unbelievable. I didn't even need to tweak or debug anything, it just ran. Props to you, good sir! I tried to make a BMP loader for something else in the past but had little luck, so I didn't know what I would do when I found myself in need of one now, but I'm so glad I found your comment! To the top with you! :D
– KyleBlumreisingerYou rock, man! Saved me so much time!
– CgPanda