- Home /
SetPixels and Alpha8 - most efficient method?
I need to set an Alpha8 texture quickly (I generate it on the fly). I see that SetPixels will work with it, but that takes a Color array (note: the float value kind with RGBA). SetPixels32 works with 8-bit RGBA's. What I was hoping for was something like a SetPixels8, or Color8 or ColorGray type of API (none exist that I know of).
So do I really need to allocate a 32-bit color array for my 8-bit bitmap? Or a full RGBA pixel array that converts to float and back? Is there a better way?
Answer by Eric5h5 · Aug 29, 2011 at 11:05 PM
SetPixels is the only way. Sounds like a good candidate for a feature request though.
Answer by Lumen Digital · Nov 23, 2014 at 08:06 PM
Try creating your texture as a byte array and use an Alpha8 texture...
Create the texture
texture = new Texture2D(width, height, TextureFormat.Alpha8, false);
Create a buffer
pixelBuffer = new byte[width * height];
Fill the buffer with the pixel values (0 - 255)....
Load the buffer into the texture
texture.LoadRawTextureData(buffer);
texture.Apply();
To get the pixel vale in your shader use the alpha value of the texture
tex2D(_MainTex, i.texcoord).a
This works for us.
Yes, this works very well for us too. Thank you for this answer!
This almost works, but i feel like it copy 1/4 of the picture over and over on the same picture. How could I fill the pixel values with only the alpha of a texture efficiently ?
For now, I have
textureA.LoadRawTextureData(Texture2DRGB.GetRawTextureData());
Your answer
![](https://koobas.hobune.stream/wayback/20220612193854im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I fill a texture with one color? 2 Answers
Texture randomly generated terrain 0 Answers
Convert 2D array to a 1D array 2 Answers