- Home /
Can we affect the packing order when using Texture2D.PackTextures
Hello fellow Unity3d Users,
First off, thanks for taking the time to look at this post.
[The issue] I'm using Texture2D.PackTextures(textures[],0)
All of the textures I'm packing are the same Width(256) and Height(256). I've added each texture to an array in the order In which they need to be, however the order always breaks, resulting in 1 or 2 frames of the animation being offset vertically and/or horizontally.
-Juoni
[edit - Little Angel]
//The Atlas were we're storing our textures
Texture2D atlas = null;
//The Locations of our textures on the atlas
Rect[] rects;
//The textures we're going to back
List<Texture2D> temptextures = new List<Texture2D>();
//The width and hight our our textures
int[] texturesize = new int[2];
//Add textures for each Animations
//to the atlas
foreach (PsiAnimation psianim in LoadChosenAnimations())
{
temptextures.AddRange(psianim.GetAnimation);
Debug.Log(psianim.AnimationName);
}
//Add The required width and high
//to the atlas
//for each texture
Texture2D[] texturearry = new Texture2D[temptextures.Count];
foreach (Texture2D t in temptextures)
{
texturearry = ArrayHelper.AddItemToArray<Texture2D>(texturearry, t);
Debug.Log(t.name);
texturesize[0] += t.width;
texturesize[1] += t.height;
}
//Make the atlast the size needed
atlas = new Texture2D(texturesize[0], texturesize[1]);
if(DEBUG)
Debug.Log(atlas.width + atlas.height);
Debug.Log(texturesize[0].ToString() +" : w| h " + texturesize[1].ToString());
endif
//Store the coords of each texture
rects = atlas.PackTextures( texturearry,padding);
So, you are asking:
"Is there any way to control the order and location that textures are packed into a new atlas?"
Correct?
Code could help, if you don't $$anonymous$$d - just in case...
<code>//The Atlas were we're storing our textures
Texture2D atlas = null;
//The Locations of our textures on the atlas
Rect[] rects;
//The textures we're going to back
List<Texture2D> temptextures = new List<Texture2D>();
//The width and hight our our textures
int[] texturesize = new int[2];
//Add textures for each Animations
//to the atlas
foreach (PsiAnimation psianim in LoadChosenAnimations())
{
temptextures.AddRange(psianim.GetAnimation);
Debug.Log(psianim.AnimationName);
}
//Add The required width and high
//to the atlas
//for each texture
Texture2D[] texturearry = new Texture2D[temptextures.Count];
foreach (Texture2D t in temptextures)
{
texturearry = ArrayHelper.AddItemToArray<Texture2D>(texturearry, t);
Debug.Log(t.name);
texturesize[0] += t.width;
texturesize[1] += t.height;
}
//$$anonymous$$ake the atlast the size needed
atlas = new Texture2D(texturesize[0], texturesize[1]);
if(DEBUG)
Debug.Log(atlas.width + atlas.height);
Debug.Log(texturesize[0].ToString() +" : w| h " + texturesize[1].ToString());
endif
//Store the coords of each texture
rects = atlas.PackTextures( texturearry,padding);</code>
Answer by Aithoneku · Feb 18, 2014 at 01:24 PM
AFAIK no, you can not.
But the rects returned by PackTextures is what you can use - it's relative position (UV coordinates) of each texture in the atlas so you can use these to run your animation - rects[0] contains position of first frame, rects[1] second frame etc.