- Home /
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.
Also when I first load the project to iPad/iPod the textures will show up black. The next time I install it to iPad/iPod they'll come up with the distorted textures. Hope this helps!
Answer by JoelAtMoodkie · Sep 06, 2010 at 10:32 PM
Solved it. In Unity iPhone you must have "Get/Set pixels" enabled in import settings for any textures used in an atlas.
It might be useful to mention this in the Unity iPhone docs if anyone from Unity is reading this :)
Answer by Maybe_memory · Sep 13, 2012 at 12:42 PM
hello.I can't find "Get/Set pixels" in import settings.can you put a screenshot here ?
I think he means the option called "Read/Write Enabled" (4th from the top when you have a texture selected, and its "Texture Type" is set to "Advanced")
Your answer
Follow this Question
Related Questions
Clearing Editor References when Building for Mobile 1 Answer
Have there been changes to PackTextures in 5.6 ? Seeing Artifacts in Scene which disappear in 5.5 0 Answers
Can I get some profile output inside unity iphone's editor itself? 1 Answer
Editor Crash: Where is Scene Data kept? 3 Answers
Dealing with GUI size on iPhone 1 Answer