- Home /
Tiling textures issue
Hi, guys:
I need to create a graphical display for a variable in my game. This variable (energy) is decreasing over time so i want a colored label indicating the level of energy. The variable is controlled by a script (charscript) in the char. So i created a texture bitmap which is a green-red gradient. This variable goes from 100 (total green) to 1 (total red) so i've been watching around and i've come to this:
public Texture2D gradbitmap;
public charscript cs;
public float energygradient;
cs = (charscript)GameObject.Find("Char").GetComponent<charscript>();
OnGUI(){
energygradient=cs.energy;
GUI.DrawTextureWithTexCoords(new Rect(500,80,20,20), gradbitmap, new Rect(0f,0f,energygradient,1f));
}
The problem is energygradient will draw a percent of the bitmap into the label, so if it is 0.1 it will draw 10% of the left side of the bitmap, -0.1 10% of the right side of the bitmap, 0.2 20% of the left side of the map and so on. But the variable decreases from 100 to 1. I want the label shows only 10% of the bitmap, but i need it to "move" along the bitmap, first 10%, then from 1% to 11%, 2% to 12% ... and finally 90% to 100%. I mean to create a "window" on the gradient texture so it looks like the GUI object becomes green to red in a soft way.
I can't find a math solution for this. Do you know any other way to achieve it?
I think the only way to have this working by maths relies on TexCoords. It is defined by a Rect(x1,y1,x2,y2). All these coordinates are between 0-1, but i'm trying different values for x1 and x2 with no good results, and i cant find documentation about it. I first thought Rect(energygradient,0f,energygradient+0.1,1f) would do it, but it isnt.
Answer by Baalhug · Dec 12, 2013 at 09:09 AM
Well, i have found he way to do it. It seems the texture coordinates (TexCoords) in DrawTextureWithTexCoords(position, texture, TexCoords) are as follow:
Rect(FromX, FromY, ToX, ToY)
They are all in values from 0 to 1, so 0.1 is 10% of the texture, 0.5 is 50% and so on. ToX and ToY are the percent amount of bitmap to display, and FromX, FromY are the percent of the texture where it starts to print.
I used:
energygradient=cs.energy/100; GUI.DrawTextureWithTexCoords(new Rect(500,80,20,20), gradbitmap, new Rect(energygradient,0f, 0.1f, 1f));
So energygradient will have a value from 0 to 1 too (actually from 1 to 0 as it begins in 100 and decreases to 1). This way at the beginning it will be:
Rect (1f,0f,0.1f,1f)
In the middle:
Rect (0.5f,0f,0.1f,1f)
And finally:
Rect (0.01f, 0f, 0.1f, 1f)
| | | |
| | | -> End of texture Y (100%)
| | ------> End of texture X (10%)
| ------------> Start of texture Y (0%)
-----------------> Start of texture X (1%)
It will always begin to print based on the actual energy and will display 10% more of the texture. Vertical coordinates are always full, from 0 to 1 (0% to 100%)
Your answer
Follow this Question
Related Questions
GUI.DrawTexture not working? (nothing shows up) 1 Answer
inventory system - change GUI.DrawTexture texture 3 Answers
PackTextures and DrawTexture 1 Answer
Tile and offset a Texture2D? 2 Answers
Help with destroying guiRect? 0 Answers