- Home /
GUI.DrawTexture weird behaviour
Sorry for the relatively long question.
I am trying to use GUI.DrawTexture to darken the screen with a specific color. To darken the screen, apparently the recommended way is to change the GUI.color. This is what I am doing.
However, I also wanted to tint it blue, so I am also loading a single-pixel texture and tinting it dynamically:
mTexture = Resources.Load("textures/blackPixel") as Texture2D;
// Make the texture pixels blue.
mTexture.SetPixel(0, 0, effectColor);
mTexture.SetPixel(0, 1, effectColor);
mTexture.SetPixel(1, 0, effectColor);
mTexture.SetPixel(1, 1, effectColor);
mTexture.Apply(false);
Once I have that texture created, I am drawing a texture overlay using that texture and modifying the alpha:
var savedColor = GUI.color;
mColor.a = 0.3f;
GUI.color = mColor;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mTexture);
GUI.color = savedColor;
However, the result is the following:
If I do all of this in a new script component (called ScreenEffect) and I dynamically instance this script from within another script, then it works as expected (The screen is darkened and tinted blue).
If I attach the script in the editor (which should be just the same, I use all defaults), then the screen is darkened but the screen is not tinted blue at all.
Sub-Question 1: Is my approach right? Or is there an easier way? Just doing a GUI.color = Color.blue doesn't seem to work at all (which is why I am doing the weird texture-creation thing).
Sub-Question 2: Why would it work as expected when I instance it dynamically, but not work when I add the script through the editor? Any idea?
Thanks and sorry for the lengthy question.