- Home /
How to change a certain colour in a texture?
At the moment I have one texture on a sphere and the texture has three different colours:
eg: white or in RGBA (1,1,1,1), Black (0,0,0,1) and red(1,0,0,1).
I then have three different buttons one for each colour and I want that button to change the texture colour to green (0,1,0,1). My initial plan of attack was to do something like this: Texture.color(1,1,1,1) = color(0,1,0,1). Selecting all the white from a texture and make it green. I’ve tried to use getColor & setColor, while using a material that is the texture. I'm quite lost in what to do/ wondering if it is even possible to do in unity. Would I be changing the texture or material/ which would be better? My Coding experience is quite low, but I'm willing to learn to get it working.
In the end I'll actually have it so I can change it to any colour using a colour picker, but for testing purposes I just want to stick with one colour.
Any help on this will be much appreciated.
Answer by Benproductions1 · Jul 14, 2013 at 01:57 AM
Hello,
If the only thing that has to change on you'r texture is the "tint color", then changing the color of the material is just fine. It's also a lot faster, since you're just telling the GPU to use a different color for tinting, rather than a completely different texture.
On how to do this, check here: http://answers.unity3d.com/questions/209573/how-to-change-material-color-of-an-object.html
If you want to change the texture itself (not very easy), you will have to:
Firstly, cast it to a Texture2D
. This is Unity's "editable" texture. However note, that by default you cannot grab the Texture
from an object and cast it to a Texture2D
, unless you change some import settings.
This Texture2D
allows you to do things like GetPixel
and SetPixel
, allowing for more accurate, pixel based color "setting" and "getting".
Check the documentation on Texture2D
for more information :)
Hope this helps,
Benproductions1
thanks for the quick reply I had a look at the "tint color", however I don't think this is entirely what I want, as I want to be able to edit all the three colors not just the texture as a whole. (or am I doing something wrong, I read something about 50% gray works the best from the link: http://answers.unity3d.com/questions/209573/how-to-change-material-color-of-an-object.html does this have to do anything with it?)
Could you help explain a bit more in regards to Texture2D, I've read into it before and tried but just couldn't seem to get it to work. $$anonymous$$y understanding: I have to declare the texture public texture2D testTexture; the texture needs to be readable in the import settings then within my button code which is on my GUI, I'll have to place: GetPixels, which will return an array, then find which pixels are the right color, then change them, to the color it says on the button or do I straight out use SetPixels?
@$$anonymous$$averre that is exactly what you have to do.
If you wanted to, you could do it using shaders, which would be faster, but you'd have to learn how to write them yourself :)
Thanks Benproductions1 I'll have a look into shaders and see where that leads me, otherwise I'll give the other way another crack and hopefully I can get it to work.
Don't forget call the Texture2D.Apply() method for the changes to take effect.
Your answer

Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
Material doesn't have a color property '_Color' 4 Answers
3D models has white parts 0 Answers
Texture and material not showing up 2 Answers
Double Layer Material 0 Answers