- Home /
How to change a GUI Texture color?
I got a 2d Texture and I want to change It's color, I tried with "GUI.color = Color.black" "GUI.contentColor = Color.blue" but It didn't change anything. How can I do this?
There's the code I'm using:
public class PlayerGUI : MonoBehaviour {
[SerializeField]
Texture2D _crosshair;
void OnGUI()
{
float x = (Screen.width - _crosshair.width) / 2;
float y = (Screen.height - _crosshair.height) / 2;
GUI.DrawTexture(new Rect(x, y, _crosshair.width, _crosshair.height), _crosshair);
}
}
Answer by robertbu · Jan 14, 2014 at 12:11 AM
Short of modifying the texture itself, I know of no way to change the texture color with GUI.DrawTexture(). I'm assuming you want to change the crosshair color when you are on/off target or some such? The typical way I see this solved when using GUI.Drawtexture() or using a GUITexture is to provide a second texture with a different color. Then the code changes the whole texture rather than just the color.
Yes, that's just what I thought, but I was looking for a way to do It without providing a second texture, thanks for the answer :)