- Home /
How to slide UVs of a GUI Texture
I have a GUI Texture of a boost gauge, the texture contains all 5 states of the boost gauge. Instead of having to cut them up into 5 different textures and swapping them between them when my condition becomes true, I would rather use only 1 texture and "slide" the UVs down to the new boost gauge state.
I tried GUI.DrawTexture but it doesn't work for sliding UVs. I was thinking about using iTween to slide the UVs, but I don't even know how to specify the UV coordinates between which to slide. Does anyone have a good method for sliding UVs on a GUI Texture ?
Answer by Eric5h5 · Jan 31, 2011 at 05:14 AM
GUITextures have no access to UVs. You can use a custom plane with an unlit shader, and change renderer.material.mainTextureOffset.
Ah I was beginning to think that, thanks a lot for your quick answer, it saved me lots of "research" time!!!
hmmm it looks like using the begingroup trick below you don't actually need UV's to animate a sprite on GUI
Answer by 3DMagicVR · Mar 19, 2011 at 06:07 AM
Hi, you can make that using this:
Function OnGUI() {
GUI.BeginGroup(the position and size of the button);
GUI.Label(the main texture);
GUI.EndGroup();
}
then use a functiont to make the image moves an "x" value in the position.x example: the image have a 60 width and have 5 images on it, the width of every image will be 12, so add 12 at every x-time in the position.x in the GUI.Label position so the image looks like a gif animation.
for some reason, gui.label or begingroup forces a stretch or distortion or nonlinear offset error for the sprite frames...
ahh... six hours of trying to find the fudge factor... it looks like it's due to the 3px/4px margin/padding in default gui style!! setting the label margin padding to 0 fixed it!!
Answer by maxime.du · Feb 07, 2012 at 11:24 AM
The public beta (Unity 3.5 version) now contains
GUI.DrawTextureWithTexCoords(PositionRect, Texture, UVRect);
This works fine with GUI with GUILayout!
Thanks!
Roger
the same thing is available earlier too with graphics.drawtexture ()
not a big difference
But Graphics.DrawTexture() has issues if used within GUILayout. It wont work correctly used in ScrollView() and other elements.
The 3.5 version of DrawTextureWithTexCoords() works perfectly.
Answer by maxime.du · Feb 07, 2012 at 10:27 AM
Graphics.DrawTexture(PositionRect, Texture, UVRect, 0, 0, 0, 0);
solves the problem with one line of code. This does not include GUILayout, here it will work only with some limitations.
Regards, Roger
Your answer
