- Home /
How do I access a texture slice's rect via scripting?
Hi, I need to get the rect with the coordinates for each of the slices in a texture that's already been sliced in the sprite editor. Couldn't find any reference to slices in the scripting docs. Is there a way to access those "sub-images" directly?
(I'm assuming here that those sub-images are kept in some sort of texture atlas, and what I need are the coordinates of those slices in the atlas).)
Thanks!
Nonono, every image is a separate object you need to threat them as a seperate images not as a whole atlas.
DajBuz: the Q is about getting the secret Atlas coords. Say you have a goat sprite. The Sprite editor puts it in some part of the altas it makes(?), and saves those goat UV coords.
If you could get the altas texture and those goat coords, you could draw the goat sprite yourself, maybe changing it some special way.
Yes, I'm trying to manipulate the UVs associated with each slice here, not access the images themselves. I imagine there's a rect array that contains the coordinates relative to the original image, which I could convert to a vector2 array representing the uvs in the atlas. Thanks!
Answer by Mr.LeoSantos · Aug 14, 2014 at 05:05 AM
I figured it out. It's pretty obvious, actually.
Sprite.rect returns a rectangle that points to the location (in pixels) relative to the original texture. To use it as 4 UV coordinates, one for each corner, simply divide rect.xMin (cast as a float) by the image's width, rect.yMin by the image's height, rect.xMax by the image's width and rect.yMax + rect.height by the image's height again.