- Home /
How to redye pixels of a texture?
I have a picture (256 x 512) which I use as a texture for a RawImage which has bigger resolution (around 500 x 900). The first question is, how do i get the Color[]
of the stretched texture or just access the stretched texture? When I call RawImage.texture.getPixels()
I only get an array with 256x512 entries but I need 500x900. The second question is, how to redye single Pixels? Like in the example above I want to iterate over the pixels of the stretched Texture and everytime I find a specific color I want to change the color of this pixel e.g. to red. If I do this with SetPixel()
the whole texture gets the color. I also tried to instantiate a RawImage of the size of one pixel for each pixel but this leads to a very high memory usage and also took a long time which is obvious if you remember that you have to instantiate around 450.000 RawImages. As I want to adapt the script for a WebCamTexture
and I didn't work out how to delete Textures from memory I chose this method since once I instaniated the textures/RawImages I could redye them the way I want. Are there better possibilities?
Answer by Remy_Unity · Dec 11, 2017 at 01:03 PM
First, RawImage.texture.getPixels()
will always return a 256*512 array in you case because the texture is at this size. Displaying it at 500*900 (or anything else) is just the GPU rendering a quad with this texture and applying a filter to show it bigger than it is (pixel wise).
Anyway, using Texture2D.SetPixel(x, y, color)
(doc : https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html) should correctly change a single pixel in the texture. Just don't forget to call texture2d.Apply()
after.
And finally, maybe using a shader to do the color replacement would be more optimal.
Your answer
Follow this Question
Related Questions
!texture.texture error 0 Answers
[Android+Eclipse]Get album image from mp3 file 0 Answers
Texture2D creating blurry textures 1 Answer
Custom shader or texture2d? (Best way to color swap) 1 Answer