I’m trying to blit or draw part of a texture from a larger texture in onGUI using GUI.Label. The problem is
the part of the texture blitted is wrong and slightly off, it gets more off the further right into the source texture I try and blit from.
If you create a 512x256 RGBA texture and then try and blit from x=480 it actually blits from approx 490ish and I’ve no idea why. Here is my code:
It’s supposed to copy the 32x64 rectangle at coords 480,128 in the source texture. Annoyingly if you try and copy say from 0,128 it all works fine. Then 32,128 missed the 1st pixel on the left and so it gets worse. Any ideas?
GUI.DrawTexture doesn’t let you cut out a source rectangle though, I used this first attempt and came up against that problem.
On another thread the label idea was mooted to solve the problem.
I’ll try passing label a style with everything zeroed and see if that helps.
The fix was so simple!! You need to pass in a style with zero padding!! here is the new code. Only cost me a couple of hours!
UNITY needs GUI.Label to have a source rect!!
public static GUIStyle blit_style = new GUIStyle();
blit_style.border.left = 0;
static void DrawTextureClipped(Texture2D textureToDraw, float u1, float v1, float u2, float v2, float x, float y)
{
float du = u2 - u1;
float dv = v2 - v1;
GUI.BeginGroup(new Rect(x, y, 32, 64));
GUI.Label(new Rect(-u1, -v1, textureToDraw.width + 1, textureToDraw.height), textureToDraw, blit_style);
GUI.EndGroup();
}