- Home /
How I can stretch texture with GUI.DrawTextureWithTexCoords ?
Could someone explain me, how can I draw GUI Texture partially (with given UV-coords) and make texture stretched (with aspect ration different from it`s original)?
GUI.DrawTexture can stretch textures, but cannot draw a part of it. GUI.DrawTextureWithTexCoords can accept the UVs, but cannot stretching options.
My case: I draw animated GUI-elements, and use UVs to draw one frame of spritesheet. It worked fine until I had to animate non-square texture. Besides, the size (and aspect ratio) of the output rect is calculated runtime.
Does someone know any solution?
Thanks in advance :)
Answer by zharik86 · Oct 18, 2014 at 06:10 PM
Using GUI.DrawTextureWithTexCoords() you can draw part of a texture. You need to set correctly coordinate UVs. For example, we should draw only a half of a texture in a certain size. See an example below(write on CSharp):
public Texture2D myTex = null; //our texture, reference in Inspector
private Rect myRect; //our UVs coordinate
void Start() {
//Initialixation rectangle for half texture;
myRect = new Rect(0, 0, 0.5f, 0.5f);
}
void OnGUI() {
//Draw our part texture for size, for example, 300x200. It doesn't consider aspect of a texture
//first element - position, second - texture, third - UVs
GUI.DrawTextureWithTexCoords(new Rect(20, 20, 300, 200), myTex, myRect);
}
I hope that it will help you.
do your mean that if I have a source texture, for example, 300x200, then myRect = new Rect(0, 0, 1f, 0.5f) will stretch a texture vertically to fill all the Rect(20, 20, 300, 200) on screen?
@hav_ngs_ru You misunderstand textural coordinates. For your texture 300x200 and myRect = new Rect (0, 0, 1f, 0.5f), you receive the picture stretch down in Rect(20, 20, 300, 200). And its initial size will be 300x100. But as we insert it into the size 300x200, the vertical component will be expanded. It is checked for Unity 4.1.2.
yes, I not quite correctly formulated question. I actually ment "will stretch a texture vertically to fill all the Rect(20, 20, 300, 200) by half of texture". So, you in fact answered "yes" to my question :) Thank you for answer.
zharik86, sure, but I'll test your advice first ;)
still haven't test because the request is in backlog yet