- Home /
Texture offset not behaving like it should (?)
A spritesheet-like texture has 8x4 individual frames on it. In a C# script I'm trying to pick one frame (31 in the following example) and, during run-time, alter the texture scale and offset to only show that selected frame. Take a look at my code:
const int framesX = 8;
const int framesY = 4;
const int frames = framesX * framesY;
int frame = 31; // for example
Material material = renderer.material;
material.mainTextureScale = new Vector2(1f / framesX, 1f / framesY);
material.mainTextureOffset = new Vector2((frame % framesX) * material.mainTextureScale.x,
(frame / framesX) * material.mainTextureScale.y);
Debug.Log("offset: " + material.mainTextureOffset.x + "/" + material.mainTextureOffset.y);
8x4 = 32 frames total on the texture, we're going for frame 31, ie. the last frame. The debug output shows correct values of 0.875/0.75 which are 7/8 and 3/4 accordingly on the 8x4 sheet. But for some reason I'm not seeing the last frame in-game, but the one above (from the looks of it atleast) instead. The scaling works properly. Any ideas?
Answer by dkh · Mar 17, 2013 at 08:50 PM
Pretty sure I figured it out. Looks like Unity's UV coords start in the BOTTOM left corner of the texture. I thought it was TOP left, as I would have considered standard.
Your answer
Follow this Question
Related Questions
Offset texture ONCE on key press 1 Answer
Changing button texture by setting a texture offset of a sprite sheet?(SOLVED) 1 Answer
Texture / Material offset doesn't work 1 Answer
Shader with two textures separated by an alpha channel? 1 Answer
How Can I Make Materials Keep The Same Global Offset On Different Objects? 1 Answer