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 _= Instantiate(temp*,instPoint,Quaternion.identity) as GameObject;*_
<em>_tools*.transform.parent = Camera.main.transform;*_</em>
<em>_*// Cache old materials so they can be packed into atlas*_</em>
<em><em><em>oldMats _= tools*.renderer.material.mainTexture as Texture2D;*_</em></em></em>
<em><em><em>_*xPos+=iconWidth+padding;*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*// Pack textures and assign atlas to our new material*_</em></em></em>
<em><em><em>_*Rect[] texUVs = packedTexture.PackTextures(oldMats,0);*_</em></em></em>
<em><em><em>_*newMat.mainTexture = packedTexture;*_</em></em></em>
<em><em><em>_*// Assign new material to objects and adjust UVs accordingly*_</em></em></em>
<em><em><em>_*for(int i = 0; i<tools.Length; i++)*_</em></em></em>
<em><em><em>_*{*_</em></em></em>
<em><em><em><em>_tools*.renderer.material = newMat;*_</em></em></em></em>
<em><em><em><em><em>_Vector2[] originUV = (tools*.GetComponent(typeof(MeshFilter))as MeshFilter).mesh.uv;*_</em></em></em></em></em>
<em><em><em><em><em>_*Vector2[] newUV = new Vector2[originUV.Length];*_</em></em></em></em></em>
<em><em><em><em><em>_*for(int k = 0; k<originUV.Length; k++)*_</em></em></em></em></em>
<em><em><em><em><em>_*{*_</em></em></em></em></em>
<em><em><em><em><em>_*// Recalculate UV*_</em></em></em></em></em>
<em><em><em><em><em><em><em>newUV[k] = new Vector2((originUV[k].x*texUVs_.width)+texUVs*.x,*_</em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em>(originUV[k].y*texUVs_.height)+texUVs*.y);*_</em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em>_*}*_</em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em>_*// Reassign UVs*_</em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em><em>_(tools*.GetComponent(typeof(MeshFilter))as MeshFilter).mesh.uv = newUV;*_</em></em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em><em>_*}*_</em></em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em><em>_*}*_</em></em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em><em>_*```*_</em></em></em></em></em></em></em></em></em></em>
<em><em><em><em><em><em><em><em><em><em>_*<p>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.</p>*_</em></em></em></em></em></em></em></em></em></em>