Is it possible to take a small image from a texture sheet?

Hi,

If I have a texture sheet like below, how do I extract/take an image from it? I’d like to combine all my textures into 1 sheet but not sure how to call it?

  • The texture is not mine, I’m just using it for the purpose of this question.

Thanks.
-Hakimo

Create a new material and add your image to it as the texture.

You use the .Tiling and .Offset properties of the texture to select the image you want.

Remember that the 0,0 location is the BOTTOM-left corner. Also remember that coordinates are between 0 and 1, where 1 is the width or height of the entire texture.

The material should be applied to a plane. The plane should be the correct height/width or you will see more (or less) than just the image you are after.

Use something like the following (code is untested):

// This is your texture atlas (the big image)
var textureWidth = 600;
var textureHeight = 800;

// This defines the small image you want, starting TOP-left
var subImageX = 300; // pixels from left
var subImageY = 270; // pixelthis from TOP
var subImageWidth = 100;
var subImageHeight = 100;

// Calculate the coordinates within the texture
var calcImageWidth = subImageWidth/textureWidth;
var calcImageHeight = subImageHeight/textureHeight;
var calcImageX = subImageX/textureWidth;
var calcImageY = 1-(subImageY/textureHeight)-calcImageHeight;

// Set the texture on your tile to display the correct image
gameObject.renderer.material.mainTextureOffset = new Vector2(calcImageX , calcImageY);

Use UV mapping as appropriate to map parts of textures where desired.

–Eric

Thanks for the replies :slight_smile:

How do I use UV mapping in Unity? Is it something done in the inspector? I find it really tricky to adjust the values for offset and tiling to match the image size I want.

-Hakimo

As in my post above, you can use material.mainTextureOffset (as a short-cut) or several other methods. Look at the help section for Material.

Material.SetTextureOffset
Material.SetTextureScale
Material.mainTextureOffset
Material.mainTextureScale