/// <summary> /// 合并贴图 /// </summary> /// <param name="textureList"></param> /// <param name="atlasName"></param> /// <param name="isTransparent"></param> /// <param name="atlasWidth"></param> /// <param name="atlasHeight"></param> /// <returns></returns> private static List <TextureAtlas> PackAtlas(List <Texture2D> textureList, string atlasName, bool isTransparent, int atlasWidth, int atlasHeight) { int atlasIndex = 0; int textureCount = textureList.Count; textureList.Sort((a, b) => { return(a.width * a.height - b.width * b.height); }); List <TextureAtlas> atlasList = new List <TextureAtlas>(); string fileName = atlasName + "_" + atlasIndex; string assetPath = TextureAtlas.GetAssetPath(isTransparent, fileName); while (File.Exists(assetPath)) { TextureAtlas atlas = AssetDatabase.LoadAssetAtPath <TextureAtlas>(assetPath); if (null != atlas) { atlas.Layout(); atlasList.Add(atlas); } atlasIndex++; fileName = atlasName + "_" + atlasIndex; assetPath = TextureAtlas.GetAssetPath(isTransparent, fileName); } List <Texture2D> newlyTextures = new List <Texture2D>(); //找出新增的图片 for (int i = 0; i < textureList.Count; ++i) { bool found = false; for (int j = 0; j < atlasList.Count; ++j) { TextureAtlas atlas = atlasList[j]; if (null != atlas) { TextureAtlasElement element = atlas.GetElement(textureList[i]); if (null != element) { found = true; break; } } } if (!found) { newlyTextures.Add(textureList[i]); } } //Asset中删除图片已经删除的记录 for (int i = 0; i < atlasList.Count; ++i) { TextureAtlas atlas = atlasList[i]; for (int j = atlas.ElementList.Count - 1; j >= 0; --j) { Texture2D elementTex = atlas.ElementList[j].Tex; bool found = false; for (int m = 0; m < textureList.Count; ++m) { if (elementTex == textureList[m]) { found = true; break; } } if (!found) { atlas.RemoveElementAt(j); } } } //排列新增的图片 for (int i = 0; i < newlyTextures.Count; ++i) { Texture2D newlyTexture = newlyTextures[i]; if (null != newlyTexture) { bool added = false; for (int j = 0; j < atlasList.Count; ++j) { if (atlasList[j].AddTexture(newlyTexture)) { added = true; break; } } if (!added) { fileName = atlasName + "_" + atlasList.Count; assetPath = TextureAtlas.GetAssetPath(isTransparent, fileName); TextureAtlas atlas = ScriptableObject.CreateInstance <TextureAtlas>(); if (null != atlas) { atlas.Init(atlasWidth, atlasHeight, false, isTransparent, fileName); atlas.AddTexture(newlyTexture); atlasList.Add(atlas); } else { Debug.Log("Create atlas instance failed."); } } } } for (int i = 0; i < atlasList.Count; ++i) { EditorUtility.DisplayProgressBar("", "Pack atlas ", (i + 1) / atlasList.Count); atlasList[i].Pack(); } EditorUtility.ClearProgressBar(); return(atlasList); }
public static Mesh DoCombine(List <MeshData> meshDatas, string newMeshName) { Mesh combineMesh = new Mesh { name = newMeshName }; int vertexCount = 0; int triangleCount = 0; //只要有一个元素没有值,则设为没有值 bool hasUV2Data = true; bool hasNormalData = true; bool hasColorData = true; for (int i = 0; i < meshDatas.Count; ++i) { MeshData meshData = meshDatas[i]; int vertexLength = meshData.vertices.Length; vertexCount += vertexLength; triangleCount += meshData.triangles.Length; if (null == meshData.uv2 || meshData.uv2.Length != vertexLength) { hasUV2Data = false; } if (null == meshData.normals || meshData.normals.Length != vertexLength) { hasNormalData = false; } if (null == meshData.colors || meshData.colors.Length != vertexLength) { hasColorData = false; } } Vector3[] vertices = new Vector3[vertexCount]; int[] triangles = new int[triangleCount]; Vector2[] uvs = new Vector2[vertexCount]; Vector2[] uv2s = new Vector2[vertexCount]; Color[] colors = new Color[vertexCount]; Vector3[] normals = new Vector3[vertexCount]; int vertexArrayIndex = 0; int triangleArrayIndex = 0; for (int i = 0; i < meshDatas.Count; ++i) { MeshData meshData = meshDatas[i]; if (null != meshData) { meshData.vertices.CopyTo(vertices, vertexArrayIndex); for (int j = 0; j < meshData.triangles.Length; ++j) { triangles[triangleArrayIndex + j] = meshData.triangles[j] + vertexArrayIndex; } IntVector2 elementOffset = meshData.texData.Element.Offset; TextureAtlas atlas = meshData.texData.Atlas; int atlasWidth = atlas.Width; int atlasHeight = atlas.Height; Vector2 uvOffset = new Vector2(elementOffset.x / (float)atlasWidth, elementOffset.y / (float)atlasHeight); float atlasRealWidth = atlas.Atlas.width; float atlasRealHeight = atlas.Atlas.height; Vector2 atlasScale = new Vector2(atlasRealWidth / atlasWidth, atlasRealHeight / atlasHeight); IntVector2 texSize = meshData.texData.Element.Size; Vector2 texLocalOffset = new Vector2(texSize.x / (float)atlasWidth, texSize.y / (float)atlasHeight); for (int j = 0; j < meshData.uv.Length; ++j) { uvs[vertexArrayIndex + j] = new Vector2(meshData.uv[j].x * texLocalOffset.x, meshData.uv[j].y * texLocalOffset.y) + new Vector2(uvOffset.x * atlasScale.x, uvOffset.y * atlasScale.y); } if (hasUV2Data) { meshData.uv2.CopyTo(uv2s, vertexArrayIndex); } if (hasColorData) { meshData.colors.CopyTo(colors, vertexArrayIndex); } if (hasNormalData) { meshData.normals.CopyTo(normals, vertexArrayIndex); } vertexArrayIndex += meshData.vertices.Length; triangleArrayIndex += meshData.triangles.Length; } } combineMesh.vertices = vertices; combineMesh.SetTriangles(triangles, 0); combineMesh.uv = uvs; combineMesh.uv2 = uv2s; combineMesh.colors = colors; combineMesh.RecalculateNormals();// FIXME return(combineMesh); }