Using information stored in atlas?

Hi,
I’m generating an atlas at runtime by combining an array of Texture2D using PackTextures method. Each textures position within the atlas is then stored in an Array. Here is my script:

static var atlasArray : Array; //Will store positions of each texture within atlas
var atlas : Texture2D; //This will be the new atlas
var textures : Texture2D[]; //The array of textures to be baked
var padding : int = 0;
var maximumAtlasSize : int = 1024;
   
function Awake()
{
    atlas = new Texture2D(1024,1024);
    atlasArray = atlas.PackTextures(textures, padding, maximumAtlasSize);
    
    //Get coords
    print(atlasArray[5]);
}

This will generate the atlas correctly and I can get the position of wanted texture, which will return its x,y,width and height. I would like to use this information to navigate each of my objects to only show its relevant texture within the atlas, but have no idea how to use the information.

I’m now breaking this information up in “x,y” and “width,height” portions by manually setting each objects SetTextureOffset and SetTextureScale. I’ve tried using GetPixels, SetPixels methods but can’t seem to get them to work. Could someone please help me?

In order to use the Info stored in your atlas on a mesh you need to adjust the uvs of the mesh. It would look something like this (C#, not testet). “olduvs” is a Vector2 array to store the original uvs of your mesh:

 public void SetUVs(Rect rect)
    {
        MeshFilter filter = GetComponent<MeshFilter>();
        if (olduv.Length == 0)
            olduv = filter.sharedMesh.uv;

        if (filter)
        {
            Vector2[] newuv = new Vector2[olduv.Length];
            for (int i = 0; i < newuv.Length; i++)
            {
                newuv *= new Vector2(*

olduv_.x * rect.width + rect.x,
olduv.y * rect.height + rect.y);
}
filter.sharedMesh.uv = newuv;
}
}_