I need to create a texture Atlas dynamically, so I have used Texture2D.PackTextures to pack all textures in a folder named “Images” in my Resources folder.
Texture2D[] atlasTextures = Resources.LoadAll<Texture2D>("Images");
Texture2D atlas = new Texture2D(4096, 4096);
Rect[] rects = atlas.PackTextures(atlasTextures, 2, 4096);
string atlasDirPath = Application.persistentDataPath + "/Resources/Images";
if (!Directory.Exists(atlasDirPath))
{
Directory.CreateDirectory(atlasDirPath);
}
FileStream fs = new FileStream(atlasDirPath+"Atlas.png", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(atlas.EncodeToPNG());
bw.Close();
fs.Close();
So an Atlas Images is perfectly generated and saved in the persistent path.
Now, I need to get a particluar texture individually and convert it to sprite and add it to spriteRenderer in my scene. But to select a texture, I only have the name/path of the texture.
But Texture2D.PackTextures returns Rect array.
Now I don’t understand how should I pick a particular texture from the atlas with me only having the name of the texture.