Texture2D.PackTextures - attempt to determine if texture was scaled
Packing unity textures into atlases
Unity 5.2 Sprite packer too slow
Texture2D.PackTextures causing seams in atlas?
Saving Textures As Readable In AssetBundles
How do I make my textured models pull the source images for the textures in a package?
One Texture2D from multiple others
Have there been changes to PackTextures in 5.6 ? Seeing Artifacts in Scene which disappear in 5.5
PackTextures works in editor but not on iPad/iPhone ...
Hiya all.
I've made a script which loads some objects, instantiates them and then uses PackTextures() to make a texture atlas so they all use one draw call. Simple enough I thought.
It works fine in the editor but when i load it to iPad/iPhone the textures are distorted. They look a bit like this image, but more pixelated:http://www.athlens.com/myspace-backgrounds/multicolor/color_multicolor_186.jpg.
Here's my code. It's a little messy as I've not had any time to tidy it up.
public static bool showMainGUI = true;
public int iconWidth = 30;
public int padding = 15;
public Material newMat; //Created and assigned in inspector
private GameObject[] tools;
private Texture2D packedTexture = new Texture2D(1024,1024);
private void Start()
{
useGUILayout = false;
Object[] temp = Resources.LoadAll("Icons", typeof(GameObject));
tools = new GameObject[temp.Length];
Texture2D[] oldMats = new Texture2D[tools.Length];
int xPos = 50;
for(int i = 0; i<temp.Length; i++)
{
Vector3 instPoint = Camera.main.ScreenToWorldPoint(new Vector3(xPos,18,1));
tools[i] = Instantiate(temp[i],instPoint,Quaternion.identity) as GameObject;
tools[i].transform.parent = Camera.main.transform;
// Cache old materials so they can be packed into atlas
oldMats[i] = tools[i].renderer.material.mainTexture as Texture2D;
xPos+=iconWidth+padding;
}
// Pack textures and assign atlas to our new material
Rect[] texUVs = packedTexture.PackTextures(oldMats,0);
newMat.mainTexture = packedTexture;
// Assign new material to objects and adjust UVs accordingly
for(int i = 0; i<tools.Length; i++)
{
tools[i].renderer.material = newMat;
Vector2[] originUV = (tools[i].GetComponent(typeof(MeshFilter))as MeshFilter).mesh.uv;
Vector2[] newUV = new Vector2[originUV.Length];
for(int k = 0; k<originUV.Length; k++)
{
// Recalculate UV
newUV[k] = new Vector2((originUV[k].x*texUVs[i].width)+texUVs[i].x,
(originUV[k].y*texUVs[i].height)+texUVs[i].y);
}
// Reassign UVs
(tools[i].GetComponent(typeof(MeshFilter))as MeshFilter).mesh.uv = newUV;
}
}
Any help would be greatly appreciated as I'm starting to get a little tired of the inconstencies between the Unity editor and the actual build.
Is it possible to get the atlas for a font texture?
Using the PackTextures method returns an array of indexes of all the different textures in a given texture atlas, apparently. That's hugely useful, and I assume that with the font textures Unity is doing something similar so that it can effectively render text in a given font when given a string as input.
My question is: can I get access to that array (and also the characters that map to each array index)? It seems pretty definite that Unity has that information internally, and normally I wouldn't need it, but I want to be able to get at the font texture and then use GetPixels to extract a single character at a time out, and then composite those into another texture.
My goal is just to composite some drawn fonts onto custom textures of my choosing. RenderTextures can't have a transparent background, to my knowledge, and they seem awkward and troublesome in general. But I am using Unity Pro, so I have access to that if absolutely necessary.
PackTexture not packing correctly?
I have a script with this
public Texture2D[] World_Textures; public Texture2D WorldTextureAtlas; private Rect[] m_WorldTextureAtlasUvs;Later, I do this for the atlas generatio:
WorldTextureAtlas = new Texture2D(2048, 2048); m_WorldTextureAtlasUvs = WorldTextureAtlas.PackTextures(World_Textures, 0); Debug.Log(WorldTextureAtlasUvs[0]);Now, I currently only have two textures in Texture2D[] World_Textures , both are 256x256.
However, the atlas I get back stretches them to an atlas of 512x512...effectively doubling the height of my textures and their uvs. The debug statement shows this:
> (left:0.00, top:0.00, width:0.50, height:1.00) UnityEngine.Debug:Log(Object)> World:InitializeTextures() (at Assets/Scripts/World.cs:58)> World:Start() (at Assets/Scripts/World.cs:41)Any idea why this is?
Texture2D.PackTextures() - maximumAtlasSize
The manual says PackTextures() will create an atlas that has a maximum size of maximumAtlasSize, if the textures don't fit in there they will be downscaled. I have some rather long animations, that would require an atlas that is at least 2048x2048 pixels big. However for some reason PackTextures() will always create a 1024x1024 texture and downscale everything to fit in there. The result is suboptimal at best. If I pass 2048 or 4096 as maximum size it will still generate a 1024 texure. If I set it to a smaller value (e.g. 512) it will create a smaller texture (512x512). So the question is how do I get PackTextures() to create textures of sizes > 1024x1024??